Unverified Commit 76504eba authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure custom routes handling with i18n (#18427)

This makes sure custom-routes function properly with i18n support. Since the `locale` is stripped from the `pathname` this re-adds when checking custom-routes to allow the locale to be matched against

Closes: https://github.com/vercel/next.js/issues/18349
parent 50d98962
......@@ -19,6 +19,10 @@ export default function prepareDestination(
port?: string
} & ReturnType<typeof parseRelativeUrl> = {} as any
// clone query so we don't modify the original
query = Object.assign({}, query)
delete query.__nextLocale
if (destination.startsWith('/')) {
parsedDestination = parseRelativeUrl(destination)
} else {
......
......@@ -177,6 +177,17 @@ export default class Router {
currentPathname = replaceBasePath(this.basePath, currentPathname!)
}
// re-add locale for custom-routes to allow matching against
if (
isCustomRoute &&
(req as any).__nextStrippedLocale &&
parsedUrl.query.__nextLocale
) {
currentPathname = `/${parsedUrl.query.__nextLocale}${
currentPathname === '/' ? '' : currentPathname
}`
}
const newParams = testRoute.match(currentPathname)
// Check if the match function matched
......
......@@ -20,4 +20,70 @@ module.exports = {
},
],
},
async redirects() {
return [
{
source: '/en-US/redirect',
destination: '/somewhere-else',
permanent: false,
},
{
source: '/nl/redirect',
destination: '/somewhere-else',
permanent: false,
},
{
source: '/redirect',
destination: '/somewhere-else',
permanent: false,
},
]
},
async rewrites() {
return [
{
source: '/en-US/rewrite',
destination: '/another',
},
{
source: '/nl/rewrite',
destination: '/another',
},
{
source: '/rewrite',
destination: '/another',
},
]
},
async headers() {
return [
{
source: '/en-US/add-header',
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
{
source: '/nl/add-header',
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
{
source: '/add-header',
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
]
},
}
......@@ -193,6 +193,61 @@ function runTests(isDev) {
})
}
it('should apply redirects correctly', async () => {
for (const path of ['/redirect', '/en-US/redirect', '/nl/redirect']) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(307)
const parsed = url.parse(res.headers.get('location'), true)
expect(parsed.pathname).toBe('/somewhere-else')
expect(parsed.query).toEqual({})
}
})
it('should apply headers correctly', async () => {
for (const path of ['/add-header', '/en-US/add-header', '/nl/add-header']) {
const res = await fetchViaHTTP(appPort, path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(404)
expect(res.headers.get('x-hello')).toBe('world')
}
})
it('should apply rewrites correctly', async () => {
const checks = [
{
locale: 'en-US',
path: '/rewrite',
},
{
locale: 'en-US',
path: '/en-US/rewrite',
},
{
locale: 'nl',
path: '/nl/rewrite',
},
]
for (const check of checks) {
const res = await fetchViaHTTP(appPort, check.path, undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('html').attr('lang')).toBe(check.locale)
expect($('#router-locale').text()).toBe(check.locale)
expect($('#router-pathname').text()).toBe('/another')
expect($('#router-as-path').text()).toBe('/rewrite')
}
})
it('should navigate with locale prop correctly', async () => {
const browser = await webdriver(appPort, '/links?nextLocale=fr')
await addDefaultLocaleCookie(browser)
......
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