Skip to content

Commit 854a849

Browse files
authored
Fix a couple i18n cases (#17805)
While working on #17755 noticed a couple of cases that needed fixing and broke them out to this PR to make that one easier to review. One fix is for `ssgCacheKey` where it wasn't having the `locale` prefix stripped correctly due to the locales no longer being populated under the server instances `renderOpts` and the second fix is for the `asPath` not being set to `/` when the `locale` is the only part in the URL e.g. `/en` became an empty string `""` x-ref: #17370
1 parent 1eeac4f commit 854a849

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

packages/next/client/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ if (process.env.__NEXT_i18n_SUPPORT) {
9292
const localePathResult = normalizeLocalePath(asPath, locales)
9393

9494
if (localePathResult.detectedLocale) {
95-
asPath = asPath.substr(localePathResult.detectedLocale.length + 1)
95+
asPath = asPath.substr(localePathResult.detectedLocale.length + 1) || '/'
9696
} else {
9797
// derive the default locale if it wasn't detected in the asPath
9898
// since we don't prerender static pages with all possible default

packages/next/next-server/server/next-server.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,13 @@ export default class Server {
11501150
const isDataReq = !!query._nextDataReq && (isSSG || isServerProps)
11511151
delete query._nextDataReq
11521152

1153+
const locale = query.__nextLocale as string
1154+
const locales = query.__nextLocales as string[]
1155+
// const defaultLocale = query.__nextDefaultLocale as string
1156+
delete query.__nextLocale
1157+
delete query.__nextLocales
1158+
// delete query.__nextDefaultLocale
1159+
11531160
let previewData: string | false | object | undefined
11541161
let isPreviewMode = false
11551162

@@ -1178,7 +1185,7 @@ export default class Server {
11781185
}
11791186

11801187
if (this.nextConfig.experimental.i18n) {
1181-
return normalizeLocalePath(path, this.renderOpts.locales).pathname
1188+
return normalizeLocalePath(path, locales).pathname
11821189
}
11831190
return path
11841191
}
@@ -1190,13 +1197,6 @@ export default class Server {
11901197
urlPathname = stripNextDataPath(urlPathname)
11911198
}
11921199

1193-
const locale = query.__nextLocale as string
1194-
const locales = query.__nextLocales as string[]
1195-
// const defaultLocale = query.__nextDefaultLocale as string
1196-
delete query.__nextLocale
1197-
delete query.__nextLocales
1198-
// delete query.__nextDefaultLocale
1199-
12001200
const ssgCacheKey =
12011201
isPreviewMode || !isSSG
12021202
? undefined // Preview mode bypasses the cache

test/integration/i18n-support/test/index.test.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,46 @@ let appPort
2626

2727
const locales = ['en-US', 'nl-NL', 'nl-BE', 'nl', 'fr-BE', 'fr', 'en']
2828

29-
function runTests() {
29+
function runTests(isDev) {
30+
it('should update asPath on the client correctly', async () => {
31+
for (const check of ['en', 'En']) {
32+
const browser = await webdriver(appPort, `/${check}`)
33+
34+
expect(await browser.elementByCss('html').getAttribute('lang')).toBe('en')
35+
expect(await browser.elementByCss('#router-locale').text()).toBe('en')
36+
expect(
37+
JSON.parse(await browser.elementByCss('#router-locales').text())
38+
).toEqual(locales)
39+
expect(await browser.elementByCss('#router-as-path').text()).toBe('/')
40+
expect(await browser.elementByCss('#router-pathname').text()).toBe('/')
41+
}
42+
})
43+
44+
if (!isDev) {
45+
it('should handle fallback correctly after generating', async () => {
46+
const browser = await webdriver(
47+
appPort,
48+
'/en/gsp/fallback/hello-fallback'
49+
)
50+
51+
// wait for the fallback to be generated/stored to ISR cache
52+
browser.waitForElementByCss('#gsp')
53+
54+
// now make sure we're serving the previously generated file from the cache
55+
const html = await renderViaHTTP(
56+
appPort,
57+
'/en/gsp/fallback/hello-fallback'
58+
)
59+
const $ = cheerio.load(html)
60+
61+
expect($('#gsp').text()).toBe('gsp page')
62+
expect($('#router-locale').text()).toBe('en')
63+
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
64+
expect($('#router-pathname').text()).toBe('/gsp/fallback/[slug]')
65+
expect($('#router-as-path').text()).toBe('/gsp/fallback/hello-fallback')
66+
})
67+
}
68+
3069
it('should use correct default locale for locale domains', async () => {
3170
const res = await fetchViaHTTP(appPort, '/', undefined, {
3271
headers: {
@@ -729,7 +768,7 @@ describe('i18n Support', () => {
729768
})
730769
afterAll(() => killApp(app))
731770

732-
runTests()
771+
runTests(true)
733772
})
734773

735774
describe('production mode', () => {

0 commit comments

Comments
 (0)