Unverified Commit 5b89b1b6 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure href resolvedAs is correct with locale (#20080)

This makes sure the `locale` doesn't cause the wrong `href` to be resolved, this also ensures route specificity is correct when loading dynamic routes in production mode. 

Fixes: https://github.com/vercel/next.js/issues/19747
parent 748bcb99
...@@ -14,6 +14,7 @@ import { ...@@ -14,6 +14,7 @@ import {
markAssetError, markAssetError,
} from '../../../client/route-loader' } from '../../../client/route-loader'
import { denormalizePagePath } from '../../server/denormalize-page-path' import { denormalizePagePath } from '../../server/denormalize-page-path'
import { normalizeLocalePath } from '../i18n/normalize-locale-path'
import mitt, { MittEmitter } from '../mitt' import mitt, { MittEmitter } from '../mitt'
import { import {
AppContextType, AppContextType,
...@@ -625,10 +626,6 @@ export default class Router implements BaseRouter { ...@@ -625,10 +626,6 @@ export default class Router implements BaseRouter {
options.locale = this.locale options.locale = this.locale
} }
const {
normalizeLocalePath,
} = require('../i18n/normalize-locale-path') as typeof import('../i18n/normalize-locale-path')
const parsedAs = parseRelativeUrl(hasBasePath(as) ? delBasePath(as) : as) const parsedAs = parseRelativeUrl(hasBasePath(as) ? delBasePath(as) : as)
const localePathResult = normalizeLocalePath( const localePathResult = normalizeLocalePath(
parsedAs.pathname, parsedAs.pathname,
...@@ -751,7 +748,12 @@ export default class Router implements BaseRouter { ...@@ -751,7 +748,12 @@ export default class Router implements BaseRouter {
if (resolvedAs !== as) { if (resolvedAs !== as) {
const potentialHref = removePathTrailingSlash( const potentialHref = removePathTrailingSlash(
this._resolveHref( this._resolveHref(
Object.assign({}, parsed, { pathname: resolvedAs }), Object.assign({}, parsed, {
pathname: normalizeLocalePath(
hasBasePath(resolvedAs) ? delBasePath(resolvedAs) : resolvedAs,
this.locales
).pathname,
}),
pages, pages,
false false
).pathname! ).pathname!
...@@ -1224,9 +1226,6 @@ export default class Router implements BaseRouter { ...@@ -1224,9 +1226,6 @@ export default class Router implements BaseRouter {
let { pathname } = parsed let { pathname } = parsed
if (process.env.__NEXT_I18N_SUPPORT) { if (process.env.__NEXT_I18N_SUPPORT) {
const normalizeLocalePath = require('../i18n/normalize-locale-path')
.normalizeLocalePath as typeof import('../i18n/normalize-locale-path').normalizeLocalePath
if (options.locale === false) { if (options.locale === false) {
pathname = normalizeLocalePath!(pathname, this.locales).pathname pathname = normalizeLocalePath!(pathname, this.locales).pathname
parsed.pathname = pathname parsed.pathname = pathname
......
...@@ -1024,11 +1024,14 @@ export default class Server { ...@@ -1024,11 +1024,14 @@ export default class Server {
protected getDynamicRoutes(): Array<DynamicRouteItem> { protected getDynamicRoutes(): Array<DynamicRouteItem> {
const addedPages = new Set<string>() const addedPages = new Set<string>()
return getSortedRoutes(Object.keys(this.pagesManifest!)) return getSortedRoutes(
.filter(isDynamicRoute) Object.keys(this.pagesManifest!).map(
(page) =>
normalizeLocalePath(page, this.nextConfig.i18n?.locales).pathname
)
)
.map((page) => { .map((page) => {
page = normalizeLocalePath(page, this.nextConfig.i18n?.locales).pathname if (addedPages.has(page) || !isDynamicRoute(page)) return null
if (addedPages.has(page)) return null
addedPages.add(page) addedPages.add(page)
return { return {
page, page,
......
import { useRouter } from 'next/router'
export default function Post() {
const router = useRouter()
return (
<>
<p id="comment">comment: {router.query.comment}</p>
<p id="router">{JSON.stringify(router)}</p>
</>
)
}
import { useRouter } from 'next/router'
export default function Post() {
const router = useRouter()
return (
<>
<p id="post">post: {router.query.post}</p>
<p id="router">{JSON.stringify(router)}</p>
</>
)
}
import { useRouter } from 'next/router'
export default function Post() {
const router = useRouter()
return (
<>
<p id="comment">comment: {router.query.comment}</p>
<p id="router">{JSON.stringify(router)}</p>
</>
)
}
import { useRouter } from 'next/router'
export default function Post() {
const router = useRouter()
return (
<>
<p id="post">post: {router.query.post}</p>
<p id="router">{JSON.stringify(router)}</p>
</>
)
}
...@@ -27,6 +27,29 @@ async function addDefaultLocaleCookie(browser) { ...@@ -27,6 +27,29 @@ async function addDefaultLocaleCookie(browser) {
} }
export function runTests(ctx) { export function runTests(ctx) {
it('should resolve href correctly when dynamic route matches locale prefixed', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath}/nl`)
await browser.eval('window.beforeNav = 1')
await browser.eval(`(function() {
window.next.router.push('/post-1?a=b')
})()`)
await browser.waitForElementByCss('#post')
const router = JSON.parse(await browser.elementByCss('#router').text())
expect(router.query).toEqual({ post: 'post-1', a: 'b' })
expect(router.pathname).toBe('/[post]')
expect(router.asPath).toBe('/post-1?a=b')
expect(router.locale).toBe('nl')
await browser.back().waitForElementByCss('#index')
expect(await browser.elementByCss('#router-locale').text()).toBe('nl')
expect(await browser.eval('window.beforeNav')).toBe(1)
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
).toEqual({})
})
it('should use default locale when no locale is in href with locale false', async () => { it('should use default locale when no locale is in href with locale false', async () => {
const browser = await webdriver( const browser = await webdriver(
ctx.appPort, ctx.appPort,
...@@ -445,7 +468,7 @@ export function runTests(ctx) { ...@@ -445,7 +468,7 @@ export function runTests(ctx) {
} }
) )
expect(res.status).toBe(shouldRedirect ? 307 : 404) expect(res.status).toBe(shouldRedirect ? 307 : 200)
if (shouldRedirect) { if (shouldRedirect) {
const parsed = url.parse(res.headers.get('location'), true) const parsed = url.parse(res.headers.get('location'), true)
...@@ -475,7 +498,7 @@ export function runTests(ctx) { ...@@ -475,7 +498,7 @@ export function runTests(ctx) {
redirect: 'manual', redirect: 'manual',
} }
) )
expect(res.status).toBe(404) expect(res.status).toBe(200)
expect(res.headers.get('x-hello')).toBe(shouldAdd ? 'world' : null) expect(res.headers.get('x-hello')).toBe(shouldAdd ? 'world' : null)
} }
}) })
......
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