perf(nuxt): use static hashMode option (#30297)

This commit is contained in:
Daniel Roe 2024-12-18 12:12:11 +00:00 committed by GitHub
parent 46e2900a26
commit 7698967fbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 6 deletions

View File

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

View File

@ -42,6 +42,7 @@ export default defineNuxtModule({
delete tsConfig.compilerOptions.paths['#vue-router/*'] delete tsConfig.compilerOptions.paths['#vue-router/*']
}) })
const builtInRouterOptions = await findPath(resolve(runtimeDir, 'router.options')) || resolve(runtimeDir, 'router.options')
async function resolveRouterOptions () { async function resolveRouterOptions () {
const context = { const context = {
files: [] as Array<{ path: string, optional?: boolean }>, files: [] as Array<{ path: string, optional?: boolean }>,
@ -53,7 +54,7 @@ export default defineNuxtModule({
} }
// Add default options at beginning // Add default options at beginning
context.files.unshift({ path: await findPath(resolve(runtimeDir, 'router.options')) || resolve(runtimeDir, 'router.options'), optional: true }) context.files.unshift({ path: builtInRouterOptions, optional: true })
await nuxt.callHook('pages:routerOptions', context) await nuxt.callHook('pages:routerOptions', context)
return context.files return context.files
@ -535,6 +536,7 @@ export default defineNuxtModule({
return [ return [
...routerOptionsFiles.map((file, index) => genImport(file.path, `routerOptions${index}`)), ...routerOptionsFiles.map((file, index) => genImport(file.path, `routerOptions${index}`)),
`const configRouterOptions = ${configRouterOptions}`, `const configRouterOptions = ${configRouterOptions}`,
`export const hashMode = ${[...routerOptionsFiles.filter(o => o.path !== builtInRouterOptions).map((_, index) => `routerOptions${index}.hashMode`).reverse(), nuxt.options.router.options.hashMode].join(' ?? ')}`,
'export default {', 'export default {',
'...configRouterOptions,', '...configRouterOptions,',
...routerOptionsFiles.map((_, index) => `...routerOptions${index},`), ...routerOptionsFiles.map((_, index) => `...routerOptions${index},`),

View File

@ -7,7 +7,7 @@ import { defineNuxtPlugin, useRuntimeConfig } from '#app/nuxt'
import { prerenderRoutes } from '#app/composables/ssr' import { prerenderRoutes } from '#app/composables/ssr'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import _routes from '#build/routes' import _routes from '#build/routes'
import routerOptions from '#build/router.options' import routerOptions, { hashMode } from '#build/router.options'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { crawlLinks } from '#build/nuxt.config.mjs' import { crawlLinks } from '#build/nuxt.config.mjs'
@ -16,7 +16,7 @@ let routes: string[]
let _routeRulesMatcher: undefined | ReturnType<typeof toRouteMatcher> = undefined let _routeRulesMatcher: undefined | ReturnType<typeof toRouteMatcher> = undefined
export default defineNuxtPlugin(async () => { export default defineNuxtPlugin(async () => {
if (!import.meta.server || !import.meta.prerender || routerOptions.hashMode) { if (!import.meta.server || !import.meta.prerender || hashMode) {
return return
} }
if (routes && !routes.length) { return } if (routes && !routes.length) { return }

View File

@ -19,7 +19,7 @@ import { navigateTo } from '#app/composables/router'
import { appManifest as isAppManifestEnabled } from '#build/nuxt.config.mjs' import { appManifest as isAppManifestEnabled } from '#build/nuxt.config.mjs'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import _routes, { handleHotUpdate } from '#build/routes' import _routes, { handleHotUpdate } from '#build/routes'
import routerOptions from '#build/router.options' import routerOptions, { hashMode } from '#build/router.options'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { globalMiddleware, namedMiddleware } from '#build/middleware' import { globalMiddleware, namedMiddleware } from '#build/middleware'
@ -51,13 +51,13 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
enforce: 'pre', enforce: 'pre',
async setup (nuxtApp) { async setup (nuxtApp) {
let routerBase = useRuntimeConfig().app.baseURL let routerBase = useRuntimeConfig().app.baseURL
if (routerOptions.hashMode && !routerBase.includes('#')) { if (hashMode && !routerBase.includes('#')) {
// allow the user to provide a `#` in the middle: `/base/#/app` // allow the user to provide a `#` in the middle: `/base/#/app`
routerBase += '#' routerBase += '#'
} }
const history = routerOptions.history?.(routerBase) ?? (import.meta.client const history = routerOptions.history?.(routerBase) ?? (import.meta.client
? (routerOptions.hashMode ? createWebHashHistory(routerBase) : createWebHistory(routerBase)) ? (hashMode ? createWebHashHistory(routerBase) : createWebHistory(routerBase))
: createMemoryHistory(routerBase) : createMemoryHistory(routerBase)
) )