Repository: nuxt/nuxt
File: packages/nuxt/src/pages/module.ts (TypeScript)

Code before:

    if (useExperimentalTypedPages) {
      const declarationFile = './types/typed-router.d.ts'

      const typedRouterOptions: TypedRouterOptions = {
        routesFolder: [],
        dts: resolve(nuxt.options.buildDir, declarationFile),
        logs: nuxt.options.debug && nuxt.options.debug.router,
        async beforeWriteFiles (rootPage) {
          rootPage.children.forEach(child => child.delete())
          const pages = nuxt.apps.default?.pages || await resolvePagesRoutes(options.pattern, nuxt)
          if (nuxt.apps.default) {
            nuxt.apps.default.pages = pages
          }
          const addedPagePaths = new Set<string>()
          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 = addedPagePaths.has(absolutePagePath) ? parent : parent.insert(page.path, page.file)
            addedPagePaths.add(absolutePagePath)
            if (page.meta) {
              route.addToMeta(page.meta)
            }
            if (page.alias) {
              route.addAlias(page.alias)
            }
            if (page.name) {
              route.name = page.name
            }
            // TODO: implement redirect support
            // if (page.redirect) {}
            if (page.children) {
              page.children.forEach(child => addPage(route, child))
            }
          }

          for (const page of pages) {
            addPage(rootPage, page)
          }
        },
      }

      nuxt.hook('prepare:types', ({ references }) => {
        // This file will be generated by unplugin-vue-router
        references.push({ path: declarationFile })
        references.push({ types: 'unplugin-vue-router/client' })
      })


Review comment (bug):
this is the key thing - by inserting the page at the root, uvr is able to correctly map the route to the right place in the tree.

not sure if this is a bug in uvr or not, but it does seem to resolve the issue for nuxt

Code after:

    if (useExperimentalTypedPages) {
      const declarationFile = './types/typed-router.d.ts'

      const typedRouterOptions: TypedRouterOptions = {
        routesFolder: [],
        dts: resolve(nuxt.options.buildDir, declarationFile),
        logs: nuxt.options.debug && nuxt.options.debug.router,
        async beforeWriteFiles (rootPage) {
          rootPage.children.forEach(child => child.delete())
          const pages = nuxt.apps.default?.pages || await resolvePagesRoutes(options.pattern, nuxt)
          if (nuxt.apps.default) {
            nuxt.apps.default.pages = pages
          }
          const addedPagePaths = new Set<string>()
          function addPage (parent: EditableTreeNode, page: NuxtPage) {
            // Avoid duplicate keys in the generated RouteNamedMap type
            const absolutePagePath = joinURL(parent.path, page.path)

            // way to add a route without a file, which must be possible
            const route = addedPagePaths.has(absolutePagePath)
              ? parent
              : /^\//.test(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
                ? rootPage.insert(page.path, page.file)
                // @ts-expect-error TODO: either fix types upstream or figure out another
                // way to add a route without a file, which must be possible
                : parent.insert(page.path, page.file)

            addedPagePaths.add(absolutePagePath)
            if (page.meta) {
              route.addToMeta(page.meta)
            }
            if (page.alias) {
              route.addAlias(page.alias)
            }
            if (page.name) {
              route.name = page.name
            }
            // TODO: implement redirect support
            // if (page.redirect) {}
            if (page.children) {
              page.children.forEach(child => addPage(route, child))
            }
          }

          for (const page of pages) {
            addPage(rootPage, page)
          }
        },