Skip to content

Commit

Permalink
Update default locale handling with locale: false
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Nov 20, 2020
1 parent 2eb3cb3 commit f0b1d6d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/next/next-server/lib/router/router.ts
Expand Up @@ -616,7 +616,10 @@ export default class Router implements BaseRouter {
let localeChange = options.locale !== this.locale

if (process.env.__NEXT_I18N_SUPPORT) {
this.locale = options.locale || this.locale
this.locale =
options.locale === false
? this.defaultLocale
: options.locale || this.locale

if (typeof options.locale === 'undefined') {
options.locale = this.locale
Expand All @@ -627,12 +630,10 @@ export default class Router implements BaseRouter {
} = 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,
this.locales
)

if (localePathResult.detectedLocale) {
this.locale = localePathResult.detectedLocale
url = addBasePath(localePathResult.pathname)
Expand Down Expand Up @@ -1228,7 +1229,7 @@ export default class Router implements BaseRouter {
this.locales
)
parsedAs.pathname = localePathResult.pathname
options.locale = localePathResult.detectedLocale || options.locale
options.locale = localePathResult.detectedLocale || this.defaultLocale
asPath = formatWithValidation(parsedAs)
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/integration/i18n-support-base-path/pages/locale-false.js
Expand Up @@ -42,6 +42,14 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a>
</Link>
<br />
<Link href={`/gssp/first`} locale={false}>
<a id="to-gssp-slug-default">to /gssp/first (default locale)</a>
</Link>
<br />
<Link href={`/gsp`} locale={false}>
<a id="to-gsp-default">to /gsp (default locale)</a>
</Link>
<br />
</>
)
}
Expand Down
8 changes: 8 additions & 0 deletions test/integration/i18n-support/pages/locale-false.js
Expand Up @@ -42,6 +42,14 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a>
</Link>
<br />
<Link href={`/gssp/first`} locale={false}>
<a id="to-gssp-slug-default">to /gssp/first (default locale)</a>
</Link>
<br />
<Link href={`/gsp`} locale={false}>
<a id="to-gsp-default">to /gsp (default locale)</a>
</Link>
<br />
</>
)
}
Expand Down
39 changes: 39 additions & 0 deletions test/integration/i18n-support/test/shared.js
Expand Up @@ -27,6 +27,44 @@ async function addDefaultLocaleCookie(browser) {
}

export function runTests(ctx) {
it('should use default locale when no locale is in href with locale false', async () => {
const browser = await webdriver(
ctx.appPort,
`${ctx.basePath}/nl/locale-false?nextLocale=fr`
)

await browser.eval('window.beforeNav = 1')

if (!ctx.isDev) {
await browser.eval(`(function() {
document.querySelector('#to-gssp-slug-default').scrollIntoView()
document.querySelector('#to-gsp-default').scrollIntoView()
})()`)

await check(async () => {
const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
hrefs.sort()

assert.deepEqual(
hrefs.map((href) =>
new URL(href).pathname
.replace(ctx.basePath, '')
.replace(/^\/_next\/data\/[^/]+/, '')
),
[
'/en-US/gsp.json',
'/fr/gsp.json',
'/fr/gsp/fallback/first.json',
'/fr/gsp/fallback/hello.json',
]
)
return 'yes'
}, 'yes')
}

expect(await browser.eval('window.beforeNav')).toBe(1)
})

if (ctx.isDev) {
it('should show error for redirect and notFound returned at same time', async () => {
const html = await renderViaHTTP(
Expand Down Expand Up @@ -715,6 +753,7 @@ export function runTests(ctx) {
.replace(/^\/_next\/data\/[^/]+/, '')
),
[
'/en-US/gsp.json',
'/fr/gsp.json',
'/fr/gsp/fallback/first.json',
'/fr/gsp/fallback/hello.json',
Expand Down

0 comments on commit f0b1d6d

Please sign in to comment.