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

NAME

Leyland::Manual::StaticFiles - How to serve static files from your application

VERSION

version 0.001007

STATIC FILES

Pretty much every web application has static files, i.e. files which are not generated dynamically. These are mostly CSS, JavaScript, images, videos, etc.

How to serve static files from your application depends on the way you run the application and your personal preferences. During development and testing, when you're most likely to run your application by simply using plackup, and the easiest way to serve static files would be to let the Static middleware provided with Plack do it. Simply add the following to the builder section of app.psgi (or create that section if it doesn't already exist):

        use Plack::Builder;

        ... rest of app.psgi ...
        my $app = ...
        ... rest of app.psgi ...

        builder {
                enable 'Static',
                        path => qr{^/((images|js|css)/|favicon\.ico$|apple-touch-icon\.png$)},
                        root => './public/';
                $app;
        };

In builder, we enable Plack::Middleware::Static, and tell it to serve any requests to paths that start with "images", "js" or "css" (plus the files "favicon.ico" and "apple-touch-icon.png") from the "public/" directory of your application.

When deploying your application, however, it is likely that you will run the application behind a frontend webserver like Apache, nginx, lighttpd or Cherokee. If so, you might find it better to let them serve the static files of your application. I will not go into detail how to perform this, as every web server is configured differently. If you are doing this, then you don't need the static middleware.

A good option when doing this, however, is enabling the Static middleware only when running on the "development" environment (or if not running on the "deployment" environment), like so:

        builder {
                enable_if { $ENV{PLACK_ENV} eq 'development' } 'Static',
                        path => qr{^/((images|js|css)/|favicon\.ico$|apple-touch-icon\.png$)},
                        root => './public/';
        }

And that's pretty much it.

WHAT'S NEXT?

Read Leyland::Manual::Localization to learn how to localize your applications or return to the table of contents.

AUTHOR

Ido Perlmuter, <ido at ido50.net>

BUGS

Please report any bugs or feature requests to bug-Leyland at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Leyland. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc Leyland::Manual::StaticFiles

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2010-2011 Ido Perlmuter.

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.