未验证 提交 358861d3 编写于 作者: J JJ Kasper 提交者: GitHub

Ensure trailingSlash redirect applies correctly for i18n (#19859)

This ensures locales aren't applied for the trailing slash redirect un-necessarily

Fixes: https://github.com/vercel/next.js/issues/19784
上级 90e97b53
......@@ -466,12 +466,14 @@ export default async function loadCustomRoutes(
destination: '/:file',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect,
{
source: '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)',
destination: '/:notfile/',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect
)
if (config.basePath) {
......@@ -481,6 +483,7 @@ export default async function loadCustomRoutes(
permanent: true,
basePath: false,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect)
}
} else {
......@@ -489,6 +492,7 @@ export default async function loadCustomRoutes(
destination: '/:path+',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect)
if (config.basePath) {
redirects.unshift({
......@@ -497,6 +501,7 @@ export default async function loadCustomRoutes(
permanent: true,
basePath: false,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect)
}
}
......
......@@ -720,6 +720,7 @@ export default class Server {
const redirects = this.customRoutes.redirects.map((redirect) => {
const redirectRoute = getCustomRoute(redirect, 'redirect')
return {
internal: redirectRoute.internal,
type: redirectRoute.type,
match: redirectRoute.match,
statusCode: redirectRoute.statusCode,
......
......@@ -24,6 +24,7 @@ export type Route = {
statusCode?: number
name: string
requireBasePath?: false
internal?: true
fn: (
req: IncomingMessage,
res: ServerResponse,
......@@ -197,7 +198,11 @@ export default class Router {
const activeBasePath = keepBasePath ? this.basePath : ''
if (keepLocale) {
if (!localePathResult.detectedLocale && parsedUrl.query.__nextLocale) {
if (
!testRoute.internal &&
parsedUrl.query.__nextLocale &&
!localePathResult.detectedLocale
) {
currentPathname = `${activeBasePath}/${parsedUrl.query.__nextLocale}${
currentPathnameNoBasePath === '/' ? '' : currentPathnameNoBasePath
}`
......
......@@ -644,6 +644,7 @@ const runTests = (isDev = false) => {
),
source: '/:path+/',
statusCode: 308,
internal: true,
},
{
destination: '/:lang/about',
......
......@@ -406,6 +406,26 @@ export function runTests(ctx) {
}
})
it('should apply trailingSlash redirect correctly', async () => {
for (const [testPath, path, hostname, query] of [
['/first/', '/first', 'localhost', {}],
['/en/', '/en', 'localhost', {}],
['/en/another/', '/en/another', 'localhost', {}],
['/fr/', '/fr', 'localhost', {}],
['/fr/another/', '/fr/another', 'localhost', {}],
]) {
const res = await fetchViaHTTP(ctx.appPort, testPath, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(308)
const parsed = url.parse(res.headers.get('location'), true)
expect(parsed.pathname).toBe(path)
expect(parsed.hostname).toBe(hostname)
expect(parsed.query).toEqual(query)
}
})
it('should apply redirects correctly', async () => {
for (const [path, shouldRedirect, locale] of [
['/en-US/redirect-1', true],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册