diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index aff818a2a..f42cb8e7b 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -14,6 +14,17 @@ type EngineInfoByRoute = Record; let Router: typeof EmberRouter; +interface GetRoute { + (name: string): unknown; + isEmbroiderRouterHandler?: true; +} + +interface Internals { + _routerMicrolib: { + getRoute: GetRoute; + }; +} + if (macroCondition(getGlobalConfig()['@embroider/core']?.active ?? false)) { const waiter = buildWaiter('@embroider/router:lazy-route-waiter'); @@ -65,19 +76,18 @@ if (macroCondition(getGlobalConfig()['@embroider/core']?.active ?? // This is the framework method that we're overriding to provide our own // handlerResolver. - setupRouter(...args: unknown[]) { + setupRouter(this: this & Internals, ...args: unknown[]) { // @ts-expect-error extending private method let isSetup = super.setupRouter(...args); - if (isSetup) { - let microLib = (this as unknown as { _routerMicrolib: { getRoute: (name: string) => unknown } }) - ._routerMicrolib; + let microLib = this._routerMicrolib; + if (!microLib.getRoute.isEmbroiderRouterHandler) { microLib.getRoute = this._handlerResolver(microLib.getRoute.bind(microLib)); } return isSetup; } private _handlerResolver(original: (name: string) => unknown) { - return (name: string) => { + let handler = ((name: string) => { const bundle = this.lazyBundle(name); if (!bundle || bundle.loaded) { return original(name); @@ -96,7 +106,9 @@ if (macroCondition(getGlobalConfig()['@embroider/core']?.active ?? throw err; } ); - }; + }) as GetRoute; + handler.isEmbroiderRouterHandler = true; + return handler; } }