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 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
4 changes: 3 additions & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -817,7 +817,7 @@ export default class Router implements BaseRouter {
this.isReady = true
}

let localeChange = options.locale !== this.locale
const prevLocale = this.locale

if (process.env.__NEXT_I18N_SUPPORT) {
this.locale =
Expand Down Expand Up @@ -927,6 +927,8 @@ export default class Router implements BaseRouter {
)
this._inFlightRoute = as

let localeChange = prevLocale !== this.locale

// If the url change is only related to a hash change
// We should not proceed. We should only change the state.

Expand Down
@@ -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
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