Jan 08 2010
Workaround for Google App Engine Static File 304 Not Modified (GAE)
Google App Engine does not support 304 (Not modified) response code for static files. This means it will always return response code 200 with full content data, even if the content has not changed since the last time the browser requested it. This is not good because it waste your bandwidth quotas and it will also cause unnecessary delays when loading pages with static content. This is a well known issue, see Google App Engine Issue 575 for more details.
There is actually a very simple workaround for this if you use Google App Engine for Java. Suppose you have the following static file:
/static/big_javascript_lib.js
In your .html files, you reference that the static file like this:
<script type=”text/javascript” src=”/static/big_javascript_lib.js“></script>
And everytime the browser will load a html page containing the static file, Google App Engine will return status code 200 with the full content of the file, even if the content was not modified since the last time the browser requested it.
The workaround is very simple. First, in “WEB-INF/web.xml”, define a servlet for path “/304_enabled/*” (you can use any path for which no static content exist):
<servlet>
<servlet-name>StaticServlet</servlet-name>
<servlet-class>com.opcode.servlet.StaticServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StaticServlet</servlet-name>
<url-pattern>/304_enabled/*</url-pattern>
</servlet-mapping>
Then, create the StaticServlet class like this:
package com.opcode.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class StaticServlet extends HttpServlet {
// This static file handler will correctly return a 304 (Not Modified)
// response code when appropriate. This is not the case with the default app
// engine static file handler.public static final String PATH = “/304_enabled”;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String file = request.getRequestURI().substring(PATH.length());
request.getRequestDispatcher(file).forward(request, response);
}
}
Finally, in your .html files, prepend “/304_enabled” to the references of all all static files like this:
<script type=”text/javascript” src=”/304_enabled/static/big_javascript_lib.js”></script>
You must NOT move your static files in a “/304_enabled” folder, let the static files where they are, the servlet will find them.
Now the servlet will correctly return response code 304 if the static content was not modified since the last time the browser requested it.
UPDATE: This issue is now fixed, so the workaround is not necessary anymore. Thanks Google.
2 responses so far
[...] GoodCamel Blog ยป Workaround for Google App Engine Static File 304 … [...]
I truly enjoy this blog.