2017-05-14 00:25:57 +00:00
|
|
|
/* global __NEXT_DATA__ */
|
|
|
|
|
2019-01-11 15:04:56 +00:00
|
|
|
import React from 'react'
|
2016-12-24 17:29:23 +00:00
|
|
|
import { parse, format } from 'url'
|
2019-01-04 20:49:21 +00:00
|
|
|
import mitt from '../mitt'
|
2018-11-28 14:03:02 +00:00
|
|
|
import shallowEquals from './shallow-equals'
|
2018-09-27 19:10:53 +00:00
|
|
|
import { loadGetInitialProps, getURL } from '../utils'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2019-01-11 15:04:56 +00:00
|
|
|
export const RouterContext = React.createContext()
|
|
|
|
|
2017-03-13 04:34:30 +00:00
|
|
|
export default class Router {
|
2019-01-04 20:49:21 +00:00
|
|
|
static events = mitt()
|
2018-07-31 19:04:14 +00:00
|
|
|
|
2018-04-12 08:33:22 +00:00
|
|
|
constructor (pathname, query, as, { initialProps, pageLoader, App, Component, ErrorComponent, err } = {}) {
|
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-04-18 04:18:43 +00:00
|
|
|
this.components = {}
|
|
|
|
// We should not keep the cache, if there's an error
|
|
|
|
// Otherwise, this cause issues when when going back and
|
|
|
|
// come again to the errored page.
|
|
|
|
if (Component !== ErrorComponent) {
|
2018-04-12 08:33:22 +00:00
|
|
|
this.components[this.route] = { Component, props: initialProps, err }
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|
2017-02-15 19:29:42 +00:00
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
this.components['/_app'] = { Component: App }
|
|
|
|
|
2018-07-31 19:04:14 +00:00
|
|
|
// Backwards compat for Router.router.events
|
|
|
|
// TODO: Should be remove the following major version as it was never documented
|
|
|
|
this.events = Router.events
|
2017-03-13 04:34:30 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
this.pageLoader = pageLoader
|
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-05-03 16:40:09 +00:00
|
|
|
this.asPath = as
|
2016-10-08 05:12:51 +00:00
|
|
|
this.subscriptions = new Set()
|
|
|
|
this.componentLoadCancel = null
|
2018-03-31 14:21:51 +00:00
|
|
|
this._beforePopState = () => true
|
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
|
2019-01-01 01:08:32 +00:00
|
|
|
this.changeState('replaceState', format({ pathname, query }), as)
|
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
|
|
|
}
|
|
|
|
|
2018-11-28 14:03:02 +00:00
|
|
|
static _rewriteUrlForNextExport (url) {
|
|
|
|
const [, hash] = url.split('#')
|
|
|
|
url = url.replace(/#.*/, '')
|
|
|
|
|
|
|
|
let [path, qs] = url.split('?')
|
|
|
|
path = path.replace(/\/$/, '')
|
|
|
|
|
|
|
|
let newPath = path
|
|
|
|
// Append a trailing slash if this path does not have an extension
|
|
|
|
if (!/\.[^/]+\/?$/.test(path)) {
|
|
|
|
newPath = `${path}/`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qs) {
|
|
|
|
newPath = `${newPath}?${qs}`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hash) {
|
|
|
|
newPath = `${newPath}#${hash}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPath
|
|
|
|
}
|
|
|
|
|
2018-08-07 03:20:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-31 14:21:51 +00:00
|
|
|
// If the downstream application returns falsy, return.
|
|
|
|
// They will then be responsible for handling the event.
|
|
|
|
if (!this._beforePopState(e.state)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-06 16:48:35 +00:00
|
|
|
const { url, as, options } = e.state
|
2018-09-18 18:17:35 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2018-08-24 10:30:27 +00:00
|
|
|
if (typeof url === 'undefined' || typeof as === 'undefined') {
|
|
|
|
console.warn('`popstate` event triggered but `event.state` did not have `url` or `as` https://err.sh/zeit/next.js/popstate-state-empty')
|
|
|
|
}
|
|
|
|
}
|
2017-03-06 16:48:35 +00:00
|
|
|
this.replace(url, as, options)
|
2018-08-07 03:20:01 +00:00
|
|
|
};
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-10-17 14:35:31 +00:00
|
|
|
update (route, Component) {
|
2017-03-06 16:48:35 +00:00
|
|
|
const data = this.components[route]
|
|
|
|
if (!data) {
|
|
|
|
throw new Error(`Cannot update unavailable route: ${route}`)
|
|
|
|
}
|
|
|
|
|
2016-10-17 14:35:31 +00:00
|
|
|
const newData = { ...data, Component }
|
|
|
|
this.components[route] = newData
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// pages/_app.js updated
|
|
|
|
if (route === '/_app') {
|
|
|
|
this.notify(this.components[this.route])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-04-18 04:18:43 +00:00
|
|
|
this.pageLoader.clearCache(route)
|
2016-10-24 07:22:15 +00:00
|
|
|
|
|
|
|
if (route !== this.route) return
|
|
|
|
|
2017-04-07 16:52:41 +00:00
|
|
|
const { pathname, query } = this
|
2016-12-31 01:15:22 +00:00
|
|
|
const url = window.location.href
|
2018-06-18 18:54:05 +00:00
|
|
|
// This makes sure we only use pathname + query + hash, to mirror `asPath` coming from the server.
|
|
|
|
const as = window.location.pathname + window.location.search + window.location.hash
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('routeChangeStart', url)
|
2018-06-18 18:54:05 +00:00
|
|
|
const routeInfo = await this.getRouteInfo(route, pathname, query, as)
|
2017-03-06 16:48:35 +00:00
|
|
|
const { error } = routeInfo
|
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
|
|
|
}
|
|
|
|
|
2017-03-06 16:48:35 +00:00
|
|
|
this.notify(routeInfo)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error) {
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('routeChangeError', error, url)
|
2017-01-01 19:36:37 +00:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.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
|
|
|
}
|
|
|
|
|
2017-03-06 16:48:35 +00:00
|
|
|
push (url, as = url, options = {}) {
|
|
|
|
return this.change('pushState', url, as, options)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 16:48:35 +00:00
|
|
|
replace (url, as = url, options = {}) {
|
|
|
|
return this.change('replaceState', url, as, options)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 03:57:51 +00:00
|
|
|
async change (method, _url, _as, options) {
|
|
|
|
// If url and as provided as an object representation,
|
|
|
|
// we'll format them into the string version here.
|
|
|
|
const url = typeof _url === 'object' ? format(_url) : _url
|
2017-05-14 00:25:57 +00:00
|
|
|
let as = typeof _as === 'object' ? format(_as) : _as
|
|
|
|
|
|
|
|
// Add the ending slash to the paths. So, we can serve the
|
|
|
|
// "<page>/index.html" directly for the SSR page.
|
2017-05-14 00:40:32 +00:00
|
|
|
if (__NEXT_DATA__.nextExport) {
|
2018-11-28 14:03:02 +00:00
|
|
|
as = Router._rewriteUrlForNextExport(as)
|
2017-05-14 00:25:57 +00:00
|
|
|
}
|
2017-03-12 03:57:51 +00:00
|
|
|
|
2017-02-28 08:48:40 +00:00
|
|
|
this.abortComponentLoad(as)
|
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
|
2017-06-09 19:32:28 +00:00
|
|
|
// We should not proceed. We should only change the state.
|
2017-02-28 17:31:17 +00:00
|
|
|
if (this.onlyAHashChange(as)) {
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('hashChangeStart', as)
|
2017-06-09 19:32:28 +00:00
|
|
|
this.changeState(method, url, as)
|
|
|
|
this.scrollToHash(as)
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('hashChangeComplete', as)
|
2018-02-22 17:07:40 +00:00
|
|
|
return true
|
2017-02-28 17:31:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:26:34 +00:00
|
|
|
const { pathname: asPathname, query: asQuery } = parse(as, true)
|
2018-01-30 15:40:52 +00:00
|
|
|
const { pathname, query } = parse(url, true)
|
|
|
|
|
2017-02-15 18:27:41 +00:00
|
|
|
// If asked to change the current URL we should reload the current page
|
2018-08-24 10:30:27 +00:00
|
|
|
// (not location.reload() but reload getInitialProps and other Next.js stuffs)
|
2017-02-15 18:27:41 +00:00
|
|
|
// We also need to set the method = replaceState always
|
|
|
|
// as this should not go into the history (That's how browsers work)
|
2018-09-03 13:26:34 +00:00
|
|
|
if (!this.urlIsNew(asPathname, asQuery)) {
|
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)
|
2017-03-06 16:48:35 +00:00
|
|
|
const { shallow = false } = options
|
|
|
|
let routeInfo = null
|
2016-10-08 05:12:51 +00:00
|
|
|
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('routeChangeStart', as)
|
2017-03-06 16:48:35 +00:00
|
|
|
|
|
|
|
// If shallow === false and other conditions met, we reuse the
|
|
|
|
// existing routeInfo for this route.
|
|
|
|
// Because of this, getInitialProps would not run.
|
|
|
|
if (shallow && this.isShallowRoutingPossible(route)) {
|
|
|
|
routeInfo = this.components[route]
|
|
|
|
} else {
|
|
|
|
routeInfo = await this.getRouteInfo(route, pathname, query, as)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { error } = routeInfo
|
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
|
|
|
}
|
|
|
|
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('beforeHistoryChange', as)
|
2017-03-06 16:48:35 +00:00
|
|
|
this.changeState(method, url, as, options)
|
2017-02-28 17:31:17 +00:00
|
|
|
const hash = window.location.hash.substring(1)
|
2016-11-03 10:15:03 +00:00
|
|
|
|
2017-03-06 16:48:35 +00:00
|
|
|
this.set(route, pathname, query, as, { ...routeInfo, hash })
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error) {
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('routeChangeError', error, as)
|
2017-01-01 19:36:37 +00:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.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-03-06 16:48:35 +00:00
|
|
|
changeState (method, url, as, options = {}) {
|
2018-09-27 19:10:53 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
if (typeof window.history === 'undefined') {
|
|
|
|
console.error(`Warning: window.history is not available.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (typeof window.history[method] === 'undefined') {
|
|
|
|
console.error(`Warning: window.history.${method} is not available`)
|
|
|
|
return
|
|
|
|
}
|
2018-02-25 14:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method !== 'pushState' || getURL() !== as) {
|
2017-03-06 16:48:35 +00:00
|
|
|
window.history[method]({ url, as, options }, 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) {
|
2017-03-06 16:48:35 +00:00
|
|
|
let routeInfo = null
|
2016-12-31 01:15:22 +00:00
|
|
|
|
|
|
|
try {
|
2017-03-06 16:48:35 +00:00
|
|
|
routeInfo = this.components[route]
|
|
|
|
if (!routeInfo) {
|
2017-04-18 04:18:43 +00:00
|
|
|
routeInfo = { Component: await this.fetchComponent(route, as) }
|
2017-02-20 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
const { Component } = routeInfo
|
2017-12-05 23:35:30 +00:00
|
|
|
|
2018-12-17 15:09:23 +00:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const { isValidElementType } = require('react-is')
|
|
|
|
if (!isValidElementType(Component)) {
|
|
|
|
throw new Error(`The default export is not a React Component in page: "${pathname}"`)
|
|
|
|
}
|
2017-12-05 23:35:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:40:09 +00:00
|
|
|
const ctx = { pathname, query, asPath: as }
|
2017-01-12 01:58:20 +00:00
|
|
|
routeInfo.props = await this.getInitialProps(Component, ctx)
|
2017-03-06 16:48:35 +00:00
|
|
|
|
|
|
|
this.components[route] = routeInfo
|
2016-12-31 01:15:22 +00:00
|
|
|
} catch (err) {
|
2018-02-21 17:41:25 +00:00
|
|
|
if (err.code === 'PAGE_LOAD_ERROR') {
|
|
|
|
// If we can't load the page it could be one of following reasons
|
|
|
|
// 1. Page doesn't exists
|
|
|
|
// 2. Page does exist in a different zone
|
|
|
|
// 3. Internal error while loading the page
|
2017-04-18 04:18:43 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
// So, doing a hard reload is the proper way to deal with this.
|
|
|
|
window.location.href = as
|
|
|
|
|
|
|
|
// Changing the URL doesn't block executing the current code path.
|
|
|
|
// So, we need to mark it as a cancelled error and stop the routing logic.
|
|
|
|
err.cancelled = true
|
|
|
|
return { error: err }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.cancelled) {
|
|
|
|
return { error: err }
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
const Component = this.ErrorComponent
|
2017-03-06 16:48:35 +00:00
|
|
|
routeInfo = { Component, err }
|
2017-01-12 01:58:20 +00:00
|
|
|
const ctx = { err, pathname, query }
|
2018-06-14 13:58:31 +00:00
|
|
|
try {
|
|
|
|
routeInfo.props = await this.getInitialProps(Component, ctx)
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error in error page `getInitialProps`: ', err)
|
|
|
|
routeInfo.props = {}
|
|
|
|
}
|
2016-12-31 01:15:22 +00:00
|
|
|
|
|
|
|
routeInfo.error = err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routeInfo
|
|
|
|
}
|
|
|
|
|
2017-03-06 16:48:35 +00:00
|
|
|
set (route, pathname, query, as, data) {
|
|
|
|
this.route = route
|
2016-12-30 19:55:00 +00:00
|
|
|
this.pathname = pathname
|
|
|
|
this.query = query
|
2017-05-03 16:40:09 +00:00
|
|
|
this.asPath = as
|
2016-12-30 19:55:00 +00:00
|
|
|
this.notify(data)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 14:21:51 +00:00
|
|
|
beforePopState (cb) {
|
|
|
|
this._beforePopState = cb
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
onlyAHashChange (as) {
|
2017-05-03 16:40:09 +00:00
|
|
|
if (!this.asPath) return false
|
2017-09-27 19:55:49 +00:00
|
|
|
const [ oldUrlNoHash, oldHash ] = this.asPath.split('#')
|
2017-02-28 17:31:17 +00:00
|
|
|
const [ newUrlNoHash, newHash ] = as.split('#')
|
|
|
|
|
2018-06-28 18:56:18 +00:00
|
|
|
// Makes sure we scroll to the provided hash if the url/hash are the same
|
|
|
|
if (newHash && (oldUrlNoHash === newUrlNoHash) && (oldHash === newHash)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:31:17 +00:00
|
|
|
// If the urls are change, there's more than a hash change
|
|
|
|
if (oldUrlNoHash !== newUrlNoHash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-27 19:55:49 +00:00
|
|
|
// If the hash has changed, then it's a hash only change.
|
|
|
|
// This check is necessary to handle both the enter and
|
|
|
|
// leave hash === '' cases. The identity case falls through
|
|
|
|
// and is treated as a next reload.
|
|
|
|
return oldHash !== newHash
|
2017-02-28 17:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 19:32:28 +00:00
|
|
|
scrollToHash (as) {
|
|
|
|
const [ , hash ] = as.split('#')
|
2018-06-28 18:56:18 +00:00
|
|
|
// Scroll to top if the hash is just `#` with no value
|
|
|
|
if (hash === '') {
|
|
|
|
window.scrollTo(0, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:20:45 +00:00
|
|
|
// First we check if the element by id is found
|
|
|
|
const idEl = document.getElementById(hash)
|
|
|
|
if (idEl) {
|
|
|
|
idEl.scrollIntoView()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If there's no element with the id, we check the `name` property
|
|
|
|
// To mirror browsers
|
|
|
|
const nameEl = document.getElementsByName(hash)[0]
|
|
|
|
if (nameEl) {
|
|
|
|
nameEl.scrollIntoView()
|
2017-06-09 19:32:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-06 16:48:35 +00:00
|
|
|
isShallowRoutingPossible (route) {
|
|
|
|
return (
|
|
|
|
// If there's cached routeInfo for the route.
|
|
|
|
Boolean(this.components[route]) &&
|
|
|
|
// If the route is already rendered on the screen.
|
|
|
|
this.route === route
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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)
|
2018-11-24 23:47:39 +00:00
|
|
|
return this.pageLoader.prefetch(route)
|
2017-02-15 08:52:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 23:48:17 +00:00
|
|
|
async fetchComponent (route, as) {
|
2017-02-15 08:52:22 +00:00
|
|
|
let cancelled = false
|
|
|
|
const cancel = this.componentLoadCancel = function () {
|
|
|
|
cancelled = true
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
const Component = await this.fetchRoute(route)
|
2017-02-15 08:52:22 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
if (cancelled) {
|
|
|
|
const error = new Error(`Abort fetching component for route: "${route}"`)
|
|
|
|
error.cancelled = true
|
|
|
|
throw error
|
|
|
|
}
|
2017-02-15 08:52:22 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
if (cancel === this.componentLoadCancel) {
|
|
|
|
this.componentLoadCancel = null
|
2017-07-18 07:00:23 +00:00
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
return Component
|
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
|
2018-05-31 09:47:29 +00:00
|
|
|
const {Component: App} = this.components['/_app']
|
2016-10-08 05:12:51 +00:00
|
|
|
|
2018-05-31 09:47:29 +00:00
|
|
|
const props = await loadGetInitialProps(App, {Component, router: this, 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-04-18 04:18:43 +00:00
|
|
|
async fetchRoute (route) {
|
2018-03-27 18:11:03 +00:00
|
|
|
return this.pageLoader.loadPage(route)
|
2017-02-15 08:52:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 08:48:40 +00:00
|
|
|
abortComponentLoad (as) {
|
2016-10-05 23:52:50 +00:00
|
|
|
if (this.componentLoadCancel) {
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.emit('routeChangeError', new Error('Route Cancelled'), as)
|
2016-10-05 23:52:50 +00:00
|
|
|
this.componentLoadCancel()
|
|
|
|
this.componentLoadCancel = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
notify (data) {
|
2018-05-31 09:47:29 +00:00
|
|
|
const {Component: App} = this.components['/_app']
|
|
|
|
this.subscriptions.forEach((fn) => fn({...data, App}))
|
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(/\/$/, '') || '/'
|
|
|
|
}
|