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       <http://tools.ietf.org/html/rfc7232> and RFC 7233
19       <http://tools.ietf.org/html/rfc7233>.
20

ATTRIBUTES

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

METHODS

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

SEE ALSO

128       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
129
130
131
132perl v5.32.0                      2020-07-28            Mojolicious::Static(3)
Impressum