-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Server action throws when accessing headers if invoked before hydration #73992
Comments
We have similiar issue, but it happens for pages with prefetch. ⨯ Error: `headers` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
at headers (app/actions.ts:11:16) I have a hard times trying to build an easy example for it. However it seems like ALS loosing context between prefetching |
Having the same issue (also occurs with cookies as they're assigning a Set-Cookie header). Here is a simple example that you can test in a fresh Next.js install. import { cookies } from "next/headers";
export async function setCookieAction() {
"use server";
// Works with JavaScript enabled (and loaded)
// Otherwise: Error: `cookies` was called outside a request scope.
const cookieStore = await cookies();
cookieStore.set("myCookie", "myValue");
}
export default function Page() {
return (
<form action={setCookieAction}>
<button type="submit">Set Cookie</button>
</form>
);
} |
When scoping the `requestStore` to dynamic renders in #72312, we missed the case when a server action is invoked before hydration is complete. This leads to an error when a server action tries to read headers, cookies, or anything else that requires the presence of the `requestStore`. This fixes that for both the Node.js and Edge runtimes. It also appears that progressively enhanced form actions did not work at all before in the Edge runtime due to a missing `await` of `decodeFormState`. fixes #73992
When scoping the `requestStore` to dynamic renders in #72312, we missed the case when a server action is invoked before hydration is complete. This leads to an error when a server action tries to read headers, cookies, or anything else that requires the presence of the `requestStore`. This fixes that for both the Node.js and Edge runtimes. It also appears that progressively enhanced form actions did not work at all before in the Edge runtime due to a missing `await` of `decodeFormState`. fixes #73992 closes NAR-55
Hey @unstubbable ! Thanks for attention for this issue! It still reproduces in our case, not all the time but all the time for full prefetching requests |
@OlegLustenko I don't understand how prefetching is related to this progressive enhancement issue with server actions. Can you elaborate? |
@unstubbable sure! Sorry for not following a proper issue format with reproducing code-base I recorded a short video explaining some cases, in our case it's pretty flaky bug that appearing on some pages but all the time with prefetching prefetching.bug.mp4If I can provide more information I'll be glad to! It also fine if we should open a new issue for this |
Yeah, so this doesn't seem to be related to server actions that are sent before hydration at all. Can you please try to reduce your case to a minimal reproduction and create a new issue with it? |
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/nice-rgb-xsqkxt
To Reproduce
Current vs. Expected behavior
I expect the
actionWithHeaders()
action to succeed just like theactionWithoutHeaders()
action. If for some reason that isn't possible, I expect a more accurate error message (headers
is, in fact, being called inside a request scope).Provide environment information
Operating System: Platform: linux Arch: x64 Version: #1 SMP PREEMPT_DYNAMIC Sun Aug 6 20:05:33 UTC 2023 Available memory (MB): 4102 Available CPU cores: 2 Binaries: Node: 20.9.0 npm: 9.8.1 Yarn: 1.22.19 pnpm: 8.10.2 Relevant Packages: next: 15.1.1-canary.6 // Latest available version is detected (15.1.1-canary.6). eslint-config-next: N/A react: 19.0.0 react-dom: 19.0.0 typescript: 5.3.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
No response
The text was updated successfully, but these errors were encountered: