-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix accessing headers in progressively enhanced form actions (#74196)
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
- Loading branch information
1 parent
2e46a18
commit 887d746
Showing
4 changed files
with
97 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default } from '../../node/form/page' | ||
|
||
export const runtime = 'edge' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use client' | ||
|
||
import { useActionState } from 'react' | ||
import { getCookie, getHeader, setCookie } from '../../actions' | ||
|
||
const getReferrer = getHeader.bind(null, 'referer') | ||
const setTestCookie = setCookie.bind(null, 'test', '42') | ||
const getTestCookie = getCookie.bind(null, 'test') | ||
|
||
export default function Page() { | ||
const [referer, getReferrerAction] = useActionState(getReferrer, null) | ||
|
||
const [cookie, getTestCookieAction] = useActionState<ReturnType< | ||
typeof getCookie | ||
> | null>(getTestCookie, null) | ||
|
||
return ( | ||
<> | ||
<form action={getReferrerAction}> | ||
<p id="referer">{referer}</p> | ||
<button id="get-referer">Get Referer</button> | ||
</form> | ||
<form action={getTestCookieAction}> | ||
<p id="cookie">{cookie?.value}</p> | ||
<button id="set-cookie" formAction={setTestCookie}> | ||
Set Cookie | ||
</button> | ||
<button id="get-cookie">Get Cookie</button> | ||
</form> | ||
</> | ||
) | ||
} |