Fix hangs and crashes for 204 responses from route handlers and deduped fetches (#100159)
### What
`new Response(body, { status })` throws `Invalid response status code
204` for null body statuses (204, 205, 304) whenever `body` is non-null,
and that includes an empty buffer. A few places buffer a response with
`arrayBuffer()` and rebuild it, which turns a body-less response into an
empty buffer:
- **Route handler prerender with `cacheComponents`**
(`app-route/module.ts`). The throw happened inside a `.then` callback
whose rejection nothing handled, so the prerender promise never settled.
Any route handler returning 204, including the auto-implemented
`OPTIONS` handler, hung until the function timeout, and a GET returning
204 hung `next build`. We hit this on nextjs.org: every `OPTIONS
/feed.xml` logged an `unhandledRejection` and then a 60s `Vercel Runtime
Timeout Error`.
- **Serving a cached app route response**
(`build/templates/app-route.ts`), which rebuilt the response from the
stored body and returned a 500. This runs on Vercel too, right after
generation, so fixing only the prerender path would have turned the
timeouts into 500s.
- **Fetches deduped through `workStore.pendingRevalidates`**
(`patch-fetch.ts`). The origin response is buffered for any status, so
when it was a 204, every waiting caller after the first threw.
### How
- On the in-memory paths (the route handler prerender and both
`pendingRevalidates` producers), check `body === null` on the original
response and skip `arrayBuffer()` entirely, so nothing is read or
allocated. The route handler prerender also now uses
`.then().catch(reject)`, so a future throw there rejects instead of
hanging.
- Serving a cached route response checks the status with a new
`isNullBodyStatus` helper. The stored format (`.body` file,
`CachedRouteValue.body: Buffer`) stores a missing body as zero bytes, so
only the status can tell them apart. I left the format alone because
custom cache handlers and existing entries depend on it.
The fetch cache itself only stores 200 responses, so a fetch cache hit
and `createCachedPrerenderResponse` can't see a 204 and are unchanged.
### Tests
- `use-cache-route-handler-only`: a GET returning 204. Without the fix,
`next build` hangs on it and gets killed.
- `use-cache-route-handler-only`: auto-implemented `OPTIONS` on a `'use
cache'` route, deploy-only. `next start` answers non-GET/HEAD requests
to prerendered routes with a 405 before the handler runs, while Vercel
invokes the function, which is how production reached the prerender
path. Not run locally.
- `patch-fetch.test.ts`: two concurrent identical fetches during a
foreground revalidate, with the origin returning 204. Without the fix,
this throws.
<!-- NEXT_JS_LLM -->
Showing
Please
register
or
sign in
to comment