Mojolic...::Static(3)
π NAME π
okay so this is the Mojolic...::Static(3) page from the User Contributed Perl Documentation besties, and the module is Mojolicious::Static - Serve static files. That's the name, that's the vibe, periodt. β¨
β¨ SYNOPSIS β¨
tiny taste of the setup, bestie:
use Mojolicious::Static;
my $static = Mojolicious::Static->new;
push @{$static->classes}, 'MyApp::Controller::Foo';
push @{$static->paths}, '/home/sri/public';
πΈ DESCRIPTION πΈ
Mojolicious::Static is literally a static file server with "Range", "If-Modified-Since" and "If-None-Match" support, based on RFC 7232 <https://tools.ietf.org/html/rfc7232> and RFC 7233 <https://tools.ietf.org/html/rfc7233>. slay. π¦
π ATTRIBUTES π
Mojolicious::Static implements the following attributes. no skipping, bestie β all of them are here π
π asset_dir
my $dir = $static->asset_dir;
$static = $static->asset_dir('assets');
Subdirectory used for all static assets, defaults to "assets". cute. β¨
π§ classes
my $classes = $static->classes;
$static = $static->classes(['main']);
Classes to use for finding files in "DATA" sections with Mojo::Loader, first one has the highest precedence, defaults to "main". Only files with exactly one extension will be used, like "index.html". Note that for files to be detected, these classes need to have already been loaded and added before warmup is called, which usually happens automatically during application startup. π
# Add another class with static files in DATA section
push @{$static->classes}, 'Mojolicious::Plugin::Fun';
# Add another class with static files in DATA section and higher precedence
unshift @{$static->classes}, 'Mojolicious::Plugin::MoreFun';
π¦ extra
my $extra = $static->extra;
$static = $static->extra({'foo/bar.txt' => '/home/sri/myapp/bar.txt'});
Paths for extra files to be served from locations other than "paths", such as the images used by the built-in exception and not found pages. Note that extra files are only served if no better alternative could be found in "paths" and "classes". π«Ά
# Remove built-in favicon
delete $static->extra->{'favicon.ico'};
π paths
my $paths = $static->paths;
$static = $static->paths(['/home/sri/public']);
Directories to serve static files from, first one has the highest precedence. π
# Add another "public" directory
push @{$static->paths}, '/home/sri/public';
# Add another "public" directory with higher precedence
unshift @{$static->paths}, '/home/sri/themes/blue/public';
π prefix
my $prefix = $static->prefix;
$static = $static->prefix('/static');
Prefix to use for all static files, defaults to "undef". This can be very useful for production deployments where the reverse proxy server should take over serving static files. π
π¦ METHODS π¦
Mojolicious::Static inherits all methods from Mojo::Base and implements the following new ones. let's gooo β¨
β asset_path
my $path = $static->asset_path('/app.js');
Get static asset path.
π₯ dispatch
my $bool = $static->dispatch(Mojolicious::Controller->new);
Serve static file for Mojolicious::Controller object.
π file
my $asset = $static->file('images/logo.png');
my $asset = $static->file('../lib/MyApp.pm');
Build Mojo::Asset::File or Mojo::Asset::Memory object for a file, relative to "paths" or from "classes", or return "undef" if it doesn't exist. Note that this method uses a relative path, but does not protect from traversing to parent directories. π
bestie, read that again: relative path, and no protection from traversing to parent directories. That's the documented caveat, fr fr.
my $content = $static->file('foo/bar.html')->slurp;
πΈ file_path
my $path = $static->file_path('/index.html');
Get static file path with "prefix" if it has been configured.
π§ is_fresh
my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'});
my $bool = $static->is_fresh(
Mojolicious::Controller->new, {etag => 'W/"def"'});
Check freshness of request by comparing the "If-None-Match" and "If-Modified-Since" request headers to the "ETag" and "Last-Modified" response headers.
These options are currently available:
etagβ Add"ETag"header before comparing.
etag => 'abc'
etag => 'W/"abc"'
last_modifiedβ Add"Last-Modified"header before comparing.
last_modified => $epoch
π«Ά serve
my $bool = $static->serve(Mojolicious::Controller->new, 'images/logo.png');
my $bool = $static->serve(Mojolicious::Controller->new, '../lib/MyApp.pm');
Serve a specific file, relative to "paths" or from "classes". Note that this method uses a relative path, but does not protect from traversing to parent directories. π
same caveat as file, bestie β relative path, no protection from traversing to parent directories. periodt.
π serve_asset
$static->serve_asset(Mojolicious::Controller->new, Mojo::Asset::File->new);
Serve a Mojo::Asset::File or Mojo::Asset::Memory object with "Range", "If-Modified-Since" and "If-None-Match" support. π₯
π warmup
$static->warmup();
Prepare static files from "classes" and static assets for future use.
π SEE ALSO π
Mojolicious, Mojolicious::Guides, and <https://mojolicious.org>. go check 'em out, bestie π
and that's the whole page, bestie π«Άβ¨ β source/version perl v5.42.0, date 2026-07-15, and this Mojolic...::Static(3) footer energy is officially wrapped. bye, slay! π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms