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:
parent
0621171f0b
commit
758b9d251a
3 changed files with 26 additions and 0 deletions
|
@ -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) {
|
||||||
|
|
|
@ -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) => {
|
||||||
|
|
|
@ -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',
|
||||||
|
|
Loading…
Reference in a new issue