NAME

Mojolicious::Plugin::TagHelpers::NoCaching - Force images, styles, js reloading by the browser if they were modified on filesystem

SYNOPSIS

    use Mojolicious::Lite;
    
    plugin 'TagHelpers::NoCaching', {key => 'v'};
    
    get '/' => 'index';
    
    __DATA__
    
    @@index.html.ep
    <html>
        <head>
            %= javascript_nc "/js/app.js"
            %= stylesheet_nc "/css/app.css"
        </head>
        <body>
            My greate photo:<br>
            %= image_nc "/img/avatar.jpg"
        </body>
    </html>

DESCRIPTION

When you updating your project on production server with new version, new version often contains changed styles, javascript, images. You fetched all new files from the repository, restarted application, but browsers still shows you old images, your html looks like a shit (because of the old styles on new html), javascript events doesn't work (because of the old js in use). All of this because your browser cached old version of included files and don't want to reload it.

If you ever come across this, this module will help you.

HOW IT WORKS

This plugin contains several helpers described below. All this helpers are alternatives for helpers with same name (but without _nc suffix) from Mojolicious::Plugin::TagHelpers. "_nc" suffix in helpers names means "no caching". Behaviour of this helpers are identical except that helpers from this module adds query parameter with file version for each file included with help of them. For now query parameter is modification time of the file. So we can guarantee that when file will be modified query parameter will be changed and file will be reloaded by the browser on next request. This works only for server local files included with absolute url ("http://host/file.css"), absolute path ("/file.css") or relative path ("file.css"). And they will become something like "http://host/file.css?nc=1384766621", "/file.css?nc=1384766621", "file.css"?nc=1384766621" respectively.

One important thing is that query parameter for modified file will be changed only after application reload, because modification time for included files will be cached to be more efficient. This shouldn't be big problem, because when you updating your app with new version you also changed your perl files and should reload application. Or if you are on development morbo server it will reload application for you.

CONFIGURATION

Config for plugin accepts this options

key

Which query key should be used. Default is "nc".

HELPERS

Mojolicious::Plugin::TagHelpers::NoCaching implements the following helpers

javascript_nc "url_or_path"

Same as javascript, but will add query key and value to prevent caching

stylesheet_nc "url_or_path"

Same as stylesheet, but will add query key and value to prevent caching

image_nc "url_or_path"

Same as image, but will add query key and value to prevent caching

SEE ALSO

Mojolicious::Plugin::TagHelpers

COPYRIGHT

Copyright Oleg G <oleg@cpan.org>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.