Skip to content

Instantly share code, notes, and snippets.

@khg0712
Created May 28, 2023 18:17
Show Gist options
  • Save khg0712/0bbbb2dc51ca79205d833539be34c17f to your computer and use it in GitHub Desktop.
Save khg0712/0bbbb2dc51ca79205d833539be34c17f to your computer and use it in GitHub Desktop.
// packages/next/client/router.ts
// Router 싱글톤을 저장하는 객체
// https://github.com/vercel/next.js/blob/v12.3.4/packages/next/client/router.ts#L20
const singletonRouter: SingletonRouterBase = {
router: null, // holds the actual router instance
readyCallbacks: [],
ready(cb: () => void) {
if (this.router) return cb()
if (typeof window !== 'undefined') {
this.readyCallbacks.push(cb)
}
},
}
// createRouter 코드 - Router 싱글톤 초기화
// https://github.com/vercel/next.js/blob/v12.3.4/packages/next/client/router.ts#L146
/**
* Create a router and assign it as the singleton instance.
* router을 생성하고 싱글톤 인스턴스로서 할당한다.
*
* This is used in client side when we are initializing the app.
* client-side에서 앱을 초기화할 때 사용된다.
*
* This should **not** be used inside the server.
* 서버에서 사용되면 **안된다**.
* @internal
*/
export function createRouter(
...args: ConstructorParameters<typeof Router>
): Router {
singletonRouter.router = new Router(...args)
singletonRouter.readyCallbacks.forEach((cb) => cb())
singletonRouter.readyCallbacks = []
return singletonRouter.router
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment