Template::Plugin::File::StaticURL - Appends mtime and file size to the URL of static files to ensure freshness.
[% USE Static.Stat file_root='/var/www' %] <script src="[% Static.Stat.url('/js/script.js') %]"> <!-- Generates: /js/script.js/$mtime/$size --> <style>@import url([% Static.Stat.url('/css/styles.css') %]);</style> <!-- Generates: /css/styles.css/$mtime/$size -->
NOTE: You might need to configure your web server to accept path info to use this module with default settings. Please read the "SERVER CONFIGURATION" section for more information.
[% USE Static.Stat file_root = '/var/www/' # The root of the files. (Required) url_root = 'http://foo/' # Url prefix. (Optional) graceful = 0 # Don't fatal if stat() fails. postfix_filename = 0 # Prepends filename to the end of the URL. # Useful for debugging with Firebug. prefix = '/' # Prefix that follows base url. %]
Since we're appending meta information to the end of the URL, it is important that the webserver "ignores" the appended path information. There are a couple of ways of ensuring this.
To use the default '/' prefix, you would need to set the server to accept path info. In Apache 2, add the following configuration to the relevant section:
Alternatively, you can use the '?' prefix to set the meta information in the query string to the static object. This is not optimal because some HTTP proxy servers might refuse to cache any elements that have '?' in the URL.
To migitate this issue, you should configure caching headers to be set in the directories where the static files are served. In Apache (using mod_expires), you could set the following parameters:
ExpiresActive On ExpiresDefault "access plus 90 days"
NOTE: Be careful just to set this in the location where your static content is served. And note that any URL will now be cached for a long time.
Takes the path to the static file as the only argument, and returns by default a relative URL of the form:
Set the 'url_prefix' option if you need a prefix.
Bugs probably exists. Email me if you find any.
Stig Palmquist <firstname.lastname@example.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.