fix(nuxt): remove undefined keys in route object (#25667)

This commit is contained in:
Daniel Roe 2024-02-07 10:00:19 +00:00 committed by GitHub
parent e317349688
commit 4f3391fc0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 4 deletions

View File

@ -139,8 +139,8 @@ const pageContentsCache: Record<string, string> = {}
const metaCache: Record<string, Partial<Record<keyof NuxtPage, any>>> = {}
async function getRouteMeta (contents: string, absolutePath: string): Promise<Partial<Record<keyof NuxtPage, any>>> {
// set/update pageContentsCache, invalidate metaCache on cache mismatch
if (!(absolutePath in pageContentsCache) || pageContentsCache[absolutePath] !== contents) {
pageContentsCache[absolutePath] = contents
if (!(absolutePath in pageContentsCache) || pageContentsCache[absolutePath] !== contents) {
pageContentsCache[absolutePath] = contents
delete metaCache[absolutePath]
}
@ -414,6 +414,12 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> =
redirect: serializeRouteValue(page.redirect),
}
for (const key of ['path', 'name', 'meta', 'alias', 'redirect'] satisfies NormalizedRouteKeys) {
if (route[key] === undefined) {
delete route[key]
}
}
if (page.children?.length) {
route.children = normalizeRoutes(page.children, metaImports, overrideMeta).routes
}

View File

@ -15,7 +15,6 @@
"meta": "{"hello":"world"}",
"name": ""home"",
"path": ""/"",
"redirect": undefined,
},
],
"should allow pages with `:` in their path": [

View File

@ -14,7 +14,6 @@
"meta": "{"hello":"world"}",
"name": ""home"",
"path": ""/"",
"redirect": undefined,
},
],
"should allow pages with `:` in their path": [

View File

@ -100,6 +100,14 @@ export default defineNuxtConfig({
}))
addBuildPlugin(plugin)
},
function (_options, nuxt) {
nuxt.hook('pages:extend', pages => {
pages.push({
path: '/manual-redirect',
redirect: '/',
})
})
},
function (_options, nuxt) {
const routesToDuplicate = ['/async-parent', '/fixed-keyed-child-parent', '/keyed-child-parent', '/with-layout', '/with-layout2']
const stripLayout = (page: NuxtPage): NuxtPage => ({