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);


You do not need to configure this module although you may choose to add a section like this to your Dancer config file:

      root: "static"
      base: "/cdn/"
        - "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.

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: ""

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.



Copyright 2012 Grant McLean <>

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 for more information.