1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/lib/router.js

277 lines
6.3 KiB
JavaScript
Raw Normal View History

2016-10-05 23:52:50 +00:00
import { parse } from 'url'
import evalScript from './eval-script'
import shallowEquals from './shallow-equals'
export default class Router {
constructor (pathname, query, { Component, ErrorComponent, ctx } = {}) {
2016-10-08 05:12:51 +00:00
// represents the current component key
this.route = toRoute(pathname)
2016-10-05 23:52:50 +00:00
// set up the component cache (by route keys)
this.components = { [this.route]: { Component, ctx } }
2016-10-05 23:52:50 +00:00
this.ErrorComponent = ErrorComponent
this.pathname = pathname
this.query = query
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
if (typeof window !== 'undefined') {
2016-10-08 10:16:22 +00:00
window.addEventListener('popstate', this.onPopState)
}
2016-10-05 23:52:50 +00:00
}
onPopState (e) {
this.abortComponentLoad()
2016-10-08 05:12:51 +00:00
const { pathname, query } = parse(window.location.href, true)
const route = (e.state || {}).route || toRoute(pathname)
2016-10-08 05:12:51 +00:00
Promise.resolve()
.then(async () => {
const data = await this.fetchComponent(route)
const ctx = { ...data.ctx, pathname, query }
const props = await this.getInitialProps(data.Component, ctx)
2016-10-08 05:12:51 +00:00
this.route = route
this.set(getURL(), { ...data, props })
})
.catch(async (err) => {
2016-10-08 05:12:51 +00:00
if (err.cancelled) return
const data = { Component: this.ErrorComponent, ctx: { err } }
const ctx = { ...data.ctx, pathname, query }
const props = await this.getInitialProps(data.Component, ctx)
this.route = route
this.set(getURL(), { ...data, props })
console.error(err)
})
.catch((err) => {
console.error(err)
2016-10-08 05:12:51 +00:00
})
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]
if (route !== this.route) return
const { pathname, query } = parse(window.location.href, true)
2016-10-24 07:22:15 +00:00
let data
let props
let _err
2016-10-24 07:22:15 +00:00
try {
data = await this.fetchComponent(route)
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
2016-10-24 07:22:15 +00:00
} catch (err) {
if (err.cancelled) return false
data = { Component: this.ErrorComponent, ctx: { err } }
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
_err = err
console.error(err)
2016-10-24 07:22:15 +00:00
}
this.notify({ ...data, props })
if (_err) throw _err
2016-10-24 07:22:15 +00:00
}
2016-10-05 23:52:50 +00:00
back () {
window.history.back()
2016-10-05 23:52:50 +00:00
}
2016-10-08 05:12:51 +00:00
push (route, url) {
return this.change('pushState', route, url)
2016-10-05 23:52:50 +00:00
}
2016-10-08 05:12:51 +00:00
replace (route, url) {
return this.change('replaceState', route, url)
2016-10-05 23:52:50 +00:00
}
2016-10-08 05:12:51 +00:00
async change (method, route, url) {
const { pathname, query } = parse(route || url, true)
if (!route) route = toRoute(pathname)
2016-10-08 05:12:51 +00:00
2016-10-05 23:52:50 +00:00
this.abortComponentLoad()
2016-10-08 05:12:51 +00:00
let data
let props
let _err
2016-10-08 05:12:51 +00:00
try {
data = await this.fetchComponent(route)
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
2016-10-08 05:12:51 +00:00
} catch (err) {
if (err.cancelled) return false
data = { Component: this.ErrorComponent, ctx: { err } }
const ctx = { ...data.ctx, pathname, query }
props = await this.getInitialProps(data.Component, ctx)
_err = err
console.error(err)
2016-10-05 23:52:50 +00:00
}
if (getURL() !== url) {
window.history[method]({ route }, null, url)
}
2016-10-08 05:12:51 +00:00
this.route = route
this.set(url, { ...data, props })
if (_err) throw _err
2016-10-08 05:12:51 +00:00
return true
2016-10-05 23:52:50 +00:00
}
2016-10-08 05:12:51 +00:00
set (url, data) {
2016-10-05 23:52:50 +00:00
const parsed = parse(url, true)
2016-10-08 05:12:51 +00:00
2016-10-05 23:52:50 +00:00
if (this.urlIsNew(parsed)) {
this.pathname = parsed.pathname
this.query = parsed.query
2016-10-08 05:12:51 +00:00
this.notify(data)
2016-10-05 23:52:50 +00:00
}
}
urlIsNew ({ pathname, query }) {
return this.pathname !== pathname || !shallowEquals(query, this.query)
}
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 = () => {
if (xhr.abort) xhr.abort()
}
const url = `/_next/pages${route}`
const xhr = loadComponent(url, (err, data) => {
2016-10-09 11:01:06 +00:00
if (err) return reject(err)
2016-10-19 12:41:45 +00:00
resolve({
Component: data.Component,
ctx: { xhr, err: data.err }
})
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
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) {
const err = new Error('Cancelled')
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 () {
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-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-05 23:52:50 +00:00
function loadJSON (url, fn) {
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)
xhr.setRequestHeader('Accept', 'application/json')
2016-10-05 23:52:50 +00:00
xhr.send()
return xhr
}