From fbfb4e35682d1cf0327a758a73a13e1b56a974a5 Mon Sep 17 00:00:00 2001 From: Ryoya <33255443+harunari0928@users.noreply.github.com> Date: Fri, 11 Oct 2024 04:53:51 +0900 Subject: [PATCH] fix(nuxt): fix nested page types in `typedPages` (#29352) --- packages/nuxt/src/pages/module.ts | 7 ++++++- test/fixtures/basic-types/pages/param/index.vue | 12 ++++++++++++ test/fixtures/basic-types/types.ts | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/basic-types/pages/param/index.vue diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index cce4c8681f..074bce293a 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -170,10 +170,15 @@ export default defineNuxtModule({ if (nuxt.apps.default) { nuxt.apps.default.pages = pages } + const addedPagePaths = new Set() function addPage (parent: EditableTreeNode, page: NuxtPage) { + // Avoid duplicate keys in the generated RouteNamedMap type + const absolutePagePath = joinURL(parent.path, page.path) + // @ts-expect-error TODO: either fix types upstream or figure out another // way to add a route without a file, which must be possible - const route = parent.insert(page.path, page.file) + const route = addedPagePaths.has(absolutePagePath) ? parent : parent.insert(page.path, page.file) + addedPagePaths.add(absolutePagePath) if (page.meta) { route.addToMeta(page.meta) } diff --git a/test/fixtures/basic-types/pages/param/index.vue b/test/fixtures/basic-types/pages/param/index.vue new file mode 100644 index 0000000000..82dddbe38d --- /dev/null +++ b/test/fixtures/basic-types/pages/param/index.vue @@ -0,0 +1,12 @@ + + + diff --git a/test/fixtures/basic-types/types.ts b/test/fixtures/basic-types/types.ts index dbaf63e0f7..5b9f7b9883 100644 --- a/test/fixtures/basic-types/types.ts +++ b/test/fixtures/basic-types/types.ts @@ -162,6 +162,10 @@ describe('typed router integration', () => { // @ts-expect-error this is an invalid param router.push({ name: 'param-id', params: { bob: 23 } }) router.push({ name: 'param-id', params: { id: 4 } }) + // @ts-expect-error this is an invalid route + router.push({ name: 'param' }) + // @ts-expect-error this is an invalid route + router.push({ name: '/param' }) }) it('correctly reads custom names typed in `definePageMeta`', () => {