Skip to content

fix: avoid mutating runtimeConfig scriptOptions#638

Open
onmax wants to merge 1 commit intonuxt:mainfrom
onmax:fix/script-options-mutation
Open

fix: avoid mutating runtimeConfig scriptOptions#638
onmax wants to merge 1 commit intonuxt:mainfrom
onmax:fix/script-options-mutation

Conversation

@onmax
Copy link

@onmax onmax commented Mar 6, 2026

useRegistryScript uses Object.assign(userOptions?.scriptOptions || {}, options.scriptOptions || {}) to merge script options. Since Object.assign mutates the first argument and userOptions.scriptOptions is a reference to the runtimeConfig object, registry-defined functions like use() get injected directly into runtimeConfig.public.scripts.<key>.scriptOptions. When Nuxt serializes the payload, devalue throws Cannot stringify a function.

Replaced Object.assign with object spread to create a new object instead of mutating the runtimeConfig reference.

This error is present in the latest stable and in the latest beta versions

StackBlitz

Link Expected
Bug scripts-devalue-use ❌ 500 "Cannot stringify a function"
Fix scripts-devalue-use-fix ✅ Page renders

CLI Reproduction

git clone --depth 1 --filter=blob:none --sparse https://github.com/onmax/repros.git
cd repros && git sparse-checkout set scripts-devalue-use
cd scripts-devalue-use && pnpm i && pnpm dev
# Visit http://localhost:3000 → 500 error

Verify fix

git sparse-checkout add scripts-devalue-use-fix
cd ../scripts-devalue-use-fix && pnpm i && pnpm dev
# Visit http://localhost:3000 → page renders

@vercel
Copy link
Contributor

vercel bot commented Mar 6, 2026

@onmax is attempting to deploy a commit to the Nuxt Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 6, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/scripts@638

commit: e655935

@coderabbitai
Copy link

coderabbitai bot commented Mar 6, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f38fd486-b30f-439e-b2b9-487dc26cfb43

📥 Commits

Reviewing files that changed from the base of the PR and between 358aee0 and a5a2af5.

📒 Files selected for processing (2)
  • src/runtime/utils.ts
  • test/unit/utils.test.ts

📝 Walkthrough

Walkthrough

The pull request includes a refactoring of the script options merging logic in src/runtime/utils.ts, replacing Object.assign() with spread operator syntax, and adds a new unit test in test/unit/utils.test.ts to verify that user-provided scriptOptions remain immutable when passed to useRegistryScript. The refactoring preserves the original merge behavior while the test ensures the function does not mutate input parameters.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete according to the template. It lacks the required 'Linked issue' section and 'Type of change' checkboxes that are specified in the repository template. Add a 'Linked issue' section and mark the appropriate 'Type of change' checkbox (Bug fix) to match the template structure.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing a mutation issue with runtimeConfig's scriptOptions by replacing Object.assign with spread syntax.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

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.

1 participant