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

Ensure href resolvedAs is correct with locale #20080

Merged
merged 2 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
markAssetError,
} from '../../../client/route-loader'
import { denormalizePagePath } from '../../server/denormalize-page-path'
import { normalizeLocalePath } from '../i18n/normalize-locale-path'
import mitt, { MittEmitter } from '../mitt'
import {
AppContextType,
Expand Down Expand Up @@ -625,10 +626,6 @@ export default class Router implements BaseRouter {
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 localePathResult = normalizeLocalePath(
parsedAs.pathname,
Expand Down Expand Up @@ -751,7 +748,12 @@ export default class Router implements BaseRouter {
if (resolvedAs !== as) {
const potentialHref = removePathTrailingSlash(
this._resolveHref(
Object.assign({}, parsed, { pathname: resolvedAs }),
Object.assign({}, parsed, {
pathname: normalizeLocalePath(
hasBasePath(resolvedAs) ? delBasePath(resolvedAs) : resolvedAs,
this.locales
).pathname,
}),
pages,
false
).pathname!
Expand Down Expand Up @@ -1224,9 +1226,6 @@ export default class Router implements BaseRouter {
let { pathname } = parsed

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) {
pathname = normalizeLocalePath!(pathname, this.locales).pathname
parsed.pathname = pathname
Expand Down
11 changes: 7 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,14 @@ export default class Server {
protected getDynamicRoutes(): Array<DynamicRouteItem> {
const addedPages = new Set<string>()

return getSortedRoutes(Object.keys(this.pagesManifest!))
.filter(isDynamicRoute)
return getSortedRoutes(
Object.keys(this.pagesManifest!).map(
(page) =>
normalizeLocalePath(page, this.nextConfig.i18n?.locales).pathname
)
)
.map((page) => {
page = normalizeLocalePath(page, this.nextConfig.i18n?.locales).pathname
if (addedPages.has(page)) return null
if (addedPages.has(page) || !isDynamicRoute(page)) return null
addedPages.add(page)
return {
page,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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>
</>
)
}
12 changes: 12 additions & 0 deletions test/integration/i18n-support-base-path/pages/[post]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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>
</>
)
}
12 changes: 12 additions & 0 deletions test/integration/i18n-support/pages/[post]/[comment].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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>
</>
)
}
12 changes: 12 additions & 0 deletions test/integration/i18n-support/pages/[post]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 changes: 25 additions & 2 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ async function addDefaultLocaleCookie(browser) {
}

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 () => {
const browser = await webdriver(
ctx.appPort,
Expand Down Expand Up @@ -445,7 +468,7 @@ export function runTests(ctx) {
}
)

expect(res.status).toBe(shouldRedirect ? 307 : 404)
expect(res.status).toBe(shouldRedirect ? 307 : 200)

if (shouldRedirect) {
const parsed = url.parse(res.headers.get('location'), true)
Expand Down Expand Up @@ -475,7 +498,7 @@ export function runTests(ctx) {
redirect: 'manual',
}
)
expect(res.status).toBe(404)
expect(res.status).toBe(200)
expect(res.headers.get('x-hello')).toBe(shouldAdd ? 'world' : null)
}
})
Expand Down