Skip to content

fix(nuxt): pass nuxt rootDir to typed-router context - #35565

Merged
danielroe merged 3 commits into
nuxt:mainfrom
Anoesj:fix/typed-pages-root-cwd
Jul 14, 2026
Merged

danielroe merged 3 commits into
nuxt:mainfrom
Anoesj:fix/typed-pages-root-cwd

Conversation

@Anoesj

@Anoesj Anoesj commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Fixes #35564

📚 Description

With experimental.typedPages, the sfc-typed-router Volar plugin rewrites a bare useRoute() in a page to infer that page's route name. This breaks when Nuxt is prepared with process.cwd() different from nuxt.options.rootDir (e.g. nuxt prepare --cwd <subdir> from a parent directory, common in monorepos): useRoute().name falls back to the full keyof RouteNamedMap union.

Root cause: packages/nuxt/src/pages/module.ts builds the typed-router context without a root, so resolveOptions (from vue-router/unplugin) defaults it to process.cwd(). The generated _RouteFileInfoMap keys are then process.cwd()-relative, while the Volar plugin resolves files via relative(nuxt.options.rootDir, file). When the two differ, no key matches.

Fix: pass root: nuxt.options.rootDir into typedRouterOptions, so the DTS keys and the plugin both use rootDir regardless of process.cwd().

Minimal standalone reproduction with 3 demos (same-dir ✅, subdir ❌, subdir + this patch ✅): https://github.com/Anoesj/nuxt-typedpages-cwd-rootdir-bug

🧪 Regression test

test/typed-router.test.ts gains two cases asserting the _RouteFileInfoMap keys are rootDir-relative:

  • prepared from a different cwd (nuxt prepare <rootDir>) — the case that regressed (red before this fix, green after)
  • prepared with cwd === rootDir — control (green either way)

cc @posva: this touches the sfc-typed-router typed-router integration you authored. Feedback welcome.

@github-actions github-actions Bot added 5.x 🐛 bug Something isn't working as expected labels Jul 14, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@35565

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@35565

nuxt

npm i https://pkg.pr.new/nuxt@35565

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@35565

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@35565

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@35565

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@35565

commit: 1f9c60c

@Anoesj
Anoesj marked this pull request as ready for review July 14, 2026 06:43
@Anoesj
Anoesj requested a review from danielroe as a code owner July 14, 2026 06:43
@coderabbitai

This comment has been minimized.

@codspeed

codspeed Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 10.62%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 20 untouched benchmarks
⏩ 3 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ loadNuxt in the basic test fixture (dev) 357.2 ms 322.9 ms +10.62%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Anoesj:fix/typed-pages-root-cwd (1f9c60c) with main (43a63f0)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@Anoesj Anoesj changed the title fix(pages): use rootDir for typed-router context so useRoute() typing survives cwd !== rootDir fix(nuxt): use rootDir for typed-router context so useRoute() typing works even when cwd !== rootDir Jul 14, 2026
Comment thread packages/nuxt/src/pages/module.ts Resolved

@danielroe danielroe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@danielroe danielroe changed the title fix(nuxt): use rootDir for typed-router context so useRoute() typing works even when cwd !== rootDir fix(nuxt): pass nuxt rootDir to typed-router context Jul 14, 2026
@danielroe
danielroe added this pull request to the merge queue Jul 14, 2026
Merged via the queue into nuxt:main with commit c8d7662 Jul 14, 2026
30 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 14, 2026
2 of 5 tasks
danielroe added a commit that referenced this pull request Jul 15, 2026
@github-actions github-actions Bot mentioned this pull request Jul 15, 2026
danielroe added a commit that referenced this pull request Jul 17, 2026
@github-actions github-actions Bot mentioned this pull request Jul 17, 2026
danielroe added a commit that referenced this pull request Jul 17, 2026
danielroe added a commit that referenced this pull request Jul 17, 2026
danielroe added a commit that referenced this pull request Jul 17, 2026
danielroe added a commit that referenced this pull request Jul 17, 2026
@github-actions github-actions Bot mentioned this pull request Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.x 🐛 bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants