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

Handle basePath for redirect() #54277

Merged
merged 7 commits into from
Aug 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async function runOperation(renderData: RenderData) {
} = {
// TODO: give an actual buildId when next build is supported
buildId: 'development',
basePath: '',
params: renderData.params,
supportsDynamicHTML: true,
dev: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function render(request: NextRequest, event: NextFetchEvent) {
supportsDynamicHTML: true,
dev: true,
buildId: 'development',
basePath: '',
buildManifest: {
polyfillFiles: [],
rootMainFiles: BOOTSTRAP.filter((path) => path.endsWith('.js')),
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { warn } from '../../build/output/log'
import { appendMutableCookies } from '../web/spec-extension/adapters/request-cookies'
import { createServerInsertedHTML } from './server-inserted-html'
import { getRequiredScripts } from './required-scripts'
import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix'

export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug]
Expand Down Expand Up @@ -1576,7 +1577,11 @@ export async function renderToHTMLOrFlight(
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
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/app-render/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export type RenderOptsPartial = {
err?: Error | null
dev?: boolean
buildId: string
basePath: string
clientReferenceManifest?: ClientReferenceManifest
supportsDynamicHTML: boolean
runtime?: ServerRuntime
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-basepath/app/redirect/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { redirect } from 'next/navigation'

export default function Page() {
redirect('/another')
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/app-basepath/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'

createNextDescribe(
'app dir basepath',
Expand Down Expand Up @@ -34,5 +35,13 @@ createNextDescribe(

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')
})
}
)