next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Server action returns 500 under next start once the page's prerendered entry is stale (background revalidation re-runs the action on the consumed body)

Open lguzai opened this issue 1 week ago • 2 comments

Link to the code that reproduces this issue

https://github.com/lguzai/next-action-stale-repro

To Reproduce

  1. npm install && npx playwright install chromium
  2. npm run build && npm start
  3. In another terminal: npm run repro (loads /, waits 6 s so the page's prerendered entry is stale, clicks a button that calls a server action once)

Current vs. Expected behavior

Current: the action runs once and the client gets its result, but the response status is 500, and the server logs:

action ran with "hello"
⨯ SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>) {
  digest: '2589251228'
}

repro.mjs prints { actionStatus: 500, result: 'saved hello' }. Clicking before the entry is stale answers 200.

Expected: 200, as when the entry is fresh.

Provide environment information

Operating System: macOS (Darwin 27.0.0, arm64)
Node: 24.13.1
next: 16.3.8 (same code in 16.4.6 and 16.5.0-canary.56)
react, react-dom: 19.2.8

Which area(s) are affected?

Server Actions, Partial Prerendering (PPR), cacheComponents

Which stage(s) are affected?

next start (self-hosted, not minimal mode)

Additional context

This is the bug reported in #99373, which was closed for a missing reproduction link; the repository above is a minimal reproduction.

The page uses cacheComponents: true and a 'use cache' read with revalidate: 5.

Cause, in dist/build/templates/app-page-runtime.js: when a server action reaches a stale entry, the RDC branch schedules responseCache.revalidate(..., (c) => responseGenerator({ ...c, forceStaticRender: true }), ...). That calls doRender, which still passes the request's isPossibleServerAction (true) into the render options. The background render then calls handleAction a second time, on a request body the first run already consumed. decodeReply("") throws, and the error sets 500 on the response the client is waiting for.

Patching doRender's render options (CommonJS and ESM templates) fixes it:

-                        isPossibleServerAction,
+                        isPossibleServerAction: forceStaticRender ? false : isPossibleServerAction,

With it, the action answers 200, runs once, and the background revalidation still regenerates the page (the rendered timestamp changes after the action). We run this as a pnpm patch until a release fixes it.

lguzai avatar Nov 19 '26 21:11 lguzai

I'd like to work on this. I'll look into the isPossibleServerAction handling in the background revalidation render and open a PR.

wook10 avatar Nov 19 '26 23:11 wook10

I'm no longer working on this, sorry for the noise.

wook10 avatar Nov 20 '26 00:11 wook10