Unverified Commit b42f73aa authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix: server action redirects between multiple root layouts (#73063)

When handling a redirect through a server action, we pass in the current
href rather than the destination href to `handleExternalUrl`, which is
incorrect since that won't reflect the updated URL from a server
action's redirect response.

While looking into this, I noticed a handful of server action branches
that weren't properly rejecting or resolving the action promises. This
would cause the transitions to stall until they were fulfilled, so I
updated all of the existing branches where we early return to also
resolve the promise.

Fixes #72842
parent eb86d82e
...@@ -206,7 +206,6 @@ export function serverActionReducer( ...@@ -206,7 +206,6 @@ export function serverActionReducer(
): ReducerState { ): ReducerState {
const { resolve, reject } = action const { resolve, reject } = action
const mutable: ServerActionMutable = {} const mutable: ServerActionMutable = {}
const href = state.canonicalUrl
let currentTree = state.tree let currentTree = state.tree
...@@ -230,6 +229,8 @@ export function serverActionReducer( ...@@ -230,6 +229,8 @@ export function serverActionReducer(
isPrerender, isPrerender,
revalidatedParts, revalidatedParts,
}) => { }) => {
let redirectHref: string | undefined
// honor the redirect type instead of defaulting to push in case of server actions. // honor the redirect type instead of defaulting to push in case of server actions.
if (redirectLocation) { if (redirectLocation) {
if (redirectType === RedirectType.replace) { if (redirectType === RedirectType.replace) {
...@@ -239,6 +240,9 @@ export function serverActionReducer( ...@@ -239,6 +240,9 @@ export function serverActionReducer(
state.pushRef.pendingPush = true state.pushRef.pendingPush = true
mutable.pendingPush = true mutable.pendingPush = true
} }
redirectHref = createHrefFromUrl(redirectLocation, false)
mutable.canonicalUrl = redirectHref
} }
if (!flightData) { if (!flightData) {
...@@ -258,6 +262,8 @@ export function serverActionReducer( ...@@ -258,6 +262,8 @@ export function serverActionReducer(
if (typeof flightData === 'string') { if (typeof flightData === 'string') {
// Handle case when navigating to page in `pages` from `app` // Handle case when navigating to page in `pages` from `app`
resolve(actionResult)
return handleExternalUrl( return handleExternalUrl(
state, state,
mutable, mutable,
...@@ -282,6 +288,8 @@ export function serverActionReducer( ...@@ -282,6 +288,8 @@ export function serverActionReducer(
if (!isRootRender) { if (!isRootRender) {
// TODO-APP: handle this case better // TODO-APP: handle this case better
console.log('SERVER ACTION APPLY FAILED') console.log('SERVER ACTION APPLY FAILED')
resolve(actionResult)
return state return state
} }
...@@ -291,20 +299,22 @@ export function serverActionReducer( ...@@ -291,20 +299,22 @@ export function serverActionReducer(
[''], [''],
currentTree, currentTree,
treePatch, treePatch,
redirectLocation redirectHref ? redirectHref : state.canonicalUrl
? createHrefFromUrl(redirectLocation)
: state.canonicalUrl
) )
if (newTree === null) { if (newTree === null) {
resolve(actionResult)
return handleSegmentMismatch(state, action, treePatch) return handleSegmentMismatch(state, action, treePatch)
} }
if (isNavigatingToNewRootLayout(currentTree, newTree)) { if (isNavigatingToNewRootLayout(currentTree, newTree)) {
resolve(actionResult)
return handleExternalUrl( return handleExternalUrl(
state, state,
mutable, mutable,
href, redirectHref || state.canonicalUrl,
state.pushRef.pendingPush state.pushRef.pendingPush
) )
} }
...@@ -343,10 +353,7 @@ export function serverActionReducer( ...@@ -343,10 +353,7 @@ export function serverActionReducer(
currentTree = newTree currentTree = newTree
} }
if (redirectLocation) { if (redirectLocation && redirectHref) {
const newHref = createHrefFromUrl(redirectLocation, false)
mutable.canonicalUrl = newHref
// Because the RedirectBoundary will trigger a navigation, we need to seed the prefetch cache // Because the RedirectBoundary will trigger a navigation, we need to seed the prefetch cache
// with the FlightData that we got from the server action for the target page, so that it's // with the FlightData that we got from the server action for the target page, so that it's
// available when the page is navigated to and doesn't need to be re-fetched. // available when the page is navigated to and doesn't need to be re-fetched.
...@@ -381,7 +388,9 @@ export function serverActionReducer( ...@@ -381,7 +388,9 @@ export function serverActionReducer(
// a response with the correct status code. // a response with the correct status code.
reject( reject(
getRedirectError( getRedirectError(
hasBasePath(newHref) ? removeBasePath(newHref) : newHref, hasBasePath(redirectHref)
? removeBasePath(redirectHref)
: redirectHref,
redirectType || RedirectType.push redirectType || RedirectType.push
) )
) )
......
'use server'
import { redirect } from 'next/navigation'
export async function redirectAction() {
redirect('/result')
}
import React from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
import { redirect } from 'next/navigation'
export default async function Page() {
async function redirectAction() {
'use server'
redirect('/root-layout-b')
}
export default function Page() {
return ( return (
<Link href="../root-layout-b" id="link-to-b"> <>
To b <Link href="../root-layout-b" id="link-to-b">
</Link> To b
</Link>
<form action={redirectAction}>
<button id="action-redirect-to-b">Redirect to B</button>
</form>
</>
) )
} }
...@@ -224,4 +224,16 @@ describe('app-dir root layout', () => { ...@@ -224,4 +224,16 @@ describe('app-dir root layout', () => {
.waitForElementByCss('#root-b') .waitForElementByCss('#root-b')
expect(await browser.hasElementByCssSelector('#root-a')).toBeFalse() expect(await browser.hasElementByCssSelector('#root-a')).toBeFalse()
}) })
it('should correctly handle navigation between multiple root layouts when redirecting in a server action', async () => {
const browser = await next.browser('/root-layout-a')
await browser.waitForElementByCss('#action-redirect-to-b')
expect(await browser.hasElementByCssSelector('#root-b')).toBeFalse()
await browser
.elementById('action-redirect-to-b')
.click()
.waitForElementByCss('#root-b')
expect(await browser.hasElementByCssSelector('#root-a')).toBeFalse()
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment