Unverified Commit f2a3fd9f authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Handle basePath for redirect() (#54277)

### What & Why
Add base path handling for the url in redirect error if the `basePath` is configured.

This is only break for server rendering case as the `basePath` is missing in the `Location` header. When running `next build`, everything is managed well with app router and base path on client side so it didn't break.

### How

Adding `basePath` if it's presented for `Location` header.
Update the `RenderOpts` type as basePath is already passed down in it, also update them for turbopack entries

Fixes #54163
Closes NEXT-1529
parent 311eea4c
...@@ -73,6 +73,7 @@ async function runOperation(renderData: RenderData) { ...@@ -73,6 +73,7 @@ async function runOperation(renderData: RenderData) {
} = { } = {
// TODO: give an actual buildId when next build is supported // TODO: give an actual buildId when next build is supported
buildId: 'development', buildId: 'development',
basePath: '',
params: renderData.params, params: renderData.params,
supportsDynamicHTML: true, supportsDynamicHTML: true,
dev: true, dev: true,
......
...@@ -29,6 +29,7 @@ async function render(request: NextRequest, event: NextFetchEvent) { ...@@ -29,6 +29,7 @@ async function render(request: NextRequest, event: NextFetchEvent) {
supportsDynamicHTML: true, supportsDynamicHTML: true,
dev: true, dev: true,
buildId: 'development', buildId: 'development',
basePath: '',
buildManifest: { buildManifest: {
polyfillFiles: [], polyfillFiles: [],
rootMainFiles: BOOTSTRAP.filter((path) => path.endsWith('.js')), rootMainFiles: BOOTSTRAP.filter((path) => path.endsWith('.js')),
......
...@@ -77,6 +77,7 @@ import { warn } from '../../build/output/log' ...@@ -77,6 +77,7 @@ import { warn } from '../../build/output/log'
import { appendMutableCookies } from '../web/spec-extension/adapters/request-cookies' import { appendMutableCookies } from '../web/spec-extension/adapters/request-cookies'
import { createServerInsertedHTML } from './server-inserted-html' import { createServerInsertedHTML } from './server-inserted-html'
import { getRequiredScripts } from './required-scripts' import { getRequiredScripts } from './required-scripts'
import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
...@@ -1576,7 +1577,11 @@ export async function renderToHTMLOrFlight( ...@@ -1576,7 +1577,11 @@ export async function renderToHTMLOrFlight(
res.setHeader('set-cookie', Array.from(headers.values())) res.setHeader('set-cookie', Array.from(headers.values()))
} }
} }
res.setHeader('Location', getURLFromRedirectError(err)) const redirectUrl = addPathPrefix(
getURLFromRedirectError(err),
renderOpts.basePath
)
res.setHeader('Location', redirectUrl)
} }
const is404 = res.statusCode === 404 const is404 = res.statusCode === 404
......
...@@ -124,6 +124,7 @@ export type RenderOptsPartial = { ...@@ -124,6 +124,7 @@ export type RenderOptsPartial = {
err?: Error | null err?: Error | null
dev?: boolean dev?: boolean
buildId: string buildId: string
basePath: string
clientReferenceManifest?: ClientReferenceManifest clientReferenceManifest?: ClientReferenceManifest
supportsDynamicHTML: boolean supportsDynamicHTML: boolean
runtime?: ServerRuntime runtime?: ServerRuntime
......
import { redirect } from 'next/navigation'
export default function Page() {
redirect('/another')
}
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe( createNextDescribe(
'app dir basepath', 'app dir basepath',
...@@ -34,5 +35,13 @@ createNextDescribe( ...@@ -34,5 +35,13 @@ createNextDescribe(
expect(ogImageHref).toContain('/base/another/opengraph-image.png') expect(ogImageHref).toContain('/base/another/opengraph-image.png')
}) })
it('should prefix redirect() with basePath', async () => {
const browser = await next.browser('/base/redirect')
await check(async () => {
expect(await browser.url()).toBe(`${next.url}/base/another`)
return 'success'
}, 'success')
})
} }
) )
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