Static routes on serverless workers

A sitemap on one of my sites returned an internal server error for months. The build passed every time. Nothing in the logs stood out. The search console quietly reported zero indexed pages for that host, and I read that as "too new" rather than "broken".

The cause was small. The sitemap route listed pages by reading a content directory with the filesystem API. On a normal server that is fine. On an edge worker the filesystem is an in-memory virtual one that does not contain the build's source tree, so the first real request found nothing to read and threw.

The fix

Mark the route as static so the framework runs it once at build, where the source tree exists, and ships the result as a file. In Next.js the route segment option dynamic = 'force-static' forces prerendering:

export const dynamic = 'force-static'

The sibling routes in the same app already did this. The sitemap was the one that had been written before the move to workers and never revisited.

The lesson

Limits

One incident, one framework, one hosting platform. The mechanism will differ on other runtimes, and I did not capture the original stack trace; the diagnosis rests on the code path and on the fix removing the error.

Related: Growth gates before growth work, where getting indexed is the second gate.

All notes