Skip to content

fix: avoid mutating runtimeConfig scriptOptions - #638

Merged
harlan-zw merged 1 commit into
nuxt:mainfrom
onmax:fix/script-options-mutation
Mar 13, 2026
Merged

fix: avoid mutating runtimeConfig scriptOptions#638
harlan-zw merged 1 commit into
nuxt:mainfrom
onmax:fix/script-options-mutation

Conversation

@onmax

@onmaxonmax commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

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

LinkExpected
Bugscripts-devalue-use❌ 500 "Cannot stringify a function"
Fixscripts-devalue-use-fix✅ Page renders

CLI Reproduction

git clone --depth 1 --filter=blob:none --sparse --branch repro/scripts-devalue-use 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

vercelBot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

@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

pkg-pr-newBot commented Mar 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: e655935

@coderabbitai

Copy link
Copy Markdown

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 nameStatusExplanationResolution
Description check⚠️ WarningThe 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⚠️ WarningDocstring 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 nameStatusExplanation
Title check✅ PassedThe 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.

@harlan-zw

Copy link
Copy Markdown
Collaborator

Hey, thanks for this. Sorry, I missed it somehow before now 🤷

@harlan-zw
harlan-zw merged commit 7e139b3 into nuxt:mainMar 13, 2026
3 of 4 checks passed
@onmax
onmax deleted the fix/script-options-mutation branch March 13, 2026 05:58
@harlan-zwharlan-zw mentioned this pull request Mar 19, 2026
Sign up for freeto 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.

2 participants

@onmax@harlan-zw