Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Triggering delayed server actions after page navigation is broken #61918

Closed
mordechaim opened this issue Feb 11, 2024 · 1 comment · Fixed by #64227
Closed

Triggering delayed server actions after page navigation is broken #61918

mordechaim opened this issue Feb 11, 2024 · 1 comment · Fixed by #64227
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@mordechaim
Copy link

mordechaim commented Feb 11, 2024

Link to the code that reproduces this issue

https://github.com/mordechaim/delayed-server-actions

To Reproduce

  1. Launch next.js application on localhost:3000
  2. Click "Process"
  3. Before 5 seconds elapsed, navigate to /page
  4. See error in server-side console

Current vs. Expected behavior

A very common paradigm before submitting data to the server is to preprocess the data client-side. But if the user navigates away while the server action has not yet been triggered, the following error is emitted in the server console:

Error: Failed to find Server Action "fb98c4e0ec9d68dfd5eeec908daa57080221f1f9". This request might be from an older or newer deployment. Original error: Invariant: Couldn't find action module ID from module map.
    at t9 (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:39:1697)
    at C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:38:7364
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at t5 (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:38:6672)
    at rR (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:40:1540)
    at C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:40:4219
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at Object.wrap (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:36:15944)
    at C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:40:4107
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at Object.wrap (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:36:15459)
    at rj (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:40:4034)
    at rq.render (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\compiled\next-server\app-page.runtime.dev.js:40:8130)
    at doRender (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:1416:44)
    at cacheEntry.responseCache.get.routeKind (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:1577:34)
    at ResponseCache.get (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\response-cache\index.js:49:26)
    at DevServer.renderToResponseWithComponentsImpl (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:1485:53)
    at C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:994:121
    at NextTracerImpl.trace (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\lib\trace\tracer.js:104:20)
    at DevServer.renderToResponseWithComponents (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:994:41)
    at DevServer.renderPageComponent (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:1881:35)
    at async DevServer.renderToResponseImpl (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:1919:32)
    at async DevServer.pipeImpl (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:913:25)
    at async NextNodeServer.handleCatchallRenderRequest (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\next-server.js:270:17)
    at async DevServer.handleRequestImpl (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\base-server.js:809:17)
    at async C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\dev\next-dev-server.js:335:20
    at async Span.traceAsyncFn (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\trace\trace.js:151:20)
    at async DevServer.handleRequest (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\dev\next-dev-server.js:332:24)
    at async invokeRender (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\lib\router-server.js:136:21)
    at async handleRequest (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\lib\router-server.js:315:24)
    at async requestHandlerImpl (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\lib\router-server.js:339:13)
    at async Server.requestListener (C:\Users\morde\workspace\delayed-server-actions\node_modules\next\dist\server\lib\start-server.js:140:13)

On the browser Network tab I can clearly see the wrong endpoint being called. In the case of the reproduction above, the request URL is http://localhost:3000/page instead of http://localhost:3000.

I tried both importing the action inside a client component or importing in a server component as passing the action as props to a client component; the results are identical.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64      
  Version: Windows 10 Home
Binaries:
  Node: 20.5.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.1.1-canary.47 // Latest available version is detected (14.1.1-canary.47).
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

No response

NEXT-2489

@mordechaim mordechaim added the bug Issue was opened via the bug report template. label Feb 11, 2024
@feedthejim feedthejim added the linear: next Confirmed issue that is tracked by the Next.js team. label Feb 14, 2024
ztanner added a commit that referenced this issue Apr 9, 2024
### What
When submitting a server action on a page that doesn't import the action
handler, a "Failed to find server action" error is thrown, even if
there's a valid handler for it elsewhere.

### Why
Workers for a particular server action ID are keyed by their page
entrypoints, and the client router invokes the current page when
triggering a server action, since it assumes it's available on the
current page. If an action is invoked after the router has moved away
from a page that can handle the action, then the action wouldn't run and
an error would be thrown in the server console.

### How
We try to find a valid worker to forward the action to, if one exists.
Otherwise it'll fallback to the usual error handling. This also adds a
header to opt out of rendering the flight tree, as if the action calls a
`revalidate` API, then it'll return a React tree corresponding with the
wrong page.

Fixes #61918
Fixes #63915

Closes NEXT-2489
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants