Dancer::Plugin::CDN - Serve static files with unique URLs and far-future expiry
use Dancer::Plugin::CDN; # Generate a CDN URL for a static file my $style_sheet = cdn_url('css/style.css'); # e.g.: "/cdn/css/style.B97EA317759D.css" # Or, in a TT2 template: <link rel="stylesheet" href="[% cdn_url('css/style.css') %]" >
This plugin generates URLs for your static files that include a content hash so that the URLs will change when the content changes. The plugin also arranges for the files to be served with cache-control and expiry headers to enable the content to be cached by the browser.
The real work is performed by the HTTP::CDN module which can also be configured with plugins to minify CSS/JS on-the-fly and also to render LESS to CSS.
A single helper function is exported into the caller's namespace. This function is also made available to be called from within your TT2 templates (probably won't work with other template engines).
Takes a pathname to a static file (e.g.: css/style.css) and returns a URL with content-hash and configurable CDN prefix added (e.g.: /cdn/css/style.B97EA317759D.css);
css/style.css
/cdn/css/style.B97EA317759D.css
You do not need to configure this module although you may choose to add a section like this to your Dancer config file:
plugins: CDN: root: "static" base: "/cdn/" plugins: - "CSS" - "CSS::Minifier::XS"
The root setting defines where the static source files can be found. By default this points to Dancer's standard public directory.
root
public
The base setting is the prefix which will be added to each URL. The default value is /cdn/. The plugin will also use this prefix to set up a route handler for serving the static content. This setting can include a hostname e.g.:
base
/cdn/
base: "http://static.example.com/cdn/"
The plugins setting should be an array of HTTP::CDN plugin names. The default setting is to enable only the HTTP::CDN::CSS plugin which rewrites URLs (e.g.: for image files) to the CDN scheme.
plugins
Bug reports and feature requests
https://github.com/grantm/Dancer-Plugin-CDN/issues
Source Code Repository
http://github.com/grantm/Dancer-Plugin-CDN/
Copyright 2012 Grant McLean <grantm@cpan.org>
<grantm@cpan.org>
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Dancer::Plugin::CDN, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer::Plugin::CDN
CPAN shell
perl -MCPAN -e shell install Dancer::Plugin::CDN
For more information on module installation, please visit the detailed CPAN module installation guide.