fix(nuxt): fix nested page types in typedPages (#29352)

This commit is contained in:
Ryoya 2024-10-11 04:53:51 +09:00 committed by Daniel Roe
parent 2513c80816
commit ff18ecffd3
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
3 changed files with 22 additions and 1 deletions

View File

@ -165,10 +165,15 @@ export default defineNuxtModule({
if (nuxt.apps.default) { if (nuxt.apps.default) {
nuxt.apps.default.pages = pages nuxt.apps.default.pages = pages
} }
const addedPagePaths = new Set<string>()
function addPage (parent: EditableTreeNode, page: NuxtPage) { 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 // @ts-expect-error TODO: either fix types upstream or figure out another
// way to add a route without a file, which must be possible // 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) { if (page.meta) {
route.addToMeta(page.meta) route.addToMeta(page.meta)
} }

View File

@ -0,0 +1,12 @@
<script setup lang="ts">
definePageMeta({
name: 'param2',
alias: ['/param-other'],
})
</script>
<template>
<div>
<!-- -->
</div>
</template>

View File

@ -162,6 +162,10 @@ describe('typed router integration', () => {
// @ts-expect-error this is an invalid param // @ts-expect-error this is an invalid param
router.push({ name: 'param-id', params: { bob: 23 } }) router.push({ name: 'param-id', params: { bob: 23 } })
router.push({ name: 'param-id', params: { id: 4 } }) 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`', () => { it('correctly reads custom names typed in `definePageMeta`', () => {