1Mojolicious::Static(3)User Contributed Perl DocumentationMojolicious::Static(3)
2
3
4

NAME

6       Mojolicious::Static - Serve static files
7

SYNOPSIS

9         use Mojolicious::Static;
10
11         my $static = Mojolicious::Static->new;
12         push @{$static->classes}, 'MyApp::Controller::Foo';
13         push @{$static->paths}, '/home/sri/public';
14

DESCRIPTION

16       Mojolicious::Static is a static file server with "Range",
17       "If-Modified-Since" and "If-None-Match" support, based on RFC 7232
18       <https://tools.ietf.org/html/rfc7232> and RFC 7233
19       <https://tools.ietf.org/html/rfc7233>.
20

ATTRIBUTES

22       Mojolicious::Static implements the following attributes.
23
24   asset_dir
25         my $dir = $static->asset_dir;
26         $static = $static->asset_dir('assets');
27
28       Subdirectory used for all static assets, defaults to "assets".
29
30   classes
31         my $classes = $static->classes;
32         $static     = $static->classes(['main']);
33
34       Classes to use for finding files in "DATA" sections with Mojo::Loader,
35       first one has the highest precedence, defaults to "main". Only files
36       with exactly one extension will be used, like "index.html". Note that
37       for files to be detected, these classes need to have already been
38       loaded and added before "warmup" is called, which usually happens
39       automatically during application startup.
40
41         # Add another class with static files in DATA section
42         push @{$static->classes}, 'Mojolicious::Plugin::Fun';
43
44         # Add another class with static files in DATA section and higher precedence
45         unshift @{$static->classes}, 'Mojolicious::Plugin::MoreFun';
46
47   extra
48         my $extra = $static->extra;
49         $static   = $static->extra({'foo/bar.txt' => '/home/sri/myapp/bar.txt'});
50
51       Paths for extra files to be served from locations other than "paths",
52       such as the images used by the built-in exception and not found pages.
53       Note that extra files are only served if no better alternative could be
54       found in "paths" and "classes".
55
56         # Remove built-in favicon
57         delete $static->extra->{'favicon.ico'};
58
59   paths
60         my $paths = $static->paths;
61         $static   = $static->paths(['/home/sri/public']);
62
63       Directories to serve static files from, first one has the highest
64       precedence.
65
66         # Add another "public" directory
67         push @{$static->paths}, '/home/sri/public';
68
69         # Add another "public" directory with higher precedence
70         unshift @{$static->paths}, '/home/sri/themes/blue/public';
71

METHODS

73       Mojolicious::Static inherits all methods from Mojo::Base and implements
74       the following new ones.
75
76   asset_path
77         my $path = $static->asset_path('/app.js');
78
79       Get static asset path.
80
81   dispatch
82         my $bool = $static->dispatch(Mojolicious::Controller->new);
83
84       Serve static file for Mojolicious::Controller object.
85
86   file
87         my $asset = $static->file('images/logo.png');
88         my $asset = $static->file('../lib/MyApp.pm');
89
90       Build Mojo::Asset::File or Mojo::Asset::Memory object for a file,
91       relative to "paths" or from "classes", or return "undef" if it doesn't
92       exist. Note that this method uses a relative path, but does not protect
93       from traversing to parent directories.
94
95         my $content = $static->file('foo/bar.html')->slurp;
96
97   is_fresh
98         my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'});
99         my $bool = $static->is_fresh(
100           Mojolicious::Controller->new, {etag => 'W/"def"'});
101
102       Check freshness of request by comparing the "If-None-Match" and
103       "If-Modified-Since" request headers to the "ETag" and "Last-Modified"
104       response headers.
105
106       These options are currently available:
107
108       etag
109           etag => 'abc'
110           etag => 'W/"abc"'
111
112         Add "ETag" header before comparing.
113
114       last_modified
115           last_modified => $epoch
116
117         Add "Last-Modified" header before comparing.
118
119   serve
120         my $bool = $static->serve(Mojolicious::Controller->new, 'images/logo.png');
121         my $bool = $static->serve(Mojolicious::Controller->new, '../lib/MyApp.pm');
122
123       Serve a specific file, relative to "paths" or from "classes". Note that
124       this method uses a relative path, but does not protect from traversing
125       to parent directories.
126
127   serve_asset
128         $static->serve_asset(Mojolicious::Controller->new, Mojo::Asset::File->new);
129
130       Serve a Mojo::Asset::File or Mojo::Asset::Memory object with "Range",
131       "If-Modified-Since" and "If-None-Match" support.
132
133   warmup
134         $static->warmup();
135
136       Prepare static files from "classes" and static assets for future use.
137

SEE ALSO

139       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
140
141
142
143perl v5.36.0                      2023-01-20            Mojolicious::Static(3)
Impressum