Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static file routing #12

Closed
wants to merge 2 commits into from

Conversation

lmcalpin
Copy link
Contributor

Small change to add a way to declare a staticFile route, which maps a single GET request to a specific file. Reuses much of the staticDir logic except, of course, it's specific to one file.

Example route:

GET /foobar staticFile:public/stylesheets/main.css
GET /favicon.ico staticFile:public/images/favicon.png
GET /public/ staticDir:public

This allows me to make /favicon.ico work even though I use the standard Play! convention of having static files under 'public'.

@amyboyd
Copy link

amyboyd commented Oct 27, 2010

If you use "staticFile" in routing, you can't customize response headers. For favicons, a cache header is needed so the browser isn't making a favicon request on every page load.

I use an action:

public static void favicon() throws FileNotFoundException
{
    File icon = play.Play.getFile("public/favicon.ico");
    InputStream is = new FileInputStream(icon);
    response.setHeader("Content-Length", icon.length() + "");
    response.cacheFor("2h");
    response.contentType = "image/x-icon";
    response.direct = is; // renderBinary() will override any caching headers.
}

@lmcalpin
Copy link
Contributor Author

This isn't intended specifically for favicon. That's just an example. This lets you add any arbitrary static file path.

Furthermore, a better solution would be to have a filter that lets you configure or customize cache rules based on contentType - instead of littering controllers with actions that deal with static content and don't have real business logic. However, that cache filter solution should be a separate bug request as it is orthogonal to static file routing which can be used for anything, not just favicons.

@guillaumebort
Copy link
Contributor

Thank you, done.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants