Unverified Commit eb367db9 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure to handle // during hydration (#11788)

* Make sure to handle // during hydration

* Add test for navigating from // URL
parent ec26b1bb
...@@ -226,13 +226,17 @@ export default class Router implements BaseRouter { ...@@ -226,13 +226,17 @@ export default class Router implements BaseRouter {
this.isFallback = isFallback this.isFallback = isFallback
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// in order for `e.state` to work on the `onpopstate` event // make sure "as" doesn't start with double slashes or else it can
// we have to register the initial route upon initialization // throw an error as it's considered invalid
this.changeState( if (as.substr(0, 2) !== '//') {
'replaceState', // in order for `e.state` to work on the `onpopstate` event
formatWithValidation({ pathname, query }), // we have to register the initial route upon initialization
as this.changeState(
) 'replaceState',
formatWithValidation({ pathname, query }),
as
)
}
window.addEventListener('popstate', this.onPopState) window.addEventListener('popstate', this.onPopState)
} }
......
export default () => {
if (typeof window !== 'undefined') {
window.didHydrate = true
}
return 'oops thats a 404'
}
...@@ -3,24 +3,64 @@ ...@@ -3,24 +3,64 @@
import path from 'path' import path from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { nextBuild, nextStart, findPort, killApp } from 'next-test-utils' import {
nextBuild,
nextStart,
findPort,
killApp,
launchApp,
check,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..') const appDir = path.join(__dirname, '..')
let app let app
let appPort let appPort
describe('Hydration', () => { const runTests = () => {
beforeAll(async () => { it('hydrates correctly for normal page', async () => {
await fs.remove(path.join(appDir, '.next')) const browser = await webdriver(appPort, '/')
await nextBuild(appDir) expect(await browser.eval('window.didHydrate')).toBe(true)
appPort = await findPort()
app = await nextStart(appDir, appPort)
}) })
afterAll(() => killApp(app))
it('Hydrates correctly', async () => { it('hydrates correctly for //', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '//')
expect(await browser.eval('window.didHydrate')).toBe(true) expect(await browser.eval('window.didHydrate')).toBe(true)
}) })
it('should be able to navigate after loading //', async () => {
const browser = await webdriver(appPort, '//')
await browser.eval('window.beforeNav = true')
await browser.eval('window.next.router.push("/details")')
await check(
() => browser.eval('document.documentElement.innerHTML'),
/details/
)
expect(await browser.eval('window.beforeNav')).toBe(true)
})
}
describe('Hydration', () => {
describe('dev mode', () => {
beforeAll(async () => {
await fs.remove(path.join(appDir, '.next'))
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(path.join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment