feat(nuxt): await custom routes function in router.options (#27644)

This commit is contained in:
Daniel Roe 2024-06-20 23:18:25 +01:00
parent 0807de5037
commit 24d65be071
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
4 changed files with 8 additions and 4 deletions

6
packages/nuxt/src/pages/build.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
declare module '#build/router.options' {
import type { RouterOptions } from '@nuxt/schema'
const _default: RouterOptions
export default _default
}

View File

@ -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[]

View File

@ -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<RouterScrollBehavior>[2] | null

View File

@ -2,7 +2,7 @@ import type { RouterHistory, RouterOptions as _RouterOptions } from 'vue-router'
export type RouterOptions = Partial<Omit<_RouterOptions, 'history' | 'routes'>> & {
history?: (baseURL?: string) => RouterHistory
routes?: (_routes: _RouterOptions['routes']) => _RouterOptions['routes']
routes?: (_routes: _RouterOptions['routes']) => _RouterOptions['routes'] | Promise<_RouterOptions['routes']>
hashMode?: boolean
scrollBehaviorType?: 'smooth' | 'auto'
}