staticFiles static method

void staticFiles(
  1. String prefix,
  2. String folder, {
  3. Duration? maxAge,
  4. bool precompressed = false,
})

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));

With precompressed, a file that has a .br (brotli) or .gz (gzip) file next to it is sent compressed, when the client accepts that coding: app.js.br for app.js. Dox does not make these files: your build makes them. The answer gets Content-Encoding and Vary: Accept-Encoding. Default false.

Route.staticFiles('/', 'public', precompressed: true);

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

Implementation

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