Skip to content

fix: merge dev route rule instead of replacing user rules - #950

Merged
danielroe merged 1 commit into
nuxt:mainfrom
onmax:fix/merge-dev-route-rule
Oct 20, 2026
Merged

danielroe merged 1 commit into
nuxt:mainfrom
onmax:fix/merge-dev-route-rule

Conversation

@onmax

@onmax onmax commented Oct 20, 2026

Copy link
Copy Markdown
Member

In dev, the public asset strategy assigns the font cache rule straight to nuxt.options.routeRules['/_fonts/**']:

nuxt.options.routeRules[joinURL(context.assetsBaseURL, '**')] = { cache: { maxAge: ONE_YEAR_IN_SECONDS } }

This PR uses extendRouteRules from @nuxt/kit instead.

Why

The assignment replaces anything the user set for /_fonts/** in routeRules, and it only happens in dev. A user rule on that path, like custom headers (CORS for fonts loaded cross-origin) or a key another module reads, disappears in dev but still applies in production. You get no warning, and dev behaves differently from prod.

We hit this with @nuxtjs/better-auth. Our app protects /** with an auth route rule, and better-auth refuses to start when an auth-protected route also has a cache rule. The fonts dev rule inherited the /** auth. The natural fix, '/_fonts/**': { auth: false } in routeRules, got wiped by this assignment, so we had to add it back in a nitro:config hook. better-auth has since exempted /_fonts (nuxt-modules/better-auth#504), so that case no longer blocks us, but the overwrite still drops any other user rule on the path.

extendRouteRules merges with defu and defaults to override: false, so keys the user set win and the cache rule only fills in what's missing. It ships in @nuxt/kit 4.6.2 (the ^4.6.2 range in package.json) and in the 5.x nightly. @nuxt/scripts uses it for the same dev cache rule on its assets path (packages/script/src/assets.ts), so the two modules now match.

One small difference: extendRouteRules writes the rule to both nuxt.options.routeRules and nuxt.options.nitro.routeRules, where the old code only wrote nuxt.options.routeRules. Without a user rule, the result is the same cache rule as before.

Test

I added a case to test/asset-strategy.test.ts. It sets routeRules: { '/_fonts/**': { headers: { 'x-test': '1' } } } in dev and checks that the rule keeps the header and gets the cache rule. It fails on main (the header is dropped) and passes with this change.

🤖 Generated with Claude Code

@onmax
onmax requested a review from danielroe as a code owner October 20, 2026 09:47
@pkg-pr-new

pkg-pr-new Bot commented Oct 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nuxt/fonts/@nuxt/fonts@950

commit: cb23d69

@coderabbitai

coderabbitai Bot commented Oct 20, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 56e03961-14d2-4318-acd5-d7140d5a6b7f
📥 Commits

Reviewing files that changed from the base of the PR and between bcd6bd2 and cb23d69.

📒 Files selected for processing: 2
  • src/assets.ts
  • test/asset-strategy.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

In development, font asset caching is registered through extendRouteRules instead of direct mutation of nuxt.options.routeRules. The test supplies existing route rules and checks that the one-year cache duration is added for /_fonts/** while custom headers are preserved.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to cb23d

The route-rule merge preserves user-defined keys while adding font caching, with targeted test coverage for the changed behavior.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: merging development route rules instead of replacing user-defined rules.
Description check Passed The description directly explains the route-rule overwrite problem, the use of extendRouteRules, the expected behavior, and the added regression test.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/assets.ts

Parsing error: Unexpected token {

test/asset-strategy.test.ts

Parsing error: Unexpected token {

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.12%. Comparing base (bcd6bd2) to head (cb23d69).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #950      +/-   ##
==========================================
- Coverage   70.55%   70.12%   -0.43%     
==========================================
  Files          15       15              
  Lines         781      780       -1     
  Branches      214      214              
==========================================
- Hits          551      547       -4     
- Misses        195      200       +5     
+ Partials       35       33       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@danielroe
danielroe added this pull request to the merge queue Oct 20, 2026
Merged via the queue into nuxt:main with commit d7eb19e Oct 20, 2026
9 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants