Unverified Commit e1737809 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update fallback locale for locale: false (#19322)

This updates the fallback locale for `locale: false` to be the `defaultLocale` instead of the currently active `locale` as it allows passing through URLs more seamlessly as a URL without the locale prefixed can be treated as the `defaultLocale` instead of having to worry if the locale matches the currently active locale. This also ensures `locale={false}` is tested in the i18n-support-catchall suite

Closes: https://github.com/vercel/next.js/issues/19048
parent c1702c23
...@@ -616,7 +616,10 @@ export default class Router implements BaseRouter { ...@@ -616,7 +616,10 @@ export default class Router implements BaseRouter {
let localeChange = options.locale !== this.locale let localeChange = options.locale !== this.locale
if (process.env.__NEXT_I18N_SUPPORT) { 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') { if (typeof options.locale === 'undefined') {
options.locale = this.locale options.locale = this.locale
...@@ -627,12 +630,10 @@ export default class Router implements BaseRouter { ...@@ -627,12 +630,10 @@ export default class Router implements BaseRouter {
} = require('../i18n/normalize-locale-path') as typeof import('../i18n/normalize-locale-path') } = 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,
this.locales this.locales
) )
if (localePathResult.detectedLocale) { if (localePathResult.detectedLocale) {
this.locale = localePathResult.detectedLocale this.locale = localePathResult.detectedLocale
url = addBasePath(localePathResult.pathname) url = addBasePath(localePathResult.pathname)
...@@ -1235,7 +1236,7 @@ export default class Router implements BaseRouter { ...@@ -1235,7 +1236,7 @@ export default class Router implements BaseRouter {
this.locales this.locales
) )
parsedAs.pathname = localePathResult.pathname parsedAs.pathname = localePathResult.pathname
options.locale = localePathResult.detectedLocale || options.locale options.locale = localePathResult.detectedLocale || this.defaultLocale
asPath = formatWithValidation(parsedAs) asPath = formatWithValidation(parsedAs)
} }
} }
......
...@@ -42,6 +42,14 @@ export default function Page(props) { ...@@ -42,6 +42,14 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a> <a id="to-gssp-slug">to /gssp/first</a>
</Link> </Link>
<br /> <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 />
</> </>
) )
} }
......
...@@ -35,12 +35,12 @@ export default function Page(props) { ...@@ -35,12 +35,12 @@ export default function Page(props) {
</Link> </Link>
<br /> <br />
<Link href="/" locale="fr"> <Link href="/fr" locale={false}>
<a id="to-fr-locale-index">to /fr</a> <a id="to-fr-locale-index">to /fr</a>
</Link> </Link>
<br /> <br />
<Link href="/another" locale="fr"> <Link href="/fr/another" locale={false}>
<a id="to-fr-locale-another">to /fr/another</a> <a id="to-fr-locale-another">to /fr/another</a>
</Link> </Link>
<br /> <br />
......
...@@ -42,6 +42,14 @@ export default function Page(props) { ...@@ -42,6 +42,14 @@ export default function Page(props) {
<a id="to-gssp-slug">to /gssp/first</a> <a id="to-gssp-slug">to /gssp/first</a>
</Link> </Link>
<br /> <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 />
</> </>
) )
} }
......
...@@ -27,56 +27,42 @@ async function addDefaultLocaleCookie(browser) { ...@@ -27,56 +27,42 @@ async function addDefaultLocaleCookie(browser) {
} }
export function runTests(ctx) { export function runTests(ctx) {
it('should trigger full navigation for unsupported locale', async () => { it('should use default locale when no locale is in href with locale false', async () => {
const browser = await webdriver(ctx.appPort, '/') const browser = await webdriver(
ctx.appPort,
`${ctx.basePath}/nl/locale-false?nextLocale=fr`
)
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
if (!ctx.isDev) {
await browser.eval(`(function() { await browser.eval(`(function() {
window.next.router.push('/auto-export', '/auto-export', { locale: 'no' }) document.querySelector('#to-gssp-slug-default').scrollIntoView()
document.querySelector('#to-gsp-default').scrollIntoView()
})()`) })()`)
await browser.waitForElementByCss('#not-found') await check(async () => {
expect(await browser.eval('window.beforeNav')).toBe(null) const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
hrefs.sort()
const props = JSON.parse(await browser.elementByCss('#props').text()) assert.deepEqual(
expect(props.is404).toBe(true) hrefs.map((href) =>
expect(props.locale).toBe('en-US') new URL(href).pathname
expect(props.locales).toEqual(locales) .replace(ctx.basePath, '')
expect(props.defaultLocale).toBe('en-US') .replace(/^\/_next\/data\/[^/]+/, '')
}) ),
[
it('should 404 for non configured locales direct visit', async () => { '/en-US/gsp.json',
for (const [pathname, gssp] of [ '/fr/gsp.json',
['/gsp', true], '/fr/gsp/fallback/first.json',
['/gssp', true], '/fr/gsp/fallback/hello.json',
['/auto-export'], ]
['/'],
['/gsp/fallback/first'],
]) {
const res = await fetchViaHTTP(
ctx.appPort,
`/no${pathname === '/' ? '' : pathname}`,
undefined,
{
redirect: 'manual',
}
) )
return 'yes'
expect(res.status).toBe(404) }, 'yes')
if (gssp) {
const res = await fetchViaHTTP(
ctx.appPort,
`/_next/data/${ctx.buildId}/no${pathname}.json`,
undefined,
{
redirect: 'manual',
} }
)
expect(res.status).toBe(404) expect(await browser.eval('window.beforeNav')).toBe(1)
}
}
}) })
if (ctx.isDev) { if (ctx.isDev) {
...@@ -802,6 +788,7 @@ export function runTests(ctx) { ...@@ -802,6 +788,7 @@ export function runTests(ctx) {
.replace(/^\/_next\/data\/[^/]+/, '') .replace(/^\/_next\/data\/[^/]+/, '')
), ),
[ [
'/en-US/gsp.json',
'/fr/gsp.json', '/fr/gsp.json',
'/fr/gsp/fallback/first.json', '/fr/gsp/fallback/first.json',
'/fr/gsp/fallback/hello.json', '/fr/gsp/fallback/hello.json',
......
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