Sometimes you are fetching static content like images, JavaScript code and CSS files from a different server that your Catalyst application is running on, let's say a CDN (content delivery network). For development, this often should not be the case because your local files may differ from the files on your CDN. Changing the URIs to the static content everytime you upload a new version of your application is not a solution and leads to mistakes.

Entering CatalystX::UriForStatic:

CatalystX::UriForStatic takes care of your static host name

CatalystX::UriForStatic either creates a local or production URI to your static files depending on your configuration! If your differences are that simple so you just can switch the host names, CatalystX::UriForStatic is for you.

Example

Production environment

Let's pretend this is your Catalyst application while sysenv controls whether your application is a development or production instance and static_host defines the host name of your static files for production environment:

package MyApp;
use Moose;
use namespace::autoclean;

extends 'Catalyst';
with 'CatalystX::UriForStatic';

__PACKAGE__->config(
    static_host => 'http://static.example.net',
    sysenv      => 'production',
    ...
);

In your templates where you point to your images etc. change the URIs to:

<% $c->uri_for_static('/static/foo.png') %>

(Note this is Mason syntax.)

On your production environment like the one above this will return http://static.example.net/static/foo.png.

Development or local environment

Leave everything as it and add a new config file myapp_local.yaml (See Catalyst::Plugin::ConfigLoader) with the following configuration line:

sysenv  local

Now, calling "uri_for_static" in CatalystX::UriForStatic will return something like http://localhost/static/foo.png or whatever your local host name is.

Not perfect in either way

There are plenty of different ways of your specific CDN implementation like not having the same paths for both systems. While adding a folder name to the static_host configuration is not an issue, removing one from the local path is. Suggestions are very welcome, as well as patches and so on.

Installation

To install this module, run the following commands:

perl Build.PL
./Build
./Build test
./Build install

Copyright (C) 2011 Matthias Dietrich

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.