Commit 400b57cf authored by Jiachi Liu's avatar Jiachi Liu

Fix redirect under suspense boundary with basePath (#62597)

Fixes `redirect()` call under suspense boundary, redirect url should
include the `basePath` in the url.

`redirect()` under suspense boundaries will go through server html
insertion, which is being used every time by suspense boundary resolved,
new errors will triggered from SSR streaming. It was missing before.

Fixes NEXT-2615
Fixes #62407
parent 714020fb
...@@ -893,6 +893,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -893,6 +893,7 @@ async function renderToHTMLOrFlightImpl(
polyfills, polyfills,
renderServerInsertedHTML, renderServerInsertedHTML,
hasPostponed, hasPostponed,
basePath: renderOpts.basePath,
}) })
const renderer = createStaticRenderer({ const renderer = createStaticRenderer({
......
...@@ -8,15 +8,18 @@ import { ...@@ -8,15 +8,18 @@ import {
import { renderToReadableStream } from 'react-dom/server.edge' import { renderToReadableStream } from 'react-dom/server.edge'
import { streamToString } from '../stream-utils/node-web-streams-helper' import { streamToString } from '../stream-utils/node-web-streams-helper'
import { RedirectStatusCode } from '../../client/components/redirect-status-code' import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'
export function makeGetServerInsertedHTML({ export function makeGetServerInsertedHTML({
polyfills, polyfills,
renderServerInsertedHTML, renderServerInsertedHTML,
basePath,
hasPostponed, hasPostponed,
}: { }: {
polyfills: JSX.IntrinsicElements['script'][] polyfills: JSX.IntrinsicElements['script'][]
renderServerInsertedHTML: () => React.ReactNode renderServerInsertedHTML: () => React.ReactNode
hasPostponed: boolean hasPostponed: boolean
basePath: string
}) { }) {
let flushedErrorMetaTagsUntilIndex = 0 let flushedErrorMetaTagsUntilIndex = 0
// If the render had postponed, then we have already flushed the polyfills. // If the render had postponed, then we have already flushed the polyfills.
...@@ -38,7 +41,10 @@ export function makeGetServerInsertedHTML({ ...@@ -38,7 +41,10 @@ export function makeGetServerInsertedHTML({
) : null ) : null
) )
} else if (isRedirectError(error)) { } else if (isRedirectError(error)) {
const redirectUrl = getURLFromRedirectError(error) const redirectUrl = addPathPrefix(
getURLFromRedirectError(error),
basePath
)
const statusCode = getRedirectStatusCodeFromError(error) const statusCode = getRedirectStatusCodeFromError(error)
const isPermanent = const isPermanent =
statusCode === RedirectStatusCode.PermanentRedirect ? true : false statusCode === RedirectStatusCode.PermanentRedirect ? true : false
......
// Have loading.js to trigger suspense boundary for /dynamic/[id] page
export default function Loading() {
return <div>loading</div>
}
import { redirect } from 'next/navigation'
export default async function Page({ params: { id } }) {
if (id === 'source') redirect('/dynamic/dest')
return (
<div>
<p>{`id:${id}`}</p>
</div>
)
}
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils' import { retry } from 'next-test-utils'
createNextDescribe( createNextDescribe(
'app dir - basepath', 'app dir - basepath',
...@@ -42,10 +42,9 @@ createNextDescribe( ...@@ -42,10 +42,9 @@ createNextDescribe(
it('should prefix redirect() with basePath', async () => { it('should prefix redirect() with basePath', async () => {
const browser = await next.browser('/base/redirect') const browser = await next.browser('/base/redirect')
await check(async () => { await retry(async () => {
expect(await browser.url()).toBe(`${next.url}/base/another`) expect(await browser.url()).toBe(`${next.url}/base/another`)
return 'success' })
}, 'success')
}) })
it('should render usePathname without the basePath', async () => { it('should render usePathname without the basePath', async () => {
...@@ -56,5 +55,13 @@ createNextDescribe( ...@@ -56,5 +55,13 @@ createNextDescribe(
}) })
await Promise.all(validatorPromises) await Promise.all(validatorPromises)
}) })
it('should handle redirect in dynamic in suspense boundary routes with basePath', async () => {
const browser = await next.browser('/base/dynamic/source')
await retry(async () => {
expect(await browser.url()).toBe(`${next.url}/base/dynamic/dest`)
expect(await browser.elementByCss('p').text()).toBe(`id:dest`)
})
})
} }
) )
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