Skip to content

Commit

Permalink
fix(nuxt): remove undefined keys in route object (#25667)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored and manniL committed Feb 18, 2024
1 parent 1bb71cf commit ec94929
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/nuxt/src/pages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"meta": "{"hello":"world"}",
"name": ""home"",
"path": ""/"",
"redirect": undefined,
},
],
"should allow pages with `:` in their path": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"meta": "{"hello":"world"}",
"name": ""home"",
"path": ""/"",
"redirect": undefined,
},
],
"should allow pages with `:` in their path": [
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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 => ({
Expand Down

0 comments on commit ec94929

Please sign in to comment.