2016-12-24 17:29:23 +00:00
|
|
|
import { parse, format } from 'url'
|
2017-02-15 19:29:42 +00:00
|
|
|
import { EventEmitter } from 'events'
|
2016-12-19 14:40:26 +00:00
|
|
|
import evalScript from '../eval-script'
|
|
|
|
import shallowEquals from '../shallow-equals'
|
2017-02-15 19:29:42 +00:00
|
|
|
import PQueue from '../p-queue'
|
2017-02-28 17:31:17 +00:00
|
|
|
import { loadGetInitialProps, getURL } from '../utils'
|
2017-02-20 23:48:17 +00:00
|
|
|
import { _notifyBuildIdMismatch } from './'
|
2017-02-26 03:41:17 +00:00
|
|
|
import fetch from 'unfetch'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-02-27 19:15:39 +00:00
|
|
|
if (typeof window !== 'undefined' && typeof navigator.serviceWorker !== 'undefined') {
|
|
|
|
navigator.serviceWorker.getRegistrations()
|
|
|
|
.then(registrations => {
|
|
|
|
registrations.forEach(registration => {
|
|
|
|
if (!registration.active) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (registration.active.scriptURL.indexOf('_next-prefetcher.js') === -1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
console.warn(`Unregistered deprecated 'next/prefetch' ServiceWorker. See https://github.com/zeit/next.js#prefetching-pages`)
|
|
|
|
registration.unregister()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
export default class Router extends EventEmitter {
|
2017-02-28 17:31:17 +00:00
|
|
|
constructor (pathname, query, as, { Component, ErrorComponent, err } = {}) {
|
2016-12-31 01:15:22 +00:00
|
|
|
super()
|
2016-10-08 05:12:51 +00:00
|
|
|
// represents the current component key
|
2016-12-16 20:33:08 +00:00
|
|
|
this.route = toRoute(pathname)
|
2016-10-05 23:52:50 +00:00
|
|
|
|
|
|
|
// set up the component cache (by route keys)
|
2017-01-12 01:58:20 +00:00
|
|
|
this.components = { [this.route]: { Component, err } }
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-02-15 19:29:42 +00:00
|
|
|
// contain a map of promise of fetch routes
|
|
|
|
this.fetchingRoutes = {}
|
|
|
|
|
|
|
|
this.prefetchQueue = new PQueue({ concurrency: 2 })
|
2016-11-24 14:03:16 +00:00
|
|
|
this.ErrorComponent = ErrorComponent
|
2016-12-16 20:33:08 +00:00
|
|
|
this.pathname = pathname
|
|
|
|
this.query = query
|
2017-02-28 17:31:17 +00:00
|
|
|
this.as = as
|
2016-10-08 05:12:51 +00:00
|
|
|
this.subscriptions = new Set()
|
|
|
|
this.componentLoadCancel = null
|
2016-10-05 23:52:50 +00:00
|
|
|
this.onPopState = this.onPopState.bind(this)
|
2016-10-08 05:12:51 +00:00
|
|
|
|
2016-10-17 00:00:17 +00:00
|
|
|
if (typeof window !== 'undefined') {
|
2016-12-24 17:29:23 +00:00
|
|
|
// in order for `e.state` to work on the `onpopstate` event
|
|
|
|
// we have to register the initial route upon initialization
|
2017-02-15 18:27:41 +00:00
|
|
|
this.changeState('replaceState', format({ pathname, query }), getURL())
|
2016-12-24 17:29:23 +00:00
|
|
|
|
2016-10-08 10:16:22 +00:00
|
|
|
window.addEventListener('popstate', this.onPopState)
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
async onPopState (e) {
|
2017-01-25 16:58:26 +00:00
|
|
|
if (!e.state) {
|
2017-02-05 08:58:44 +00:00
|
|
|
// We get state as undefined for two reasons.
|
|
|
|
// 1. With older safari (< 8) and older chrome (< 34)
|
|
|
|
// 2. When the URL changed with #
|
|
|
|
//
|
|
|
|
// In the both cases, we don't need to proceed and change the route.
|
|
|
|
// (as it's already changed)
|
|
|
|
// But we can simply replace the state with the new changes.
|
|
|
|
// Actually, for (1) we don't need to nothing. But it's hard to detect that event.
|
|
|
|
// So, doing the following for (1) does no harm.
|
|
|
|
const { pathname, query } = this
|
2017-02-28 17:31:17 +00:00
|
|
|
this.changeState('replaceState', format({ pathname, query }), getURL())
|
2017-01-25 16:58:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const { url, as } = e.state
|
2017-02-28 17:31:17 +00:00
|
|
|
this.replace(url, as)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 14:35:31 +00:00
|
|
|
update (route, Component) {
|
|
|
|
const data = this.components[route] || {}
|
|
|
|
const newData = { ...data, Component }
|
|
|
|
this.components[route] = newData
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
if (route === this.route) {
|
2016-10-17 14:35:31 +00:00
|
|
|
this.notify(newData)
|
2016-10-08 05:12:51 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 07:22:15 +00:00
|
|
|
async reload (route) {
|
|
|
|
delete this.components[route]
|
2017-02-15 19:29:42 +00:00
|
|
|
delete this.fetchingRoutes[route]
|
2016-10-24 07:22:15 +00:00
|
|
|
|
|
|
|
if (route !== this.route) return
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
const url = window.location.href
|
|
|
|
const { pathname, query } = parse(url, true)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeStart', url)
|
|
|
|
const {
|
|
|
|
data,
|
|
|
|
props,
|
|
|
|
error
|
2017-02-20 23:48:17 +00:00
|
|
|
} = await this.getRouteInfo(route, pathname, query, url)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error && error.cancelled) {
|
|
|
|
return
|
2016-10-24 07:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.notify({ ...data, props })
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error) {
|
|
|
|
this.emit('routeChangeError', error, url)
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeComplete', url)
|
2016-10-24 07:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
back () {
|
2016-10-17 00:00:17 +00:00
|
|
|
window.history.back()
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:27:52 +00:00
|
|
|
push (url, as = url) {
|
|
|
|
return this.change('pushState', url, as)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:27:52 +00:00
|
|
|
replace (url, as = url) {
|
|
|
|
return this.change('replaceState', url, as)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:27:52 +00:00
|
|
|
async change (method, url, as) {
|
2017-02-28 08:48:40 +00:00
|
|
|
this.abortComponentLoad(as)
|
2016-12-28 05:27:52 +00:00
|
|
|
const { pathname, query } = parse(url, true)
|
2016-12-30 19:55:00 +00:00
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
// If the url change is only related to a hash change
|
|
|
|
// We should not proceed. We should only replace the state.
|
|
|
|
if (this.onlyAHashChange(as)) {
|
|
|
|
this.changeState('replaceState', url, as)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:27:41 +00:00
|
|
|
// 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)
|
2016-12-30 19:55:00 +00:00
|
|
|
if (!this.urlIsNew(pathname, query)) {
|
2017-02-15 18:27:41 +00:00
|
|
|
method = 'replaceState'
|
2016-12-30 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 05:27:52 +00:00
|
|
|
const route = toRoute(pathname)
|
2016-10-08 05:12:51 +00:00
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeStart', as)
|
|
|
|
const {
|
|
|
|
data, props, error
|
2017-02-20 23:48:17 +00:00
|
|
|
} = await this.getRouteInfo(route, pathname, query, as)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error && error.cancelled) {
|
|
|
|
return false
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 18:27:41 +00:00
|
|
|
this.changeState(method, url, as)
|
2017-02-28 17:31:17 +00:00
|
|
|
const hash = window.location.hash.substring(1)
|
2016-11-03 10:15:03 +00:00
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
this.route = route
|
2017-02-28 17:31:17 +00:00
|
|
|
this.set(pathname, query, as, { ...data, props, hash })
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error) {
|
|
|
|
this.emit('routeChangeError', error, as)
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeComplete', as)
|
2016-10-08 05:12:51 +00:00
|
|
|
return true
|
2017-02-15 18:27:41 +00:00
|
|
|
}
|
2016-12-30 19:55:00 +00:00
|
|
|
|
2017-02-15 18:27:41 +00:00
|
|
|
changeState (method, url, as) {
|
|
|
|
if (method !== 'pushState' || getURL() !== as) {
|
|
|
|
window.history[method]({ url, as }, null, as)
|
2016-12-30 19:55:00 +00:00
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 23:48:17 +00:00
|
|
|
async getRouteInfo (route, pathname, query, as) {
|
2016-12-31 01:15:22 +00:00
|
|
|
const routeInfo = {}
|
|
|
|
|
|
|
|
try {
|
2017-02-20 23:48:17 +00:00
|
|
|
routeInfo.data = await this.fetchComponent(route, as)
|
|
|
|
if (!routeInfo.data) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const { Component, err, jsonPageRes } = routeInfo.data
|
2017-02-15 08:52:22 +00:00
|
|
|
const ctx = { err, pathname, query, jsonPageRes }
|
2017-01-12 01:58:20 +00:00
|
|
|
routeInfo.props = await this.getInitialProps(Component, ctx)
|
2016-12-31 01:15:22 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.cancelled) {
|
|
|
|
return { error: err }
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
const Component = this.ErrorComponent
|
|
|
|
routeInfo.data = { Component, err }
|
|
|
|
const ctx = { err, pathname, query }
|
|
|
|
routeInfo.props = await this.getInitialProps(Component, ctx)
|
2016-12-31 01:15:22 +00:00
|
|
|
|
|
|
|
routeInfo.error = err
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return routeInfo
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
set (pathname, query, as, data) {
|
2016-12-30 19:55:00 +00:00
|
|
|
this.pathname = pathname
|
|
|
|
this.query = query
|
2017-02-28 17:31:17 +00:00
|
|
|
this.as = as
|
2016-12-30 19:55:00 +00:00
|
|
|
this.notify(data)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
onlyAHashChange (as) {
|
|
|
|
if (!this.as) return false
|
|
|
|
const [ oldUrlNoHash ] = this.as.split('#')
|
|
|
|
const [ newUrlNoHash, newHash ] = as.split('#')
|
|
|
|
|
|
|
|
// If the urls are change, there's more than a hash change
|
|
|
|
if (oldUrlNoHash !== newUrlNoHash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no hash in the new url, we can't consider it as a hash change
|
|
|
|
if (!newHash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now there's a hash in the new URL.
|
|
|
|
// We don't need to worry about the old hash.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:27:52 +00:00
|
|
|
urlIsNew (pathname, query) {
|
2016-10-05 23:52:50 +00:00
|
|
|
return this.pathname !== pathname || !shallowEquals(query, this.query)
|
|
|
|
}
|
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
async prefetch (url) {
|
2017-02-26 19:45:16 +00:00
|
|
|
// We don't add support for prefetch in the development mode.
|
|
|
|
// If we do that, our on-demand-entries optimization won't performs better
|
|
|
|
if (process.env.NODE_ENV === 'development') return
|
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
const { pathname } = parse(url)
|
|
|
|
const route = toRoute(pathname)
|
2017-02-15 19:29:42 +00:00
|
|
|
return this.prefetchQueue.add(() => this.fetchRoute(route))
|
2017-02-15 08:52:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 23:48:17 +00:00
|
|
|
async fetchComponent (route, as) {
|
2016-10-08 05:12:51 +00:00
|
|
|
let data = this.components[route]
|
2017-02-15 08:52:22 +00:00
|
|
|
if (data) return data
|
|
|
|
|
|
|
|
let cancelled = false
|
|
|
|
const cancel = this.componentLoadCancel = function () {
|
|
|
|
cancelled = true
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:29:42 +00:00
|
|
|
const jsonPageRes = await this.fetchRoute(route)
|
2017-02-15 08:52:22 +00:00
|
|
|
const jsonData = await jsonPageRes.json()
|
2017-02-20 23:48:17 +00:00
|
|
|
|
|
|
|
if (jsonData.buildIdMismatch) {
|
|
|
|
_notifyBuildIdMismatch(as)
|
|
|
|
|
|
|
|
const error = Error('Abort due to BUILD_ID mismatch')
|
|
|
|
error.cancelled = true
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
const newData = {
|
|
|
|
...loadComponent(jsonData),
|
|
|
|
jsonPageRes
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
if (cancelled) {
|
|
|
|
const error = new Error(`Abort fetching component for route: "${route}"`)
|
|
|
|
error.cancelled = true
|
|
|
|
throw error
|
2016-10-08 05:12:51 +00:00
|
|
|
}
|
2017-02-15 08:52:22 +00:00
|
|
|
|
|
|
|
if (cancel === this.componentLoadCancel) {
|
|
|
|
this.componentLoadCancel = null
|
|
|
|
}
|
|
|
|
|
|
|
|
this.components[route] = newData
|
|
|
|
return newData
|
2016-10-08 05:12:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 09:25:38 +00:00
|
|
|
async getInitialProps (Component, ctx) {
|
2016-10-08 05:12:51 +00:00
|
|
|
let cancelled = false
|
|
|
|
const cancel = () => { cancelled = true }
|
2016-10-05 23:52:50 +00:00
|
|
|
this.componentLoadCancel = cancel
|
2016-10-08 05:12:51 +00:00
|
|
|
|
2017-01-20 19:33:46 +00:00
|
|
|
const props = await loadGetInitialProps(Component, ctx)
|
2016-10-08 05:12:51 +00:00
|
|
|
|
|
|
|
if (cancel === this.componentLoadCancel) {
|
|
|
|
this.componentLoadCancel = null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancelled) {
|
2016-12-31 01:15:22 +00:00
|
|
|
const err = new Error('Loading initial props cancelled')
|
2016-10-08 05:12:51 +00:00
|
|
|
err.cancelled = true
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
|
|
|
return props
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 04:12:32 +00:00
|
|
|
async fetchRoute (route) {
|
2017-02-15 19:29:42 +00:00
|
|
|
let promise = this.fetchingRoutes[route]
|
|
|
|
if (!promise) {
|
|
|
|
promise = this.fetchingRoutes[route] = this.doFetchRoute(route)
|
|
|
|
}
|
2017-02-17 04:12:32 +00:00
|
|
|
|
|
|
|
const res = await promise
|
|
|
|
// We need to clone this to prevent reading the body twice
|
|
|
|
// Becuase it's possible only once to read res.json() or a similar method.
|
|
|
|
return res.clone()
|
2017-02-15 19:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
doFetchRoute (route) {
|
|
|
|
const { buildId } = window.__NEXT_DATA__
|
|
|
|
const url = `/_next/${encodeURIComponent(buildId)}/pages${route}`
|
|
|
|
return fetch(url, {
|
2017-02-15 08:52:22 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: { 'Accept': 'application/json' }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-28 08:48:40 +00:00
|
|
|
abortComponentLoad (as) {
|
|
|
|
this.emit('routeChangeError', new Error('Route Cancelled'), as)
|
2016-10-05 23:52:50 +00:00
|
|
|
if (this.componentLoadCancel) {
|
|
|
|
this.componentLoadCancel()
|
|
|
|
this.componentLoadCancel = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
notify (data) {
|
|
|
|
this.subscriptions.forEach((fn) => fn(data))
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribe (fn) {
|
2016-10-08 05:12:51 +00:00
|
|
|
this.subscriptions.add(fn)
|
|
|
|
return () => this.subscriptions.delete(fn)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
function toRoute (path) {
|
2016-10-05 23:52:50 +00:00
|
|
|
return path.replace(/\/$/, '') || '/'
|
|
|
|
}
|
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
function loadComponent (jsonData) {
|
|
|
|
const module = evalScript(jsonData.component)
|
|
|
|
const Component = module.default || module
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2017-02-15 08:52:22 +00:00
|
|
|
return { Component, err: jsonData.err }
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|