Skip to content

Commit

Permalink
fix #61: Bad content-type from cached resources with Chrome (304 not …
Browse files Browse the repository at this point in the history
…modified)
  • Loading branch information
greenlaw110 committed Apr 30, 2013
1 parent 98d5b13 commit cbeb6ee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
16 changes: 8 additions & 8 deletions java/play/app/controllers/greenscript/Service.java
@@ -1,12 +1,12 @@
package controllers.greenscript;

import java.util.Map;

import play.modules.greenscript.GreenScriptPlugin;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Scope.Flash;

import java.util.Map;

public class Service extends Controller {

public static void getInMemoryCache(String key) {
Expand All @@ -17,17 +17,17 @@ public static void getInMemoryCache(String key) {
response.cacheFor(etag, "100d", l);
Flash.current().keep();

Map<String, Http.Header> headers = request.headers;
if (headers.containsKey("if-none-match") && headers.containsKey("if-modified-since")) {
response.status = Http.StatusCode.NOT_MODIFIED;
return;
}

if (key.endsWith(".js")) {
response.setContentTypeIfNotSet("text/javascript");
} else if (key.endsWith(".css")) {
response.setContentTypeIfNotSet("text/css");
}

Map<String, Http.Header> headers = request.headers;
if (headers.containsKey("if-none-match") && headers.containsKey("if-modified-since")) {
response.status = Http.StatusCode.NOT_MODIFIED;
return;
}

renderText(content);
}
Expand Down
6 changes: 6 additions & 0 deletions java/play/conf/application.conf 100644 → 100755
Expand Up @@ -103,6 +103,12 @@
# default: false
# greenscript.less.enabled=true

# Add ?ts=timestamp to css?
# greenscript.css.timestamp=true

# Add ?ts=timestamp to js?
# greenscript.js.timestamp=true

# Configure for product mode
%prod.application.mode=prod
%prod.greenscript.minimize=true
Expand Down
2 changes: 1 addition & 1 deletion java/play/conf/dependencies.yml
@@ -1,4 +1,4 @@
self: play -> greenscript 1.2.11a
self: play -> greenscript 1.2.11b

require:
- play [1.2,)
17 changes: 12 additions & 5 deletions java/play/src/play/modules/greenscript/GreenScriptPlugin.java
Expand Up @@ -20,8 +20,6 @@
import play.mvc.Http.Response;
import play.mvc.Router;
import play.mvc.Scope.Flash;
import play.mvc.results.NotFound;
import play.mvc.results.RenderStatic;
import play.templates.Template;
import play.utils.Utils;
import play.vfs.VirtualFile;
Expand All @@ -42,7 +40,7 @@
*/
public class GreenScriptPlugin extends PlayPlugin {

public static final String VERSION = "1.2.11";
public static final String VERSION = "1.2.11b";

private static String msg_(String msg, Object... args) {
return String.format("GreenScript-" + VERSION + "> %1$s",
Expand Down Expand Up @@ -302,6 +300,11 @@ public boolean serveStatic(VirtualFile file, Request request, Response response)
final long l = file.lastModified();
final String etag = "\"" + l + "-" + file.hashCode() + "\"";
Map<String, Http.Header> headers = request.headers;
if (fn.endsWith(".js")) {
response.setContentTypeIfNotSet("text/javascript");
} else if (fn.endsWith(".css")) {
response.setContentTypeIfNotSet("text/css");
}
if (headers.containsKey("if-none-match") && headers.containsKey("if-modified-since")) {
if ("GET".equalsIgnoreCase(request.method)) {
response.status = Http.StatusCode.NOT_MODIFIED;
Expand Down Expand Up @@ -367,7 +370,13 @@ private boolean processStatic_(VirtualFile file, Request req, Response resp, Res
if (Play.mode == Mode.PROD) {
resp.cacheFor("1h");
}
if (type == ResourceType.CSS) {
resp.setContentTypeIfNotSet("text/css");
} else if (type == ResourceType.JS) {
resp.setContentTypeIfNotSet("text/javascript");
}
IMinimizer min = type == ResourceType.CSS ? cssM_ : jsM_;

long l = min.getLastModified(file.getRealFile());
final String etag = "\"" + l + "-" + file.hashCode() + "\"";
if (!req.isModified(etag, l)) {
Expand All @@ -382,10 +391,8 @@ private boolean processStatic_(VirtualFile file, Request req, Response resp, Res
return false;
}
} else {

try {
String content = min.processStatic(file.getRealFile());
resp.contentType = type == ResourceType.JS ? "text/javascript" : "text/css";
resp.status = 200;
resp.print(content);
resp.setHeader(Names.LAST_MODIFIED, Utils.getHttpDateFormatter().format(new Date(l + 1000)));
Expand Down

0 comments on commit cbeb6ee

Please sign in to comment.