2017-02-20 23:48:17 +00:00
|
|
|
/* global window, location */
|
2016-12-19 14:40:26 +00:00
|
|
|
import _Router from './router'
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
const SingletonRouter = {
|
|
|
|
router: null, // holds the actual router instance
|
|
|
|
readyCallbacks: [],
|
|
|
|
ready (cb) {
|
|
|
|
if (this.router) return cb()
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
this.readyCallbacks.push(cb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 14:40:26 +00:00
|
|
|
|
|
|
|
// Create public properties and methods of the router in the SingletonRouter
|
2016-12-20 17:27:43 +00:00
|
|
|
const propertyFields = ['components', 'pathname', 'route', 'query']
|
2017-02-15 08:52:22 +00:00
|
|
|
const coreMethodFields = ['push', 'replace', 'reload', 'back', 'prefetch']
|
2016-12-31 01:15:22 +00:00
|
|
|
const routerEvents = ['routeChangeStart', 'routeChangeComplete', 'routeChangeError']
|
2016-12-19 14:40:26 +00:00
|
|
|
|
|
|
|
propertyFields.forEach((field) => {
|
|
|
|
// Here we need to use Object.defineProperty because, we need to return
|
|
|
|
// the property assigned to the actual router
|
|
|
|
// The value might get changed as we change routes and this is the
|
|
|
|
// proper way to access it
|
|
|
|
Object.defineProperty(SingletonRouter, field, {
|
|
|
|
get () {
|
2016-12-20 17:27:43 +00:00
|
|
|
throwIfNoRouter()
|
2016-12-31 01:15:22 +00:00
|
|
|
return SingletonRouter.router[field]
|
2016-12-19 14:40:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
coreMethodFields.forEach((field) => {
|
2016-12-19 14:40:26 +00:00
|
|
|
SingletonRouter[field] = (...args) => {
|
2016-12-20 17:27:43 +00:00
|
|
|
throwIfNoRouter()
|
2016-12-31 01:15:22 +00:00
|
|
|
return SingletonRouter.router[field](...args)
|
2016-12-19 14:40:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-12-31 01:15:22 +00:00
|
|
|
routerEvents.forEach((event) => {
|
|
|
|
SingletonRouter.ready(() => {
|
|
|
|
SingletonRouter.router.on(event, (...args) => {
|
|
|
|
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
|
|
|
|
if (SingletonRouter[eventField]) {
|
|
|
|
SingletonRouter[eventField](...args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-12-20 17:27:43 +00:00
|
|
|
function throwIfNoRouter () {
|
2016-12-31 01:15:22 +00:00
|
|
|
if (!SingletonRouter.router) {
|
2016-12-20 17:27:43 +00:00
|
|
|
const message = 'No router instance found.\n' +
|
|
|
|
'You should only use "next/router" inside the client side of your app.\n'
|
|
|
|
throw new Error(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the SingletonRouter and this is the public API.
|
|
|
|
export default SingletonRouter
|
|
|
|
|
|
|
|
// INTERNAL APIS
|
|
|
|
// -------------
|
|
|
|
// (do not use following exports inside the app)
|
|
|
|
|
|
|
|
// Create a router and assign it as the singleton instance.
|
|
|
|
// This is used in client side when we are initilizing the app.
|
|
|
|
// This should **not** use inside the server.
|
2016-12-19 14:40:26 +00:00
|
|
|
export const createRouter = function (...args) {
|
2016-12-31 01:15:22 +00:00
|
|
|
SingletonRouter.router = new _Router(...args)
|
|
|
|
SingletonRouter.readyCallbacks.forEach(cb => cb())
|
|
|
|
SingletonRouter.readyCallbacks = []
|
|
|
|
|
|
|
|
return SingletonRouter.router
|
2016-12-19 14:40:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 17:27:43 +00:00
|
|
|
// Export the actual Router class, which is usually used inside the server
|
2016-12-19 14:40:26 +00:00
|
|
|
export const Router = _Router
|
2017-02-20 23:48:17 +00:00
|
|
|
|
|
|
|
export function _notifyBuildIdMismatch (nextRoute) {
|
|
|
|
if (SingletonRouter.onAppUpdated) {
|
|
|
|
SingletonRouter.onAppUpdated(nextRoute)
|
|
|
|
} else {
|
|
|
|
location.href = nextRoute
|
|
|
|
}
|
|
|
|
}
|