- ASSETS FROM CUSTOM DOMAIN
- ONLINE ASSETS
- SEE ALSO
Mojolicious::Plugin::AssetPack::Guides::Cookbook - AssetPack recipes
This guide will provide recipes for cooking with Mojolicious::Plugin::AssetPack.
You might want to serve the assets from a domain different from where the main app is running. The reasons for that might be:
No cookies send on each request. This is especially useful when you use Mojolicious sessions as they are stored in cookies and clients send whole session with every request.
More requests done in parallel. Browsers have limits for sending parallel request to one domain. With separate domain static files can be loaded in parallel.
Serve files directly (by absolute url) from CDN (or Amazon S3).
You need to customize "route" in Mojolicious::Plugin::AssetPack to accomplish this:
$app->asset->route->to(base_url => "http://cdn.example.com/my-assets/");
Many recent browsers blocks mixed content, meaning if your HTML is served over HTTPS, then you can't serve the assets over HTTP. One way to fix this is by using "//" instead of a scheme specific URL. Example:
base_url => "//cdn.example.com/my-assets/"
This will tell the browser to use the same scheme for fetching assets, as it did fetching the web page.
Instead of making custom AssetPack plugins, it is possible to simply include projects such as Bootstrap, FontAwesome or jQuery directly from the web instead.
The SASS version of http://getbootstrap.com can easily be included:
$app->asset->process( "app.css" => ( "https://github.com/twbs/bootstrap-sass/raw/master/assets/stylesheets/_bootstrap.scss" ) );
https://fortawesome.github.io/Font-Awesome/ can be included with the snippet below:
$app->asset->process( "app.css" => ( "https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css", "your-app.css", ) );
https://www.google.com/fonts can be included with this snippet:
$app->asset->process( "app.css" => ( "https://fonts.googleapis.com/css?family=Roboto:400,700", "your-app.css", ) );
https://github.com/Modernizr/Modernizr contains a list of many shims which can be included. Here is just one example:
$app->asset->process( "app.js" => ( "http://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.3.0/es5-shim.js", "http://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.3.0/es5-sham.js", "your-app.js", ) );
http://jquery.com can easily be included by refering to a CDN:
$app->asset->process( "app.js" => ( "http://code.jquery.com/jquery-1.12.0.js", "your-app.js", ) );
$app->asset->process( "app.css" => ( "https://github.com/Dogfalo/materialize/blob/master/sass/materialize.scss" ) );