Scroll to navigation

Mojolicious::Plugin::AssetPack::Store(3pm) User Contributed Perl Documentation Mojolicious::Plugin::AssetPack::Store(3pm)


Mojolicious::Plugin::AssetPack::Store - Storage for assets


  use Mojolicious::Lite;
  # Load plugin and pipes in the right order
  plugin AssetPack => {pipes => \@pipes};
  # Change where assets can be found
  # Change where assets are stored
  app->asset->store->paths->[0] = app->home->rel_file("some/directory");
  # Define asset
  app->asset->process($moniker => @assets);
  # Retrieve a Mojolicious::Plugin::AssetPack::Asset object
  my $asset = app->asset->store->asset("some/file.js");


Mojolicious::Plugin::AssetPack::Store is an object to manage cached assets on disk.

The idea is that a Mojolicious::Plugin::AssetPack::Pipe object can store an asset after it is processed. This will speed up development, since only changed assets will be processed and it will also allow processing tools to be optional in production environment.

This module will document meta data about each asset which is saved to disk, so it can be looked up later as a unique item using "load".


Mojolicious::Plugin::AssetPack::Store inherits all attributes from Mojolicious::Static implements the following new ones.


  $str = $self->asset_class;
  $self = $self->asset_class("Mojolicious::Plugin::AssetPack::Asset");

Holds the classname of which new assets will be constructed from.


  $hash_ref = $self->default_headers;
  $self = $self->default_headers({"Cache-Control" => "max-age=31536000"});

Used to set headers used by "serve_asset".


  $hash_ref = $self->fallback_headers;
  $self = $self->fallback_headers({"Cache-Control" => "max-age=300"});

Used to set headers used by "serve_fallback_for_assets".

This is currently an EXPERIMENTAL feature.


  $hash_ref = $self->fallback_templates;
  $self = $self->fallback_templates->{"css"} = Mojo::Template->new;

Used to set up templates used by "serve_fallback_for_assets".

This is currently an EXPERIMENTAL feature.


  $paths = $self->paths;
  $self = $self->paths([$app->home->rel_file("assets")]);

See "paths" in Mojolicious::Static for details.


  $ua = $self->ua;

See "ua" in Mojolicious::Plugin::AssetPack.


Mojolicious::Plugin::AssetPack::Store inherits all attributes from Mojolicious::Static implements the following new ones.


  $asset = $self->asset($url, $paths);

Returns a Mojolicious::Plugin::AssetPack::Asset object or undef unless $url can be found in $paths. $paths default to "paths" in Mojolicious::Static. $paths and $url can be...

  • helper://some.mojo.helper/some_identifier?format=css

    Will call a helper registered under the name "csome.mojo.helper", with the query parameters as arguments. Example:

      $output = $c->some->mojo->helper(some_identifier => {format => "css"});

    $output can be a scalar containing the asset content or a hash-ref with arguments passed on to Mojolicious::Plugin::AssetPack::Asset. Note that "format" need to be present in the URL or the returning hash-ref for this to work.

    This feature is currently EXPERIMENTAL. Let me know if you use it/find it interesting.


    An absolute URL will be downloaded from web, unless the host is "local": "local" is a special host which will run the request through the current Mojolicious application.

  • foo/bar

    An relative URL will be looked up using "file" in Mojolicious::Static.

Note that assets from web will be cached locally, which means that you need to delete the files on disk to download a new version.


  $bool = $self->load($asset, \%attr);

Used to load an existing asset from disk. %attr will override the way an asset is looked up. The example below will ignore minified and rather use the value from %attr:

  $bool = $self->load($asset, {minified => $bool});


  $self = $self->persist;

Used to save the internal state of the store to disk.

This method is EXPERIMENTAL, and may change without warning.


  $bool = $self->save($asset, \%attr);

Used to save an asset to disk. %attr are usually the same as "TO_JSON" in Mojolicious::Plugin::AssetPack::Asset and used to document metadata about the $asset so it can be looked up using "load".


  $self = $self->serve_asset($c, $asset);

Override "serve_asset" in Mojolicious::Static with the functionality to set response headers first, from "default_headers".

Will call "render" in Mojolicious::Plugin::AssetPack::Asset if available, after setting Content-Type header and other "default_headers".


  $self = $self->serve_fallback_for_assets($c, $topic, $assets);

Used to serve a fallback response for given $topic and a Mojo::Collection of "Mojolicious::Plugin::AssetPack::Asset" objects.

Will set the headers in "fallback_headers" and then either render either a template matching the extension from $topic from "fallback_templates", a 302 redirect to the actual asset, or a 404 Not Found.

This is currently an EXPERIMENTAL feature.



2021-09-30 perl v5.32.1