2017-03-07 19:43:56 +01:00
|
|
|
/* global window */
|
2019-04-03 11:28:53 +02:00
|
|
|
import React from 'react'
|
2019-09-04 16:00:54 +02:00
|
|
|
import Router, { NextRouter } from '../next-server/lib/router/router'
|
|
|
|
import { RouterContext } from '../next-server/lib/router-context'
|
2016-12-19 15:40:26 +01:00
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
type ClassArguments<T> = T extends new (...args: infer U) => any ? U : any
|
2019-04-16 06:00:48 +02:00
|
|
|
|
|
|
|
type RouterArgs = ClassArguments<typeof Router>
|
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
type SingletonRouterBase = {
|
2019-04-16 06:00:48 +02:00
|
|
|
router: Router | null
|
|
|
|
readyCallbacks: Array<() => any>
|
2019-05-29 13:57:26 +02:00
|
|
|
ready(cb: () => any): void
|
2019-04-16 06:00:48 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 23:56:59 +01:00
|
|
|
export { Router, NextRouter }
|
2019-04-29 18:47:40 +02:00
|
|
|
|
2019-07-11 19:35:39 +02:00
|
|
|
export type SingletonRouter = SingletonRouterBase & NextRouter
|
2019-04-16 06:00:48 +02:00
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
const singletonRouter: SingletonRouterBase = {
|
2016-12-31 02:15:22 +01:00
|
|
|
router: null, // holds the actual router instance
|
|
|
|
readyCallbacks: [],
|
2019-04-16 06:00:48 +02:00
|
|
|
ready(cb: () => void) {
|
2016-12-31 02:15:22 +01:00
|
|
|
if (this.router) return cb()
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
this.readyCallbacks.push(cb)
|
|
|
|
}
|
2019-04-16 06:00:48 +02:00
|
|
|
},
|
2016-12-31 02:15:22 +01:00
|
|
|
}
|
2016-12-19 15:40:26 +01:00
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
// Create public properties and methods of the router in the singletonRouter
|
2020-02-15 19:01:10 +01:00
|
|
|
const urlPropertyFields = [
|
|
|
|
'pathname',
|
|
|
|
'route',
|
|
|
|
'query',
|
|
|
|
'asPath',
|
|
|
|
'components',
|
|
|
|
'isFallback',
|
2020-04-14 09:50:39 +02:00
|
|
|
'basePath',
|
2020-10-07 23:11:01 +02:00
|
|
|
'locale',
|
|
|
|
'locales',
|
2020-10-08 13:12:17 +02:00
|
|
|
'defaultLocale',
|
2020-12-31 17:54:32 +01:00
|
|
|
'isReady',
|
2021-02-18 19:34:33 +01:00
|
|
|
'isPreview',
|
2021-02-11 11:18:24 +01:00
|
|
|
'isLocaleDomain',
|
2021-06-22 18:55:52 +02:00
|
|
|
'domainLocales',
|
2020-02-15 19:01:10 +01:00
|
|
|
]
|
2019-05-29 13:57:26 +02:00
|
|
|
const routerEvents = [
|
|
|
|
'routeChangeStart',
|
|
|
|
'beforeHistoryChange',
|
|
|
|
'routeChangeComplete',
|
|
|
|
'routeChangeError',
|
|
|
|
'hashChangeStart',
|
|
|
|
'hashChangeComplete',
|
2021-06-22 20:43:09 +02:00
|
|
|
] as const
|
|
|
|
export type RouterEvent = typeof routerEvents[number]
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
const coreMethodFields = [
|
|
|
|
'push',
|
|
|
|
'replace',
|
|
|
|
'reload',
|
|
|
|
'back',
|
|
|
|
'prefetch',
|
|
|
|
'beforePopState',
|
|
|
|
]
|
2016-12-19 15:40:26 +01:00
|
|
|
|
2018-07-31 21:04:14 +02:00
|
|
|
// Events is a static property on the router, the router doesn't have to be initialized to use it
|
2019-04-26 09:37:57 +02:00
|
|
|
Object.defineProperty(singletonRouter, 'events', {
|
2019-04-16 06:00:48 +02:00
|
|
|
get() {
|
|
|
|
return Router.events
|
|
|
|
},
|
2018-07-31 21:04:14 +02:00
|
|
|
})
|
|
|
|
|
2021-01-05 16:11:37 +01:00
|
|
|
urlPropertyFields.forEach((field: string) => {
|
2021-06-11 11:51:52 +02:00
|
|
|
// Here we need to use Object.defineProperty because we need to return
|
2016-12-19 15:40:26 +01:00
|
|
|
// 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
|
2019-04-26 09:37:57 +02:00
|
|
|
Object.defineProperty(singletonRouter, field, {
|
2019-04-16 06:00:48 +02:00
|
|
|
get() {
|
|
|
|
const router = getRouter() as any
|
|
|
|
return router[field] as string
|
|
|
|
},
|
2016-12-19 15:40:26 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-05 16:11:37 +01:00
|
|
|
coreMethodFields.forEach((field: string) => {
|
2019-04-16 06:00:48 +02:00
|
|
|
// We don't really know the types here, so we add them later instead
|
2019-05-29 13:57:26 +02:00
|
|
|
;(singletonRouter as any)[field] = (...args: any[]) => {
|
2019-04-16 06:00:48 +02:00
|
|
|
const router = getRouter() as any
|
|
|
|
return router[field](...args)
|
2016-12-19 15:40:26 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-22 20:43:09 +02:00
|
|
|
routerEvents.forEach((event) => {
|
2019-04-26 09:37:57 +02:00
|
|
|
singletonRouter.ready(() => {
|
2019-04-16 06:00:48 +02:00
|
|
|
Router.events.on(event, (...args) => {
|
2019-05-29 13:57:26 +02:00
|
|
|
const eventField = `on${event.charAt(0).toUpperCase()}${event.substring(
|
|
|
|
1
|
|
|
|
)}`
|
2019-04-26 09:37:57 +02:00
|
|
|
const _singletonRouter = singletonRouter as any
|
|
|
|
if (_singletonRouter[eventField]) {
|
2017-07-27 15:50:39 +02:00
|
|
|
try {
|
2019-04-26 09:37:57 +02:00
|
|
|
_singletonRouter[eventField](...args)
|
2017-07-27 15:50:39 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error when running the Router event: ${eventField}`)
|
|
|
|
console.error(`${err.message}\n${err.stack}`)
|
|
|
|
}
|
2016-12-31 02:15:22 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-10 23:52:30 +02:00
|
|
|
function getRouter(): Router {
|
2019-04-26 09:37:57 +02:00
|
|
|
if (!singletonRouter.router) {
|
2019-05-29 13:57:26 +02:00
|
|
|
const message =
|
|
|
|
'No router instance found.\n' +
|
2021-06-11 11:51:52 +02:00
|
|
|
'You should only use "next/router" on the client side of your app.\n'
|
2016-12-20 18:27:43 +01:00
|
|
|
throw new Error(message)
|
|
|
|
}
|
2019-04-26 09:37:57 +02:00
|
|
|
return singletonRouter.router
|
2016-12-20 18:27:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
// Export the singletonRouter and this is the public API.
|
|
|
|
export default singletonRouter as SingletonRouter
|
2016-12-20 18:27:43 +01:00
|
|
|
|
2017-08-30 16:07:12 +02:00
|
|
|
// Reexport the withRoute HOC
|
2019-07-25 15:58:24 +02:00
|
|
|
export { default as withRouter } from './with-router'
|
2017-08-30 16:07:12 +02:00
|
|
|
|
2020-05-10 23:52:30 +02:00
|
|
|
export function useRouter(): NextRouter {
|
2019-04-03 11:28:53 +02:00
|
|
|
return React.useContext(RouterContext)
|
|
|
|
}
|
|
|
|
|
2016-12-20 18:27:43 +01: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.
|
2021-06-11 11:51:52 +02:00
|
|
|
// This should **not** be used inside the server.
|
2020-05-10 23:52:30 +02:00
|
|
|
export const createRouter = (...args: RouterArgs): Router => {
|
2019-04-26 09:37:57 +02:00
|
|
|
singletonRouter.router = new Router(...args)
|
2020-05-18 21:24:37 +02:00
|
|
|
singletonRouter.readyCallbacks.forEach((cb) => cb())
|
2019-04-26 09:37:57 +02:00
|
|
|
singletonRouter.readyCallbacks = []
|
2016-12-31 02:15:22 +01:00
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
return singletonRouter.router
|
2016-12-19 15:40:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 17:10:28 +02:00
|
|
|
// This function is used to create the `withRouter` router instance
|
2019-07-11 19:35:39 +02:00
|
|
|
export function makePublicRouterInstance(router: Router): NextRouter {
|
2019-04-16 06:00:48 +02:00
|
|
|
const _router = router as any
|
|
|
|
const instance = {} as any
|
2017-08-30 16:07:12 +02:00
|
|
|
|
2018-06-05 17:10:28 +02:00
|
|
|
for (const property of urlPropertyFields) {
|
2019-04-16 06:00:48 +02:00
|
|
|
if (typeof _router[property] === 'object') {
|
2020-10-07 23:11:01 +02:00
|
|
|
instance[property] = Object.assign(
|
|
|
|
Array.isArray(_router[property]) ? [] : {},
|
|
|
|
_router[property]
|
|
|
|
) // makes sure query is not stateful
|
2018-06-05 17:10:28 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:00:48 +02:00
|
|
|
instance[property] = _router[property]
|
2018-06-05 17:10:28 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 21:04:14 +02:00
|
|
|
// Events is a static property on the router, the router doesn't have to be initialized to use it
|
2019-04-16 06:00:48 +02:00
|
|
|
instance.events = Router.events
|
2018-07-31 21:04:14 +02:00
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
coreMethodFields.forEach((field) => {
|
2019-04-16 06:00:48 +02:00
|
|
|
instance[field] = (...args: any[]) => {
|
|
|
|
return _router[field](...args)
|
2017-08-30 16:07:12 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return instance
|
|
|
|
}
|