After an upgrade to Express 5, express.static serving a built frontend returns 404 for /.well-known/ files; why, and how should its cache options be set?
answer
- dotfiles default is 'ignore'
- hidden directories count now
- max-age=0 unless told otherwise
- milliseconds, not seconds
- index.html must not be immutable
basics
~20 sIn Express 5, express.static's dotfiles default 'ignore' also covers hidden directories in the path, so /.well-known/ is treated as missing and ends in 404. Serve that directory from its own mount, and set maxAge with immutable only for fingerprinted assets.
solid answer
~40 sExpress 5 ships serve-static 2, whose `dotfiles` option defaults to `'ignore'` and now applies to hidden **directories** in the path relative to the root, not only to dot-named files. A request for `/.well-known/acme-challenge/<token>` is treated as missing, falls through, and ends in 404; Express 4 served it. The targeted fix is `app.use('/.well-known', express.static(path.join(build, '.well-known')))`, because the hidden segment becomes part of the root, which is not checked; `dotfiles: 'allow'` on the whole build directory would also expose any other dotfile there. For caching, `maxAge` defaults to `0` (`Cache-Control: public, max-age=0`) and takes milliseconds or an `ms` string; for fingerprinted bundles use `maxAge: '1y', immutable: true`, and send `no-cache` for `index.html` through `setHeaders` or a separate mount.
go deeper
Recall that express.static serves files from a folder and calls next() when a file is missing, and that maxAge sets the Cache-Control max-age.
Explain the dotfiles values, maxAge in milliseconds or ms strings, and why immutable needs a maxAge to matter.
Diagnose the Express 5 .well-known 404 from the dotfiles change, fix it on a narrow mount, and split long caching for hashed assets from no-cache for index.html.
Decide whether static files belong in the Express process at all or at the proxy or CDN, and which cache policy a team's build output must follow.
## What changed in Express 5 `express.static(root, options)` is Express's bundled **serve-static** middleware. It maps the request path onto files under `root`; when a file is missing it calls `next()` so later middleware or routes can answer. Its **`dotfiles`** option decides what happens to paths with a segment starting with a dot: | Value | Behaviour | |---|---| | `'ignore'` (default) | pretend the file does not exist: 404 or `next()` | | `'deny'` | refuse it: 403 or `next()` (with the default `fallthrough: true` it also falls through) | | `'allow'` | no special treatment | The Express 5 migration guide records two changes. The `hidden` and `from` options are gone in favour of `dotfiles` and `root`, and the `dotfiles` check now covers **hidden directories** in the request path, not just hidden files. In Express 4 a hidden directory such as `.well-known` was served by default; in Express 5 a request like `GET /.well-known/acme-challenge/<token>` is treated as missing. The middleware falls through, nothing else matches, and the client gets 404. Certificate challenges and other well-known URIs are the usual casualties, and they fail quietly because the rest of the site still works. The check covers only the part of the path relative to `root`; a hidden directory inside `root` itself is not inspected. ## Fixing it without exposing other dotfiles There are three fixes, in order of preference: 1. **Mount the hidden directory as its own root**: `app.use('/.well-known', express.static(path.join(build, '.well-known')))`. The dot segment is now part of the root, so the default `'ignore'` still protects everything else. 2. **Opt in on a narrow mount**: `express.static(dir, { dotfiles: 'allow' })` on a directory that contains nothing else hidden. 3. **Avoid `dotfiles: 'allow'` on the whole build directory.** If a `.env`, `.git` or editor file was ever copied into the output, it becomes downloadable. The migration guide also points to an `npx codemod@latest @expressjs/static-dotfiles` codemod for the renamed options. To confirm the diagnosis rather than guess: 1. request the exact URL with a command-line client and note the 404 and the absence of the file's `ETag` and `Last-Modified` headers, which `express.static` would have sent had it served the file; 2. check whether any segment of the path below the static root starts with a dot; 3. check the installed Express major version, since the same code served the file under Express 4. ## Cache headers: what express.static sends By default `express.static` sends `ETag` (`etag: true`), `Last-Modified` (`lastModified: true`) and, because `cacheControl` is `true`, a `Cache-Control` header built from `maxAge` and `immutable`: - **`maxAge`** defaults to `0`, so files go out with `Cache-Control: public, max-age=0` and browsers revalidate each time; - `maxAge` is in **milliseconds**, or a string the `ms` package accepts such as `'1d'` or `'1y'`; the header is capped at one year, so `'1y'` ends up as `max-age=31536000`; - **`immutable`** defaults to `false`, and setting it only adds the directive; without a `maxAge` it buys nothing; - `cacheControl: false` drops the header entirely and ignores both options. What `immutable` and a long `max-age` mean to browsers and shared caches is HTTP-caching material; here the job is producing the right header. ## A layout for a built frontend A typical build has hashed bundles under `/assets` and an `index.html` that names them. The bundles never change under the same URL, so they can be cached for a year; `index.html` must be revalidated, or users keep an old page pointing at old bundles. ```js app.use('/.well-known', express.static(path.join(build, '.well-known'))); app.use( express.static(build, { maxAge: '1y', immutable: true, setHeaders(res, filePath) { if (filePath.endsWith('.html')) res.setHeader('Cache-Control', 'no-cache'); }, }), ); ``` `setHeaders(res, path, stat)` runs synchronously for each file just before it is sent, and a `Cache-Control` set there replaces the one built from `maxAge`. A separate mount for `/assets` with the long `maxAge` and a plain one for the rest achieves the same thing. ## Other options worth knowing - **`fallthrough`** (default `true`) calls `next()` for missing files; `false` sends errors, even 404s, to `next(err)`, which suits a mount that is strictly one directory. - **`index`** (default `'index.html'`) serves that file for a directory request; `false` disables it. - **`redirect`** (default `true`) adds a trailing slash for directories. - **`extensions`** (default `false`) can try fallbacks such as `['html']`, so `/about` serves `about.html`. - **`acceptRanges`** (default `true`) honours `Range` requests, which media players rely on. - `NODE_ENV` does not change any of these options; set them explicitly.
- Why not simply set dotfiles: 'allow' on the whole build directory?Because it applies to every dot-named file and directory under that root. If a build step ever copies a `.env`, a `.git` folder or an editor backup into the output, it becomes downloadable. Mounting `/.well-known` as its own root keeps the default `'ignore'` protecting everything else, and makes the exception visible in code review.
- What does fallthrough: false change for an express.static mount?With the default `true`, a missing file just calls `next()` so later middleware or routes can answer. With `false`, client errors, including a missing file, go to `next(err)`, so the error-handling middleware answers immediately and the middleware also replies to all methods. It suits a mount that is strictly one directory, such as `/assets`, where nothing else should handle the path.
- Express 5's dotfiles 'ignore' only hides files whose own name starts with a dot.
- express.static sends no Cache-Control header unless maxAge is set.
- immutable: true on its own makes browsers cache the file for a year.
- express.static's maxAge is given in seconds, like the max-age directive.
- dotfiles: 'allow' on the build root is the safe fix for .well-known.
Assumes Express 5 (serve-static 2): dotfiles now also applies to hidden directories in the path, so .well-known is no longer served by default as it was in Express 4.