This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix props not updating when changing the locale and keeping hash (ver…
…cel#26205) Currently, there is only a `hashChangeStart` and subsequent `hashChangeComplete` event and no props update (which would be used to get translations, etc.). Happy for any feedback fixes vercel#23467 ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [ ] Make sure the linting passes
- Loading branch information
1 parent
907ef21
commit b1c097e
Showing
5 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
test/integration/i18n-support-same-page-hash-change/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
i18n: { | ||
locales: ['en', 'fr'], | ||
defaultLocale: 'en', | ||
}, | ||
} |
27 changes: 27 additions & 0 deletions
27
test/integration/i18n-support-same-page-hash-change/pages/about.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
|
||
return ( | ||
<> | ||
<p id="props-locale">{props.locale}</p> | ||
<p id="router-locale">{router.locale}</p> | ||
<Link | ||
href={{ pathname: router.pathname, query: router.query, hash: '#hash' }} | ||
locale={router.locale === 'fr' ? 'en' : 'fr'} | ||
> | ||
<a id="change-locale">Change Locale</a> | ||
</Link> | ||
</> | ||
) | ||
} | ||
|
||
export const getStaticProps = async ({ locale }) => { | ||
return { | ||
props: { | ||
locale, | ||
}, | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
test/integration/i18n-support-same-page-hash-change/pages/posts/[...slug].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
|
||
return ( | ||
<> | ||
<p id="props-locale">{props.locale}</p> | ||
<p id="router-locale">{router.locale}</p> | ||
<Link | ||
href={{ pathname: router.pathname, query: router.query, hash: '#hash' }} | ||
locale={router.locale === 'fr' ? 'en' : 'fr'} | ||
> | ||
<a id="change-locale">Change Locale</a> | ||
</Link> | ||
</> | ||
) | ||
} | ||
|
||
export const getStaticProps = async ({ locale }) => { | ||
return { | ||
props: { | ||
locale, | ||
}, | ||
} | ||
} | ||
|
||
export const getStaticPaths = () => { | ||
return { | ||
paths: [ | ||
{ | ||
params: { slug: ['a'] }, | ||
locale: 'en', | ||
}, | ||
{ | ||
params: { slug: ['a'] }, | ||
locale: 'fr', | ||
}, | ||
], | ||
fallback: false, | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
test/integration/i18n-support-same-page-hash-change/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { | ||
launchApp, | ||
killApp, | ||
findPort, | ||
nextBuild, | ||
nextStart, | ||
check, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const appDir = join(__dirname, '..') | ||
let appPort | ||
let app | ||
|
||
const runTests = () => { | ||
it('should update props on locale change with same hash', async () => { | ||
const browser = await webdriver(appPort, `/about#hash`) | ||
|
||
await browser.elementByCss('#change-locale').click() | ||
|
||
await check(() => browser.eval('window.location.pathname'), '/fr/about') | ||
await check(() => browser.eval('window.location.hash'), '#hash') | ||
|
||
expect(await browser.elementByCss('#router-locale').text()).toBe('fr') | ||
expect(await browser.elementByCss('#props-locale').text()).toBe('fr') | ||
|
||
await browser.elementByCss('#change-locale').click() | ||
|
||
await check(() => browser.eval('window.location.pathname'), '/about') | ||
await check(() => browser.eval('window.location.hash'), '#hash') | ||
|
||
expect(await browser.elementByCss('#router-locale').text()).toBe('en') | ||
expect(await browser.elementByCss('#props-locale').text()).toBe('en') | ||
}) | ||
|
||
it('should update props on locale change with same hash (dynamic page)', async () => { | ||
const browser = await webdriver(appPort, `/posts/a#hash`) | ||
|
||
await browser.elementByCss('#change-locale').click() | ||
|
||
await check(() => browser.eval('window.location.pathname'), '/fr/posts/a') | ||
await check(() => browser.eval('window.location.hash'), '#hash') | ||
|
||
expect(await browser.elementByCss('#router-locale').text()).toBe('fr') | ||
expect(await browser.elementByCss('#props-locale').text()).toBe('fr') | ||
|
||
await browser.elementByCss('#change-locale').click() | ||
|
||
await check(() => browser.eval('window.location.pathname'), '/posts/a') | ||
await check(() => browser.eval('window.location.hash'), '#hash') | ||
|
||
expect(await browser.elementByCss('#router-locale').text()).toBe('en') | ||
expect(await browser.elementByCss('#props-locale').text()).toBe('en') | ||
}) | ||
} | ||
|
||
describe('Hash changes i18n', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
runTests(true) | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
runTests() | ||
}) | ||
}) |