The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Template::Plugin::File::StaticURL - Appends mtime and file size to the URL of static files to ensure freshness.

SYNOPSIS

   [% 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.

DESCRIPTION

This plugin stats static files and generates a URL with it's modification time and file size appended. The hack ensures that changed static files (such as javascript and css) are reloaded by the client if they are changed on the server, regardless of caching in the HTTP chain.

It is particularly useful for deploying deploy new releases of a web application into production, and need to make sure that updated javascript and css files are served fresh to the client.

PARAMETERS

 [% 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.

 %]

SERVER CONFIGURATION

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.

Using the '/' prefix, and enabling Path Info.

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:

  AcceptPathInfo on

Using the '?' prefix placing the meta in the query string.

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.

METHODS

url

Takes the path to the static file as the only argument, and returns by default a relative URL of the form:

   /js/script.js/1252815667/15564

Set the 'url_prefix' option if you need a prefix.

BUGS

Bugs probably exists. Email me if you find any.

AUTHOR

Stig Palmquist <stigtsp@gmail.com>

COPYRIGHT

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.