From 24d65be07136e4e0e364ef4a8917a623a4b98635 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 20 Jun 2024 23:18:25 +0100 Subject: [PATCH] feat(nuxt): await custom `routes` function in `router.options` (#27644) --- packages/nuxt/src/pages/build.d.ts | 6 ++++++ packages/nuxt/src/pages/runtime/plugins/prerender.server.ts | 1 - packages/nuxt/src/pages/runtime/plugins/router.ts | 3 +-- packages/schema/src/types/router.ts | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 packages/nuxt/src/pages/build.d.ts diff --git a/packages/nuxt/src/pages/build.d.ts b/packages/nuxt/src/pages/build.d.ts new file mode 100644 index 0000000000..4a17fca4e5 --- /dev/null +++ b/packages/nuxt/src/pages/build.d.ts @@ -0,0 +1,6 @@ +declare module '#build/router.options' { + import type { RouterOptions } from '@nuxt/schema' + + const _default: RouterOptions + export default _default +} diff --git a/packages/nuxt/src/pages/runtime/plugins/prerender.server.ts b/packages/nuxt/src/pages/runtime/plugins/prerender.server.ts index a8cf539151..9a191f9dce 100644 --- a/packages/nuxt/src/pages/runtime/plugins/prerender.server.ts +++ b/packages/nuxt/src/pages/runtime/plugins/prerender.server.ts @@ -5,7 +5,6 @@ import { defineNuxtPlugin } from '#app/nuxt' import { prerenderRoutes } from '#app/composables/ssr' // @ts-expect-error virtual file import _routes from '#build/routes' -// @ts-expect-error virtual file import routerOptions from '#build/router.options' let routes: string[] diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index 50e2420c0b..38bd3a4d39 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -18,7 +18,6 @@ import { navigateTo } from '#app/composables/router' import { appManifest as isAppManifestEnabled } from '#build/nuxt.config.mjs' // @ts-expect-error virtual file import _routes from '#build/routes' -// @ts-expect-error virtual file import routerOptions from '#build/router.options' // @ts-expect-error virtual file import { globalMiddleware, namedMiddleware } from '#build/middleware' @@ -61,7 +60,7 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({ : createMemoryHistory(routerBase) ) - const routes = routerOptions.routes?.(_routes) ?? _routes + const routes = routerOptions.routes ? await routerOptions.routes(_routes) ?? _routes : _routes let startPosition: Parameters[2] | null diff --git a/packages/schema/src/types/router.ts b/packages/schema/src/types/router.ts index 76abc6acb8..c7cb0ce850 100644 --- a/packages/schema/src/types/router.ts +++ b/packages/schema/src/types/router.ts @@ -2,7 +2,7 @@ import type { RouterHistory, RouterOptions as _RouterOptions } from 'vue-router' export type RouterOptions = Partial> & { history?: (baseURL?: string) => RouterHistory - routes?: (_routes: _RouterOptions['routes']) => _RouterOptions['routes'] + routes?: (_routes: _RouterOptions['routes']) => _RouterOptions['routes'] | Promise<_RouterOptions['routes']> hashMode?: boolean scrollBehaviorType?: 'smooth' | 'auto' }