Skip to content

Commit

Permalink
Ensure query is present with i18n and history navigation (#20441)
Browse files Browse the repository at this point in the history
This makes sure the query isn't dropped when doing a history navigation with i18n. Additional tests have been added to ensure this is working correctly as well. 

Fixes: #20212
  • Loading branch information
ijjk authored Dec 28, 2020
1 parent 556ab4f commit 6189fe9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
10 changes: 9 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,17 @@ export default class Router implements BaseRouter {
parsedAs.pathname,
this.locales
)

if (localePathResult.detectedLocale) {
this.locale = localePathResult.detectedLocale
url = addBasePath(localePathResult.pathname)
parsedAs.pathname = addBasePath(parsedAs.pathname)
as = formatWithValidation(parsedAs)
url = addBasePath(
normalizeLocalePath(
hasBasePath(url) ? delBasePath(url) : url,
this.locales
).pathname
)
}

// if the locale isn't configured hard navigate to show 404 page
Expand Down
8 changes: 7 additions & 1 deletion test/integration/i18n-support-base-path/pages/gssp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export default function Page(props) {
)
}

export const getServerSideProps = ({ locale, locales, defaultLocale }) => {
export const getServerSideProps = ({
locale,
locales,
defaultLocale,
query,
}) => {
return {
props: {
query,
locale,
locales,
defaultLocale,
Expand Down
8 changes: 7 additions & 1 deletion test/integration/i18n-support/pages/gssp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ export default function Page(props) {
)
}

export const getServerSideProps = ({ locale, locales, defaultLocale }) => {
export const getServerSideProps = ({
locale,
locales,
defaultLocale,
query,
}) => {
return {
props: {
query,
locale,
locales,
defaultLocale,
Expand Down
46 changes: 45 additions & 1 deletion test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,46 @@ async function addDefaultLocaleCookie(browser) {
}

export function runTests(ctx) {
it('should navigate through history with query correctly', async () => {
const browser = await webdriver(ctx.appPort, `${ctx.basePath || '/'}`)

await browser.eval(`(function() {
window.beforeNav = 1
window.next.router.push(
window.next.router.pathname,
window.next.router.asPath,
{ locale: 'nl' }
)
window.next.router.push(
'/gssp?page=1'
)
})()`)

await browser.waitForElementByCss('#gssp')

const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props).toEqual({
locale: 'nl',
locales,
defaultLocale: 'en-US',
query: { page: '1' },
})

await browser
.back()
.waitForElementByCss('#index')
.forward()
.waitForElementByCss('#gssp')

const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual({
locale: 'nl',
locales,
defaultLocale: 'en-US',
query: { page: '1' },
})
})

if (!ctx.isDev) {
it('should not contain backslashes in pages-manifest', async () => {
const pagesManifestContent = await fs.readFile(
Expand Down Expand Up @@ -1310,7 +1350,7 @@ export function runTests(ctx) {
}
})

it('should provide correctly defaultLocale for locale domain', async () => {
it('should provide defaultLocale correctly for locale domain', async () => {
for (const { host, locale } of [
{ host: 'example.fr', locale: 'fr' },
{ host: 'example.be', locale: 'nl-BE' },
Expand All @@ -1336,6 +1376,7 @@ export function runTests(ctx) {
defaultLocale: locale,
locale,
locales,
query: {},
})
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
}
Expand Down Expand Up @@ -1954,6 +1995,7 @@ export function runTests(ctx) {
expect(JSON.parse($('#props').text())).toEqual({
locale: 'en-US',
locales,
query: {},
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
Expand Down Expand Up @@ -2165,6 +2207,7 @@ export function runTests(ctx) {
expect(JSON.parse($('#props').text())).toEqual({
locale: 'en-US',
locales,
query: {},
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
Expand All @@ -2178,6 +2221,7 @@ export function runTests(ctx) {
expect(JSON.parse($2('#props').text())).toEqual({
locale: 'nl-NL',
locales,
query: {},
defaultLocale: 'en-US',
})
expect($2('#router-locale').text()).toBe('nl-NL')
Expand Down

0 comments on commit 6189fe9

Please sign in to comment.