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 hash change events not firing with i18n #26994

Merged
merged 5 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ export default class Router implements BaseRouter {
}

let localeChange = options.locale !== this.locale
let prevLocale = this.locale
ijjk marked this conversation as resolved.
Show resolved Hide resolved

if (process.env.__NEXT_I18N_SUPPORT) {
this.locale =
Expand Down Expand Up @@ -936,7 +937,7 @@ export default class Router implements BaseRouter {
if (
!(options as any)._h &&
this.onlyAHashChange(cleanedAs) &&
!localeChange
prevLocale === this.locale
ijjk marked this conversation as resolved.
Show resolved Hide resolved
) {
this.asPath = cleanedAs
Router.events.emit('hashChangeStart', as, routeProps)
Expand Down
23 changes: 23 additions & 0 deletions test/integration/i18n-support-same-page-hash-change/pages/about.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect } from 'react'

export default function Page(props) {
const router = useRouter()

useEffect(() => {
window.hashChangeStart = 'no'
window.hashChangeComplete = 'no'
const hashChangeStart = () => {
window.hashChangeStart = 'yes'
}
const hashChangeComplete = () => {
window.hashChangeComplete = 'yes'
}

router.events.on('hashChangeStart', hashChangeStart)
router.events.on('hashChangeComplete', hashChangeComplete)

return () => {
router.events.off('hashChangeStart', hashChangeStart)
router.events.off('hashChangeComplete', hashChangeComplete)
}
}, [router.events])

return (
<>
<p id="props-locale">{props.locale}</p>
Expand All @@ -14,6 +34,9 @@ export default function Page(props) {
>
<a id="change-locale">Change Locale</a>
</Link>
<Link href={{ hash: '#newhash' }}>
<a id="hash-change">Hash Change</a>
</Link>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ const runTests = () => {
expect(await browser.elementByCss('#router-locale').text()).toBe('en')
expect(await browser.elementByCss('#props-locale').text()).toBe('en')
})

it('should trigger hash change events', async () => {
const browser = await webdriver(appPort, `/about#hash`)

await check(() => browser.eval('window.location.hash'), '#hash')

await browser.elementByCss('#hash-change').click()

await check(() => browser.eval('window.hashChangeStart'), 'yes')
await check(() => browser.eval('window.hashChangeComplete'), 'yes')

await check(() => browser.eval('window.location.hash'), '#newhash')
})
}

describe('Hash changes i18n', () => {
Expand Down