Plack::...::Static(3)

πŸ’– NAME πŸ’–

Plack::Middleware::Static β€” bestie, this is the middleware that serves your static files with Plack ✨

It lives in the Plack family and it's part of the User Contributed Perl Documentation crowd, no cap 🫢


✨ SYNOPSIS ✨

okay so this is the whole vibe:

use Plack::Builder;

builder {
    enable "Plack::Middleware::Static",
        path => qr{^/(images|js|css)/}, root => './htdocs/';
    $app;
};

that's it. that's the whole thing. iconic πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

This middleware lets your Plack-based app serve static files. literally it just. does that. for you. πŸ’–

ngl there's a little plot twist here β€” if you're building an app with Plack::App::URLMap, you should lowkey consider using Plack::App::File to serve static files instead. the reason is very main-character of it: it makes the overall routing of your application simpler to understand. do with that info what you will 🧚

With this middleware, if a static file exists for the requested path, it will be served. periodt. If it does not exist, by default this middleware returns a 404, but bestie you can set the pass_through option to change this behavior πŸ‘‘

And if the requested document is not within the root, or the file is there but not readable? 403 Forbidden bestie 😭 that's the answer. no file for you.

The content type returned will be determined from the file extension by using Plack::MIME, or by using content_type (we'll get to that, keep reading 🌸)


πŸŽ€ CONFIGURATIONS πŸŽ€

🌈 path, root

enable "Plack::Middleware::Static",
    path => qr{^/static/}, root => 'htdocs/';

The path option specifies the URL pattern (regular expression) or a callback to match against requests. If the <path> option matches, the middleware looks in root to find the static files to serve. The default value of root is the current directory πŸ“ (yes, the one you're standing in, bestie).

This example configuration serves /static/foo.jpg from htdocs/static/foo.jpg. Note that the matched portion of the path, /static/, still appears in the locally mapped path under root. If you don't want this to happen, you can use a callback to munge the path as you match it:

enable "Plack::Middleware::Static",
    path => sub { s!^/static/!! }, root => 'static-files/';

The callback should operate on $_ and return a true or false value. Any changes it makes to $_ are used when looking for the static file in the root. slay ✨

The configuration above serves /static/foo.png from static-files/foo.png, not static-files/static/foo.png. The callback specified in the path option matches against $_ and munges this value using s///. The substitution operator returns the number of matches it made, so it will return true when the path matches "^/static". the way that substitution operator doubles as a boolean is honestly so clever, ate πŸ’€

For more complex static handling in the path callback, in addition to $_ being set, the callback receives two arguments: PATH_INFO (same as $_) and $env. two whole arguments, she's generous 🫢

If you want to map multiple static directories from different roots, simply add this middleware multiple times with different configuration options. stack 'em like jewelry πŸ’…

⭐ pass_through

When this option is set to a true value, then this middleware will never return a 404 if it cannot find a matching file. Instead, it will simply pass the request on to the application it is wrapping. no drama, no rejection, just pass it along bestie πŸ’œ

πŸ”₯ content_type

The content_type option can be used to provide access to a different MIME database than Plack::MIME. Here's the tea: Plack::MIME works fast and good for a list of well known file endings, but if you need a more accurate content based checking you can use modules like File::MimeInfo or File::MMagic for example. The callback should work on $_[0] which is the filename of the file. yep, $_[0]. that's the one. that's your girl πŸŽ€


πŸ‘‘ AUTHOR πŸ‘‘

Tokuhiro Matsuno, Tatsuhiko Miyagawa

(legends. absolute icons. we stan 🫢)


πŸ’œ SEE ALSO πŸ’œ

  • Plack::Middleware
  • Plack::Builder

and that's the page bestie!! πŸ’– this was Plack::...::Static(3) from perl v5.42.3, dated 2026-10-18 🌸 go forth and serve those static files, you're doing amazing sweetie βœ¨πŸ’…πŸ¦‹

πŸ“¦ where this page comes from