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 props not updating when changing the locale and keeping hash #26205

Merged
merged 3 commits into from Jun 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Expand Up @@ -933,7 +933,11 @@ export default class Router implements BaseRouter {
// WARNING: `_h` is an internal option for handing Next.js client-side
// hydration. Your app should _never_ use this property. It may change at
// any time without notice.
if (!(options as any)._h && this.onlyAHashChange(cleanedAs)) {
if (
!(options as any)._h &&
this.onlyAHashChange(cleanedAs) &&
!localeChange
) {
this.asPath = cleanedAs
Router.events.emit('hashChangeStart', as, routeProps)
// TODO: do we need the resolved href when only a hash change?
Expand Down
@@ -0,0 +1,6 @@
module.exports = {
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
},
}
@@ -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,
},
}
}
@@ -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,
}
}
@@ -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()
})
})