1Mojolicious::Static(3)User Contributed Perl DocumentationMojolicious::Static(3)
2
3
4
6 Mojolicious::Static - Serve static files
7
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
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
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
72 prefix
73 my $prefix = $static->prefix;
74 $static = $static->prefix('/static');
75
76 Prefix to use for all static files, defaults to "undef". This can be
77 very useful for production deployments where the reverse proxy server
78 should take over serving static files.
79
81 Mojolicious::Static inherits all methods from Mojo::Base and implements
82 the following new ones.
83
84 asset_path
85 my $path = $static->asset_path('/app.js');
86
87 Get static asset path.
88
89 dispatch
90 my $bool = $static->dispatch(Mojolicious::Controller->new);
91
92 Serve static file for Mojolicious::Controller object.
93
94 file
95 my $asset = $static->file('images/logo.png');
96 my $asset = $static->file('../lib/MyApp.pm');
97
98 Build Mojo::Asset::File or Mojo::Asset::Memory object for a file,
99 relative to "paths" or from "classes", or return "undef" if it doesn't
100 exist. Note that this method uses a relative path, but does not protect
101 from traversing to parent directories.
102
103 my $content = $static->file('foo/bar.html')->slurp;
104
105 file_path
106 my $path = $static->file_path('/index.html');
107
108 Get static file path with "prefix" if it has been configured.
109
110 is_fresh
111 my $bool = $static->is_fresh(Mojolicious::Controller->new, {etag => 'abc'});
112 my $bool = $static->is_fresh(
113 Mojolicious::Controller->new, {etag => 'W/"def"'});
114
115 Check freshness of request by comparing the "If-None-Match" and
116 "If-Modified-Since" request headers to the "ETag" and "Last-Modified"
117 response headers.
118
119 These options are currently available:
120
121 etag
122 etag => 'abc'
123 etag => 'W/"abc"'
124
125 Add "ETag" header before comparing.
126
127 last_modified
128 last_modified => $epoch
129
130 Add "Last-Modified" header before comparing.
131
132 serve
133 my $bool = $static->serve(Mojolicious::Controller->new, 'images/logo.png');
134 my $bool = $static->serve(Mojolicious::Controller->new, '../lib/MyApp.pm');
135
136 Serve a specific file, relative to "paths" or from "classes". Note that
137 this method uses a relative path, but does not protect from traversing
138 to parent directories.
139
140 serve_asset
141 $static->serve_asset(Mojolicious::Controller->new, Mojo::Asset::File->new);
142
143 Serve a Mojo::Asset::File or Mojo::Asset::Memory object with "Range",
144 "If-Modified-Since" and "If-None-Match" support.
145
146 warmup
147 $static->warmup();
148
149 Prepare static files from "classes" and static assets for future use.
150
152 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
153
154
155
156perl v5.38.0 2023-09-11 Mojolicious::Static(3)