Use push for Server Action redirections (#54458)

Closes #53911. When calling `redirect()` instead a Server Action, the previous route should exist in the history when it's handled by the framework.
This commit is contained in:
Shu Ding 2023-08-23 21:47:46 +02:00 committed by GitHub
parent 0621171f0b
commit 758b9d251a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View file

@ -185,6 +185,13 @@ export function serverActionReducer(
action.mutable.inFlightServerAction! action.mutable.inFlightServerAction!
) as Awaited<FetchServerActionResult> ) as Awaited<FetchServerActionResult>
// Make sure the redirection is a push instead of a replace.
// Issue: https://github.com/vercel/next.js/issues/53911
if (redirectLocation) {
state.pushRef.pendingPush = true
mutable.pendingPush = true
}
mutable.previousTree = state.tree mutable.previousTree = state.tree
if (!flightData) { if (!flightData) {

View file

@ -74,6 +74,24 @@ createNextDescribe(
}, 'same') }, 'same')
}) })
it('should push new route when redirecting', async () => {
const browser = await next.browser('/header')
await browser.elementByCss('#setCookieAndRedirect').click()
await check(async () => {
return (await browser.elementByCss('#redirected').text()) || ''
}, 'redirected')
// Ensure we can navigate back
await browser.back()
await check(async () => {
return (
(await browser.elementByCss('#setCookieAndRedirect').text()) || ''
)
}, 'setCookieAndRedirect')
})
it('should support headers in client imported actions', async () => { it('should support headers in client imported actions', async () => {
const logs: string[] = [] const logs: string[] = []
next.on('stdout', (log) => { next.on('stdout', (log) => {

View file

@ -67,6 +67,7 @@ export default function UI({
</button> </button>
<form> <form>
<button <button
id="setCookieAndRedirect"
formAction={async () => { formAction={async () => {
await setCookieAndRedirect( await setCookieAndRedirect(
'redirect', 'redirect',