Skip to content

Commit

Permalink
Use push for Server Action redirections (#54458)
Browse files Browse the repository at this point in the history
Closes #53911. When calling `redirect()` instead a Server Action, the previous route should exist in the history when it's handled by the framework.
  • Loading branch information
shuding authored Aug 23, 2023
1 parent 0621171 commit 758b9d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ export function serverActionReducer(
action.mutable.inFlightServerAction!
) 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

if (!flightData) {
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ createNextDescribe(
}, '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 () => {
const logs: string[] = []
next.on('stdout', (log) => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/actions/app/header/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function UI({
</button>
<form>
<button
id="setCookieAndRedirect"
formAction={async () => {
await setCookieAndRedirect(
'redirect',
Expand Down

0 comments on commit 758b9d2

Please sign in to comment.