2017-03-07 18:43:56 +00:00
|
|
|
/* global window */
|
2016-12-19 14:40:26 +00:00
|
|
|
import _Router from './router'
|
2018-02-21 17:41:25 +00:00
|
|
|
import { execOnce } from '../utils'
|
2016-12-19 14:40:26 +00:00
|
|
|
|
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
|
2018-06-05 15:10:28 +00:00
|
|
|
const urlPropertyFields = ['pathname', 'route', 'query', 'asPath']
|
2018-07-05 12:41:18 +00:00
|
|
|
const propertyFields = ['components', 'events']
|
2018-05-25 12:47:58 +00:00
|
|
|
const routerEvents = ['routeChangeStart', 'beforeHistoryChange', 'routeChangeComplete', 'routeChangeError', 'hashChangeStart', 'hashChangeComplete']
|
2018-03-31 14:21:51 +00:00
|
|
|
const coreMethodFields = ['push', 'replace', 'reload', 'back', 'prefetch', 'beforePopState']
|
2016-12-19 14:40:26 +00:00
|
|
|
|
2018-06-05 15:10:28 +00:00
|
|
|
propertyFields.concat(urlPropertyFields).forEach((field) => {
|
2016-12-19 14:40:26 +00:00
|
|
|
// 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(() => {
|
2017-03-13 04:34:30 +00:00
|
|
|
SingletonRouter.router.events.on(event, (...args) => {
|
2016-12-31 01:15:22 +00:00
|
|
|
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(1)}`
|
|
|
|
if (SingletonRouter[eventField]) {
|
2017-07-27 13:50:39 +00:00
|
|
|
try {
|
|
|
|
SingletonRouter[eventField](...args)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error when running the Router event: ${eventField}`)
|
|
|
|
console.error(`${err.message}\n${err.stack}`)
|
|
|
|
}
|
2016-12-31 01:15:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-02-21 17:41:25 +00:00
|
|
|
const warnAboutRouterOnAppUpdated = execOnce(() => {
|
|
|
|
console.warn(`Router.onAppUpdated is removed - visit https://err.sh/next.js/no-on-app-updated-hook for more information.`)
|
|
|
|
})
|
|
|
|
|
|
|
|
Object.defineProperty(SingletonRouter, 'onAppUpdated', {
|
|
|
|
get () { return null },
|
|
|
|
set () {
|
|
|
|
warnAboutRouterOnAppUpdated()
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
2017-08-30 14:07:12 +00:00
|
|
|
// Reexport the withRoute HOC
|
|
|
|
export { default as withRouter } from './with-router'
|
|
|
|
|
2016-12-20 17:27:43 +00:00
|
|
|
// 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
|
|
|
|
2017-05-14 00:40:32 +00:00
|
|
|
export function _rewriteUrlForNextExport (url) {
|
2017-06-13 06:14:31 +00:00
|
|
|
const [, hash] = url.split('#')
|
|
|
|
url = url.replace(/#.*/, '')
|
2017-05-14 00:40:32 +00:00
|
|
|
|
2017-06-13 06:14:31 +00:00
|
|
|
let [path, qs] = url.split('?')
|
|
|
|
path = path.replace(/\/$/, '')
|
2017-05-14 00:40:32 +00:00
|
|
|
|
2017-10-05 18:33:10 +00:00
|
|
|
let newPath = path
|
|
|
|
// Append a trailing slash if this path does not have an extension
|
|
|
|
if (!/\.[^/]+\/?$/.test(path)) {
|
|
|
|
newPath = `${path}/`
|
|
|
|
}
|
|
|
|
|
2017-06-13 06:14:31 +00:00
|
|
|
if (qs) {
|
|
|
|
newPath = `${newPath}?${qs}`
|
|
|
|
}
|
2017-05-14 00:40:32 +00:00
|
|
|
|
2017-06-13 06:14:31 +00:00
|
|
|
if (hash) {
|
|
|
|
newPath = `${newPath}#${hash}`
|
2017-05-14 00:40:32 +00:00
|
|
|
}
|
2017-06-13 06:14:31 +00:00
|
|
|
|
|
|
|
return newPath
|
2017-05-14 00:40:32 +00:00
|
|
|
}
|
2017-08-30 14:07:12 +00:00
|
|
|
|
2018-06-05 15:10:28 +00:00
|
|
|
// This function is used to create the `withRouter` router instance
|
2017-08-30 14:07:12 +00:00
|
|
|
export function makePublicRouterInstance (router) {
|
|
|
|
const instance = {}
|
|
|
|
|
2018-06-05 15:10:28 +00:00
|
|
|
for (const property of urlPropertyFields) {
|
|
|
|
if (typeof router[property] === 'object') {
|
|
|
|
instance[property] = {...router[property]} // makes sure query is not stateful
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
instance[property] = router[property]
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:07:12 +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(instance, field, {
|
|
|
|
get () {
|
|
|
|
return router[field]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
coreMethodFields.forEach((field) => {
|
|
|
|
instance[field] = (...args) => {
|
|
|
|
return router[field](...args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return instance
|
|
|
|
}
|