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

Fix a couple i18n cases #17805

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ if (process.env.__NEXT_i18n_SUPPORT) {
const localePathResult = normalizeLocalePath(asPath, locales)

if (localePathResult.detectedLocale) {
asPath = asPath.substr(localePathResult.detectedLocale.length + 1)
asPath = asPath.substr(localePathResult.detectedLocale.length + 1) || '/'
} else {
// derive the default locale if it wasn't detected in the asPath
// since we don't prerender static pages with all possible default
Expand Down
16 changes: 8 additions & 8 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,13 @@ export default class Server {
const isDataReq = !!query._nextDataReq && (isSSG || isServerProps)
delete query._nextDataReq

const locale = query.__nextLocale as string
const locales = query.__nextLocales as string[]
// const defaultLocale = query.__nextDefaultLocale as string
delete query.__nextLocale
delete query.__nextLocales
// delete query.__nextDefaultLocale

let previewData: string | false | object | undefined
let isPreviewMode = false

Expand Down Expand Up @@ -1178,7 +1185,7 @@ export default class Server {
}

if (this.nextConfig.experimental.i18n) {
return normalizeLocalePath(path, this.renderOpts.locales).pathname
return normalizeLocalePath(path, locales).pathname
}
return path
}
Expand All @@ -1190,13 +1197,6 @@ export default class Server {
urlPathname = stripNextDataPath(urlPathname)
}

const locale = query.__nextLocale as string
const locales = query.__nextLocales as string[]
// const defaultLocale = query.__nextDefaultLocale as string
delete query.__nextLocale
delete query.__nextLocales
// delete query.__nextDefaultLocale

const ssgCacheKey =
isPreviewMode || !isSSG
? undefined // Preview mode bypasses the cache
Expand Down
43 changes: 41 additions & 2 deletions test/integration/i18n-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,46 @@ let appPort

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

function runTests() {
function runTests(isDev) {
it('should update asPath on the client correctly', async () => {
for (const check of ['en', 'En']) {
const browser = await webdriver(appPort, `/${check}`)

expect(await browser.elementByCss('html').getAttribute('lang')).toBe('en')
expect(await browser.elementByCss('#router-locale').text()).toBe('en')
expect(
JSON.parse(await browser.elementByCss('#router-locales').text())
).toEqual(locales)
expect(await browser.elementByCss('#router-as-path').text()).toBe('/')
expect(await browser.elementByCss('#router-pathname').text()).toBe('/')
}
})

if (!isDev) {
it('should handle fallback correctly after generating', async () => {
const browser = await webdriver(
appPort,
'/en/gsp/fallback/hello-fallback'
)

// wait for the fallback to be generated/stored to ISR cache
browser.waitForElementByCss('#gsp')

// now make sure we're serving the previously generated file from the cache
const html = await renderViaHTTP(
appPort,
'/en/gsp/fallback/hello-fallback'
)
const $ = cheerio.load(html)

expect($('#gsp').text()).toBe('gsp page')
expect($('#router-locale').text()).toBe('en')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
expect($('#router-pathname').text()).toBe('/gsp/fallback/[slug]')
expect($('#router-as-path').text()).toBe('/gsp/fallback/hello-fallback')
})
}

it('should use correct default locale for locale domains', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
headers: {
Expand Down Expand Up @@ -729,7 +768,7 @@ describe('i18n Support', () => {
})
afterAll(() => killApp(app))

runTests()
runTests(true)
})

describe('production mode', () => {
Expand Down