Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirect under suspense boundary with basePath #62597

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ async function renderToHTMLOrFlightImpl(
polyfills,
renderServerInsertedHTML,
serverCapturedErrors: allCapturedErrors,
basePath: renderOpts.basePath,
})

const renderer = createStaticRenderer({
Expand Down Expand Up @@ -1281,6 +1282,7 @@ async function renderToHTMLOrFlightImpl(
polyfills,
renderServerInsertedHTML,
serverCapturedErrors: [],
basePath: renderOpts.basePath,
}),
serverInsertedHTMLToHead: true,
validateRootLayout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import {
import { renderToReadableStream } from 'react-dom/server.edge'
import { streamToString } from '../stream-utils/node-web-streams-helper'
import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'

export function makeGetServerInsertedHTML({
polyfills,
renderServerInsertedHTML,
serverCapturedErrors,
basePath,
}: {
polyfills: JSX.IntrinsicElements['script'][]
renderServerInsertedHTML: () => React.ReactNode
serverCapturedErrors: Error[]
basePath: string
}) {
let flushedErrorMetaTagsUntilIndex = 0
let hasUnflushedPolyfills = polyfills.length !== 0
Expand All @@ -37,7 +40,10 @@ export function makeGetServerInsertedHTML({
) : null
)
} else if (isRedirectError(error)) {
const redirectUrl = getURLFromRedirectError(error)
const redirectUrl = addPathPrefix(
huozhi marked this conversation as resolved.
Show resolved Hide resolved
getURLFromRedirectError(error),
basePath
)
const statusCode = getRedirectStatusCodeFromError(error)
const isPermanent =
statusCode === RedirectStatusCode.PermanentRedirect ? true : false
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/app-dir/app-basepath/app/dynamic/[id]/loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Have loading.js to trigger suspense boundary for /dynamic/[id] page
export default function Loading() {
return <div>loading</div>
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/app-basepath/app/dynamic/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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>
)
}
15 changes: 11 additions & 4 deletions test/e2e/app-dir/app-basepath/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
import { retry } from 'next-test-utils'

createNextDescribe(
'app dir - basepath',
Expand Down Expand Up @@ -42,10 +42,9 @@ createNextDescribe(

it('should prefix redirect() with basePath', async () => {
const browser = await next.browser('/base/redirect')
await check(async () => {
await retry(async () => {
expect(await browser.url()).toBe(`${next.url}/base/another`)
return 'success'
}, 'success')
})
})

it('should render usePathname without the basePath', async () => {
Expand All @@ -56,5 +55,13 @@ createNextDescribe(
})
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`)
})
})
}
)