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

Ensure hash on initial request is preserved in new router #38913

Merged
merged 3 commits into from Jul 22, 2022
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/client/components/app-router.client.tsx
Expand Up @@ -82,7 +82,11 @@ export default function AppRouter({
},
pushRef: { pendingPush: false, mpaNavigation: false },
focusRef: { focus: false },
canonicalUrl: initialCanonicalUrl,
canonicalUrl:
initialCanonicalUrl +
// Hash is read as the initial value for canonicalUrl in the browser
// This is safe to do as canonicalUrl can't be rendered, it's only used to control the history updates the useEffect further down.
(typeof window !== 'undefined' ? window.location.hash : ''),
})

useEffect(() => {
Expand Down
16 changes: 15 additions & 1 deletion test/e2e/app-dir/index.test.ts
@@ -1,6 +1,6 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import { fetchViaHTTP, renderViaHTTP, waitFor } from 'next-test-utils'
import path from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
Expand Down Expand Up @@ -243,6 +243,20 @@ describe('app dir', () => {
}
)

it('should handle hash in initial url', async () => {
const browser = await webdriver(next.url, '/dashboard#abc')

try {
// Check if hash is preserved
expect(await browser.eval('window.location.hash')).toBe('#abc')
await waitFor(1000)
// Check again to be sure as it might be timed different
expect(await browser.eval('window.location.hash')).toBe('#abc')
} finally {
await browser.close()
}
})

describe('<Link />', () => {
// TODO-APP: fix development test
it.skip('should hard push', async () => {
Expand Down