Commit 412f9739 authored by Arunoda Susiripala's avatar Arunoda Susiripala Committed by Tim Neutkens

Reload the page if asked to change the current URL (#1159)

* Add support to reload the page when ask to change the same url.

* Do not run change() in the initial page load.

* Add integration tests.

* Add self-reload.js
parent b2360ec5
...@@ -35,7 +35,7 @@ export default class Router extends EventEmitter { ...@@ -35,7 +35,7 @@ export default class Router extends EventEmitter {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// in order for `e.state` to work on the `onpopstate` event // in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization // we have to register the initial route upon initialization
this.replace(format({ pathname, query }), getURL()) this.changeState('replaceState', format({ pathname, query }), getURL())
window.addEventListener('popstate', this.onPopState) window.addEventListener('popstate', this.onPopState)
} }
...@@ -150,11 +150,12 @@ export default class Router extends EventEmitter { ...@@ -150,11 +150,12 @@ export default class Router extends EventEmitter {
this.abortComponentLoad() this.abortComponentLoad()
const { pathname, query } = parse(url, true) const { pathname, query } = parse(url, true)
// If asked to change the current URL we should reload the current page
// (not location.reload() but reload getInitalProps and other Next.js stuffs)
// We also need to set the method = replaceState always
// as this should not go into the history (That's how browsers work)
if (!this.urlIsNew(pathname, query)) { if (!this.urlIsNew(pathname, query)) {
this.emit('routeChangeStart', as) method = 'replaceState'
changeState()
this.emit('routeChangeComplete', as)
return true
} }
const route = toRoute(pathname) const route = toRoute(pathname)
...@@ -169,7 +170,7 @@ export default class Router extends EventEmitter { ...@@ -169,7 +170,7 @@ export default class Router extends EventEmitter {
return false return false
} }
changeState() this.changeState(method, url, as)
this.route = route this.route = route
this.set(pathname, query, { ...data, props }) this.set(pathname, query, { ...data, props })
...@@ -181,11 +182,11 @@ export default class Router extends EventEmitter { ...@@ -181,11 +182,11 @@ export default class Router extends EventEmitter {
this.emit('routeChangeComplete', as) this.emit('routeChangeComplete', as)
return true return true
}
function changeState () { changeState (method, url, as) {
if (method !== 'pushState' || getURL() !== as) { if (method !== 'pushState' || getURL() !== as) {
window.history[method]({ url, as }, null, as) window.history[method]({ url, as }, null, as)
}
} }
} }
......
...@@ -19,6 +19,7 @@ export default class extends Component { ...@@ -19,6 +19,7 @@ export default class extends Component {
<div className='nav-home'> <div className='nav-home'>
<Link href='/nav/about'><a id='about-link' style={linkStyle}>About</a></Link> <Link href='/nav/about'><a id='about-link' style={linkStyle}>About</a></Link>
<Link href='/empty-get-initial-props'><a id='empty-props' style={linkStyle}>Empty Props</a></Link> <Link href='/empty-get-initial-props'><a id='empty-props' style={linkStyle}>Empty Props</a></Link>
<Link href='/nav/self-reload'><a id='self-reload-link'>Self Reload</a></Link>
<p>This is the home.</p> <p>This is the home.</p>
<div id='counter'> <div id='counter'>
Counter: {counter} Counter: {counter}
......
import { Component } from 'react'
import Link from 'next/link'
let count = 0
export default class SelfReload extends Component {
static getInitialProps ({ res }) {
if (res) return { count: 0 }
count += 1
return { count }
}
render () {
return (
<div id='self-reload-page'>
<Link href='/nav/self-reload'>
<a id='self-reload-link'>Self Reload</a>
</Link>
<p>COUNT: {this.props.count}</p>
</div>
)
}
}
...@@ -83,5 +83,41 @@ export default (context) => { ...@@ -83,5 +83,41 @@ export default (context) => {
await browser.close() await browser.close()
}) })
}) })
describe('with the current url', () => {
it('should reload the page', async () => {
const browser = await webdriver(context.appPort, '/nav/self-reload')
const defaultCount = await browser.elementByCss('p').text()
expect(defaultCount).toBe('COUNT: 0')
const countAfterClicked = await browser
.elementByCss('#self-reload-link').click()
.elementByCss('p').text()
expect(countAfterClicked).toBe('COUNT: 1')
await browser.close()
})
it('should always replace the state', async () => {
const browser = await webdriver(context.appPort, '/nav')
const countAfterClicked = await browser
.elementByCss('#self-reload-link').click()
.waitForElementByCss('#self-reload-page')
.elementByCss('#self-reload-link').click()
.elementByCss('#self-reload-link').click()
.elementByCss('p').text()
// counts (page change + two clicks)
expect(countAfterClicked).toBe('COUNT: 3')
// Since we replace the state, back button would simply go us back to /nav
await browser
.back()
.waitForElementByCss('.nav-home')
await browser.close()
})
})
}) })
} }
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