Skip to content

typedPages: useRoute() type falls back to keyof RouteNamedMap when process.cwd() !== rootDir #35564

Closed

Description

@Anoesj

Environment

  • Nuxt 4.4.x (reproduced on 4.4.8), experimental.typedPages: true
  • vue-router 5.x

Reproduction

Minimal repro: https://github.com/Anoesj/nuxt-typedpages-cwd-rootdir-bug

Three independent, runnable demos:

  • 1-regular-flow — nuxt.config.ts + package.json in one dir (cwd === rootDir) → useRoute() typing works
  • 2-bug-subdir — nuxt.config.ts in a subdir prepared with --cwd (cwd !== rootDir) → typing breaks (vue-tsc errors)
  • 3-patched-subdir — same layout + the one-line fix → works

Describe the bug

With experimental.typedPages, the sfc-typed-router Volar plugin rewrites a bare useRoute() in a page to useRoute<_RouteNamesForFilePath<'<file>'>>(), so route.name is inferred as that page's route name.

This silently breaks whenever Nuxt is prepared with process.cwd() different from nuxt.options.rootDir — e.g. a Nuxt app in a subdirectory prepared via nuxt <cmd> --cwd <subdir> from a parent directory (common in monorepos; --cwd sets rootDir but not the OS process.cwd()). useRoute().name then degrades to the full keyof RouteNamedMap union.

Root cause

packages/nuxt/src/pages/module.ts builds the typed-router context without a root:

const typedRouterOptions: TypedRouterOptions = {
  routesFolder: [],
  dts: declarationFile,
  // …
}
const context = createRoutesContext(resolveOptions(typedRouterOptions))

resolveOptions (from vue-router/unplugin) defaults root to process.cwd(), so the _RouteFileInfoMap keys in .nuxt/types/typed-router.d.ts are generated relative to process.cwd(). But the Volar plugin is registered with options: { rootDir: nuxt.options.rootDir } and resolves files via relative(rootDir, file). When the two bases differ, no key matches and _RouteNamesForFilePath<…> falls back to keyof RouteNamedMap.

The generated key flips accordingly:

cwd !== rootDir → 'myapp/app/pages/index.vue': { routes: 'index' … }   (cwd-relative)
cwd === rootDir → 'app/pages/index.vue':       { routes: 'index' … }   (rootDir-relative)

The plugin always queries relative(rootDir, file) = app/pages/index.vue, so only the rootDir-relative keys match.

Fix

Pass root: nuxt.options.rootDir into typedRouterOptions so the DTS keys and the Volar plugin both use rootDir, regardless of process.cwd(). A PR follows.

Activity

  1. coderabbitai commented on Jul 14, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #35036 - fix(nuxt): render component-less parent routes during client-side nav [merged]
    #35383 - perf(nuxt): reduce dev pages startup work [merged]
    #35408 - fix(nuxt): ensure page:start finishes before page:finish starts [merged]
    #35440 - fix(nuxt): avoid syncing route too early when reused page remounts [merged]
    #35442 - fix(nuxt): restore deferred query route before page is mounted [merged]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. added
    🔨 p3-minor
    and removed on Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    types🔨 p3-minor

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions