staticFiles static method

void staticFiles(
  1. String prefix,
  2. String folder, {
  3. Duration? maxAge,
})

serve the files in folder under prefix, for GET and HEAD requests that match no route. Routes always win, so a normal request does not touch the disk. No middleware runs for a file. Like other routes, prefix follows group, the router prefix and domain. folder is relative to the folder the app starts in. Never save user uploads in folder: an uploaded html or svg file would run its script on your site. Use dox_storage for them. .., dot files (like .env) and folders are never sent: they get 404. Only a .well-known folder at the top of folder is sent. A file gets ETag and Last-Modified (and 304 when the browser has it), Range support, and Cache-Control: public, max-age=... with maxAge. Give hashed build files (like Vite's) a long maxAge.

Route.staticFiles('/', 'public');
Route.staticFiles('build', 'public/build', maxAge: Duration(days: 365));

Behind a proxy (nginx, Caddy), the proxy can serve the folder instead.

Implementation

static void staticFiles(String prefix, String folder, {Duration? maxAge}) {
  Route r = Route();
  _statics.add(
    StaticFolder(r._join(prefix), folder, maxAge: maxAge, domain: r._domain),
  );
}