2017-01-11 20:16:18 +00:00
|
|
|
/* global __NEXT_DATA__ */
|
|
|
|
|
2016-12-24 17:29:23 +00:00
|
|
|
import { parse, format } from 'url'
|
2016-12-19 14:40:26 +00:00
|
|
|
import evalScript from '../eval-script'
|
|
|
|
import shallowEquals from '../shallow-equals'
|
2016-12-31 01:15:22 +00:00
|
|
|
import { EventEmitter } from 'events'
|
2017-01-02 11:18:14 +00:00
|
|
|
import { reloadIfPrefetched } from '../prefetch'
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
export default class Router extends EventEmitter {
|
2017-01-12 01:58:20 +00:00
|
|
|
constructor (pathname, query, { 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
|
|
|
|
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
|
2016-10-08 05:12:51 +00:00
|
|
|
this.subscriptions = new Set()
|
2016-12-31 01:15:22 +00:00
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
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
|
|
|
|
this.replace(format({ pathname, query }), getURL())
|
|
|
|
|
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) {
|
2016-10-05 23:52:50 +00:00
|
|
|
this.abortComponentLoad()
|
2016-10-08 05:12:51 +00:00
|
|
|
|
2017-01-02 06:22:07 +00:00
|
|
|
const { url = getURL(), as = url } = e.state || {}
|
2016-12-28 05:27:52 +00:00
|
|
|
const { pathname, query } = parse(url, true)
|
2016-12-30 19:55:00 +00:00
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
if (!this.urlIsNew(pathname, query)) {
|
|
|
|
this.emit('routeChangeStart', as)
|
|
|
|
this.emit('routeChangeComplete', as)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
} = await this.getRouteInfo(route, pathname, query)
|
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error && error.cancelled) {
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeError', error, as)
|
|
|
|
return
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
this.route = route
|
|
|
|
this.set(pathname, query, { ...data, props })
|
2017-01-01 19:36:37 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
this.emit('routeChangeError', error, as)
|
|
|
|
} else {
|
|
|
|
this.emit('routeChangeComplete', 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-01-02 11:18:14 +00:00
|
|
|
await reloadIfPrefetched(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
|
|
|
|
} = await this.getRouteInfo(route, pathname, query)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error && error.cancelled) {
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeError', error, url)
|
2017-01-01 19:36:37 +00:00
|
|
|
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) {
|
2016-12-31 01:15:22 +00:00
|
|
|
this.abortComponentLoad()
|
2016-12-28 05:27:52 +00:00
|
|
|
const { pathname, query } = parse(url, true)
|
2016-12-30 19:55:00 +00:00
|
|
|
|
|
|
|
if (!this.urlIsNew(pathname, query)) {
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeStart', as)
|
2016-12-30 19:55:00 +00:00
|
|
|
changeState()
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeComplete', as)
|
2016-12-30 19:55:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
} = await this.getRouteInfo(route, pathname, query)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
2017-01-01 19:36:37 +00:00
|
|
|
if (error && error.cancelled) {
|
2016-12-31 01:15:22 +00:00
|
|
|
this.emit('routeChangeError', error, as)
|
2017-01-01 19:36:37 +00:00
|
|
|
return false
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 19:55:00 +00:00
|
|
|
changeState()
|
2016-11-03 10:15:03 +00:00
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
this.route = route
|
2016-12-28 05:27:52 +00:00
|
|
|
this.set(pathname, query, { ...data, props })
|
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
|
2016-12-30 19:55:00 +00:00
|
|
|
|
|
|
|
function changeState () {
|
|
|
|
if (method !== 'pushState' || getURL() !== as) {
|
2016-12-31 01:15:22 +00:00
|
|
|
window.history[method]({ url, as }, null, as)
|
2016-12-30 19:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
async getRouteInfo (route, pathname, query) {
|
|
|
|
const routeInfo = {}
|
|
|
|
|
|
|
|
try {
|
2017-01-12 01:58:20 +00:00
|
|
|
const { Component, err, xhr } = routeInfo.data = await this.fetchComponent(route)
|
|
|
|
const ctx = { err, xhr, pathname, query }
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-28 05:27:52 +00:00
|
|
|
set (pathname, query, data) {
|
2016-12-30 19:55:00 +00:00
|
|
|
this.pathname = pathname
|
|
|
|
this.query = query
|
|
|
|
this.notify(data)
|
2016-10-05 23:52:50 +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)
|
|
|
|
}
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
async fetchComponent (route) {
|
2016-10-08 05:12:51 +00:00
|
|
|
let data = this.components[route]
|
2016-10-09 11:01:06 +00:00
|
|
|
if (!data) {
|
|
|
|
let cancel
|
|
|
|
|
|
|
|
data = await new Promise((resolve, reject) => {
|
|
|
|
this.componentLoadCancel = cancel = () => {
|
2016-12-31 01:15:22 +00:00
|
|
|
if (xhr.abort) {
|
|
|
|
xhr.abort()
|
|
|
|
const error = new Error('Fetching componenet cancelled')
|
|
|
|
error.cancelled = true
|
|
|
|
reject(error)
|
|
|
|
}
|
2016-10-09 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:16:18 +00:00
|
|
|
const url = `/_next/${__NEXT_DATA__.buildId}/pages${route}`
|
2016-12-16 20:33:08 +00:00
|
|
|
const xhr = loadComponent(url, (err, data) => {
|
2016-10-09 11:01:06 +00:00
|
|
|
if (err) return reject(err)
|
2017-01-12 01:58:20 +00:00
|
|
|
resolve({ ...data, xhr })
|
2016-10-09 11:01:06 +00:00
|
|
|
})
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
|
2016-10-09 11:01:06 +00:00
|
|
|
if (cancel === this.componentLoadCancel) {
|
|
|
|
this.componentLoadCancel = null
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-10-09 11:01:06 +00:00
|
|
|
this.components[route] = data
|
2016-10-08 05:12:51 +00:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-10-16 22:44:26 +00:00
|
|
|
const props = await (Component.getInitialProps ? Component.getInitialProps(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
|
|
|
}
|
|
|
|
|
|
|
|
abortComponentLoad () {
|
|
|
|
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 getURL () {
|
2016-10-17 00:00:17 +00:00
|
|
|
return window.location.pathname + (window.location.search || '') + (window.location.hash || '')
|
2016-10-08 05:12:51 +00:00
|
|
|
}
|
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(/\/$/, '') || '/'
|
|
|
|
}
|
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
function loadComponent (url, fn) {
|
2016-10-05 23:52:50 +00:00
|
|
|
return loadJSON(url, (err, data) => {
|
2016-10-08 05:12:51 +00:00
|
|
|
if (err) return fn(err)
|
2016-10-05 23:52:50 +00:00
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
let module
|
|
|
|
try {
|
2016-10-19 12:41:45 +00:00
|
|
|
module = evalScript(data.component)
|
2016-10-08 05:12:51 +00:00
|
|
|
} catch (err) {
|
|
|
|
return fn(err)
|
|
|
|
}
|
2016-10-06 01:20:48 +00:00
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
const Component = module.default || module
|
2016-10-19 12:41:45 +00:00
|
|
|
fn(null, { Component, err: data.err })
|
2016-10-08 05:12:51 +00:00
|
|
|
})
|
2016-10-06 01:20:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 23:52:50 +00:00
|
|
|
function loadJSON (url, fn) {
|
2016-10-17 00:00:17 +00:00
|
|
|
const xhr = new window.XMLHttpRequest()
|
2016-10-05 23:52:50 +00:00
|
|
|
xhr.onload = () => {
|
|
|
|
let data
|
|
|
|
|
|
|
|
try {
|
|
|
|
data = JSON.parse(xhr.responseText)
|
|
|
|
} catch (err) {
|
|
|
|
fn(new Error('Failed to load JSON for ' + url))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(null, data)
|
|
|
|
}
|
|
|
|
xhr.onerror = () => {
|
2016-10-08 05:12:51 +00:00
|
|
|
fn(new Error('XHR failed. Status: ' + xhr.status))
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
2016-10-09 11:01:06 +00:00
|
|
|
xhr.onabort = () => {
|
|
|
|
const err = new Error('XHR aborted')
|
|
|
|
err.cancelled = true
|
|
|
|
fn(err)
|
|
|
|
}
|
2016-10-05 23:52:50 +00:00
|
|
|
xhr.open('GET', url)
|
2016-12-16 20:33:08 +00:00
|
|
|
xhr.setRequestHeader('Accept', 'application/json')
|
2016-10-05 23:52:50 +00:00
|
|
|
xhr.send()
|
|
|
|
|
|
|
|
return xhr
|
|
|
|
}
|