ferne Zukunft Läuft die Kopfzeile für statische Inhalte ab
wie hier geschriebenhttp://developer.yahoo.com/performance/rules.html
Für statische Komponenten: Implementieren Sie die Richtlinie "Never expire", indem Sie den Expires-Header für die ferne Zukunft festlegen
Ich kann Leistung gewinnen, indem ich http-Anfragen mit der Antwort "304" vermeide.
Im offiziellen Spiel! Dokumentation Ich kann sehen, wie Cachesteuerungsanweisungen festgelegt werden, aber wie kann ich die ferne Zukunft festlegen. Läuft der Header ab?
Viele Grüße Nicola
bearbeiten: danke für das replay jetzt klappt es auch! hier gibt es die klassen:
conf / routes
# Static files
GET /assets/stylesheets/img/:name controllers.StaticFilesController.getBoostrapImg(name)
GET /assets/images/*name controllers.StaticFilesController.getImg(name)
GET /assets/stylesheets/*name controllers.StaticFilesController.getCss(name)
GET /assets/javascripts/*name controllers.StaticFilesController.getJs(name)
controller / StaticFilesController.java
package controllers;
import org.apache.http.impl.cookie.DateUtils;
import java.util.*;
import play.mvc.*;
import services.FileName;
import play.*;
public class StaticFilesController extends Controller {
private static String nextYearString = StaticFilesController
.getNextYearAsString();
public static Result getImg(String path) {
FileName fileName = new FileName(path);
response().setHeader(EXPIRES, nextYearString);
response().setContentType("image/" + fileName.extension());
return ok(Play.application().getFile("/public/images/" + path));
}
public static Result getBoostrapImg(String path) {
FileName fileName = new FileName(path);
response().setHeader(EXPIRES, nextYearString);
response().setContentType("image/" + fileName.extension());
return ok(Play.application().getFile(
"/public/images/" + fileName.filename() + "."
+ fileName.extension()));
}
public static Result getCss(String path) {
response().setHeader(EXPIRES, nextYearString);
response().setContentType("text/css");
return ok(Play.application().getFile("/public/stylesheets/" + path));
}
public static Result getJs(String path) {
response().setHeader(EXPIRES, nextYearString);
response().setContentType("application/x-javascript");
return ok(Play.application().getFile("/public/javascripts/" + path));
}
private static String getNextYearAsString() {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.YEAR, 1);
return DateUtils.formatDate(calendar.getTime());
}
}
services / FileName.java
package services;
/**
* This class assumes that the string used to initialize fullPath has a
* directory path, filename, and extension. The methods won't work if it
* doesn't.
*/
public class FileName {
private String fullPath;
private char pathSeparator, extensionSeparator;
public FileName(String str, char sep, char ext) {
fullPath = str;
pathSeparator = sep;
extensionSeparator = ext;
}
public FileName(String str)
{
fullPath = str;
pathSeparator = '/';
extensionSeparator = '.';
}
public String extension() {
int dot = fullPath.lastIndexOf(extensionSeparator);
return fullPath.substring(dot + 1);
}
public String filename() { // gets filename without extension
int dot = fullPath.lastIndexOf(extensionSeparator);
int sep = fullPath.lastIndexOf(pathSeparator);
return fullPath.substring(sep + 1, dot);
}
public String path() {
int sep = fullPath.lastIndexOf(pathSeparator);
return fullPath.substring(0, sep);
}
}
Und dasviews / main.scala.html
@(skin: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>LibreTitan</title>
<link rel="stylesheet" media="screen" href="@routes.StaticFilesController.getCss("bootstrap/bootstrap.min.css")">
@if(skin != null && !skin.equals("")) {
<link rel="stylesheet" media="screen" href="@routes.StaticFilesController.getCss(skin+".min.css")">
}
<link rel="shortcut icon" type="image/png" href="@routes.StaticFilesController.getImg("favicon.png")">
<script async src="@routes.StaticFilesController.getJs("jquery-1.9.0.min.js")"></script>
<script async src="@routes.StaticFilesController.getJs("bootstrap.min.js")"></script>
</head>
<body>
<div class="container">
@content
</div>
</body>
</html>