Search code examples
perlcdncatalyst

Serving static files from a CDN with Catalyst


I'm trying to serve all static files (js, css, images) from a CDN instead of the /static folder. I started writing my own sub to rewrite image URLs:

sub uri_for_image { 
    my ( $c, $path, @args ) = @_; 
    my $uri = $c->uri_for($path, @args);

  if (MyApp->config->{use_cdn}) {
    my $cdn = MyApp->config->{cdn_location};
    $uri = $path;
    $uri =~ s/^/$cdn/si;
  } 

return $uri;
}

But then I realized its gonna take a lot more work to get all the static files rerouted automatically. How should I go about setting a CDN url for static files?

I was looking into configuring Static::Simple but it seems to only have settings for location of files locally.


Solution

  • What do you think Catalyst actually can and should do for you here?

    I'm afraid if you want to generate your own links which are outside the app, then you're pretty much on your own—there isn't anything Catalyst can really do or provide to help you here, is there?

    Or do you just want uri_for to smash all paths starting in /static to point your CDN? As you can just wrap uri_for and make it do that?

    I think your version (having a separate method) is cleaner and neater, but if all your static content is going onto the CDN, then just giving uri_for the behavior you want is reasonable.