Skip to content

fix(carbon-ads): keep the script id resolvable after unmount - #872

Merged
harlan-zw merged 1 commit into
mainfrom
fix/carbon-ads-unmount-id
Aug 20, 2026
Merged

harlan-zw merged 1 commit into
mainfrom
fix/carbon-ads-unmount-id

Conversation

@harlan-zw

Copy link
Copy Markdown
Collaborator

📚 Description

Unmounting ScriptCarbonAds crashes the page. carbon.js resolves #_carbonads_js from its own pending ad callback and reads .src off it with no null check, so removing that element throws inside third-party code where nothing can catch it. Sites that key the ad on the route to refresh it per page hit this on every navigation.

The root ref was also seeded from document.getElementById('_carbonads_js'), which is the script's id rather than a container. Every other registry component starts that ref at null.

Open question: two instances mounted at once still collide on the hardcoded id. Carbon's own code assumes one per page, so I left it alone.

🤖 AI disclosure: Harlan Agent Kit modified this description. My AI open-source policy.

@vercel

vercel Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
scripts-playground Ready Ready Preview Aug 20, 2026 3:15am

Request Review

@harlan-zw

Copy link
Copy Markdown
Collaborator Author

Checked in a real browser against live carbon.js, since CI has no Carbon coverage.

Mount, unmount, remount, then three :key cycles on a page that mirrors how unlighthouse.dev uses the component:

Without this change, after unmount:
0 elements match #_carbonads_js
window._carbonads.getUrlVar('serve') -> TypeError: Cannot read properties of null (reading 'src')

With it, after unmount:
1 element matches #_carbonads_js
window._carbonads.getUrlVar('serve') -> 'CW7DTKJL'

Remount and the three key cycles kept exactly one script element and one rendered ad, with no page errors.

@pkg-pr-new

pkg-pr-new Bot commented Aug 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 0d12693

@github-actions

Copy link
Copy Markdown

📦 Package Size

✅ No notable size changes

📚 22 runtime dependencies (no change)

All tracked output (27)
Package output Gzipped Raw
@nuxt/scripts-cli · runtime dependencies 72 kB 355 kB ✅
@nuxt/scripts-cli · dependency magicast 72 kB 355 kB ✅
@nuxt/scripts-cli · export . 3.4 kB 12 kB ✅
@nuxt/scripts-cli · published payload 3.4 kB 12 kB ✅
@nuxt/scripts · runtime dependencies 451 kB 2.00 MB ✅
@nuxt/scripts · dependency @nuxt/devtools-kit 2.9 kB 7.7 kB ✅
@nuxt/scripts · dependency @oxc-project/types 0 B 0 B ✅
@nuxt/scripts · dependency @vueuse/core 174 kB 707 kB ✅
@nuxt/scripts · dependency @vueuse/shared 39 kB 154 kB ✅
@nuxt/scripts · dependency h3 34 kB 146 kB ✅
@nuxt/scripts · dependency magic-string 9.4 kB 42 kB ✅
@nuxt/scripts · dependency oxc-walker 7.6 kB 31 kB ✅
@nuxt/scripts · dependency semver 25 kB 72 kB ✅
@nuxt/scripts · dependency sirv 8.8 kB 21 kB ✅
@nuxt/scripts · dependency unstorage 70 kB 225 kB ✅
@nuxt/scripts · dependency valibot 80 kB 592 kB ✅
@nuxt/scripts · dist/runtime 98 kB 284 kB ✅
@nuxt/scripts · export . 25 kB 105 kB ✅
@nuxt/scripts · export ./registry 28 kB 89 kB ✅
@nuxt/scripts · export ./stats 13 kB 89 kB ✅
@nuxt/scripts · export ./types-source 43 kB 222 kB ✅
@nuxt/scripts · published payload 208 kB 790 kB ✅
@nuxt/scripts · components runtime 2.4 kB 6.2 kB ✅
@nuxt/scripts · composables runtime 7.5 kB 24 kB ✅
@nuxt/scripts · registry runtime 42 kB 123 kB ✅
@nuxt/scripts · server runtime 28 kB 84 kB ✅
@nuxt/scripts · utils runtime 2.5 kB 7.4 kB ✅
Runtime dependencies (22)
Package Dependency Requested Resolved Cost
@nuxt/scripts-cli magicast ^0.5.4 0.5.4 📦 72 kB gzip
@nuxt/scripts-cli pathe ^2.0.3 2.0.3 ♻️ free via Nuxt 4.5.1
@nuxt/scripts @nuxt/devtools-kit ^3.4.1 3.4.1 📦 2.9 kB gzip
@nuxt/scripts @oxc-project/types ^0.143.0 0.143.0 📦 0 B gzip
@nuxt/scripts @vueuse/core ^14.4.0 14.4.0 📦 174 kB gzip
@nuxt/scripts @vueuse/shared ^14.4.0 14.4.0 📦 39 kB gzip
@nuxt/scripts consola ^3.4.2 3.4.2 ♻️ free via Nuxt 4.5.1
@nuxt/scripts defu ^6.1.7 6.1.7 ♻️ free via Nuxt 4.5.1
@nuxt/scripts h3 ^1.15.11 1.15.11 📦 34 kB gzip
@nuxt/scripts magic-string ^1.1.0 1.1.0 📦 9.4 kB gzip, Nuxt has 1.0.0
@nuxt/scripts ofetch ^1.5.1 1.5.1 ♻️ free via Nuxt 4.5.1
@nuxt/scripts ohash ^2.0.11 2.0.11 ♻️ free via Nuxt 4.5.1
@nuxt/scripts oxc-walker ^1.1.1 1.1.1 📦 7.6 kB gzip, Nuxt has 1.0.0
@nuxt/scripts pathe ^2.0.3 2.0.3 ♻️ free via Nuxt 4.5.1
@nuxt/scripts semver ^7.8.5 7.8.5 📦 25 kB gzip
@nuxt/scripts sirv ^3.0.2 3.0.2 📦 8.8 kB gzip
@nuxt/scripts std-env ^4.2.0 4.2.0 ♻️ free via Nuxt 4.5.1
@nuxt/scripts ufo ^1.6.4 1.6.4 ♻️ free via Nuxt 4.5.1
@nuxt/scripts ultrahtml ^1.7.0 1.7.0 ♻️ free via Nuxt 4.5.1
@nuxt/scripts unplugin ^3.3.0 3.3.0 ♻️ free via Nuxt 4.5.1
@nuxt/scripts unstorage ^1.17.5 1.17.5 📦 70 kB gzip
@nuxt/scripts valibot ^1.4.2 1.4.2 📦 80 kB gzip

Baseline: main_@_a423e821___2026-08-20 · gzip is the comparison metric · changes below 16 B gzip are ignored

@coderabbitai

coderabbitai Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 205cde3a-7905-4205-bde8-8b3cdd244f37

📥 Commits

Reviewing files that changed from the base of the PR and between a423e82 and 0d12693.

📒 Files selected for processing (2)
  • packages/script/src/runtime/components/ScriptCarbonAds.vue
  • test/nuxt-runtime/script-component-lifecycle.nuxt.test.ts

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


📝 Walkthrough

Walkthrough

The Carbon Ads component now initializes its element reference as null, removes an existing script before loading, and moves the loaded script to <head> during unmount. Lifecycle tests remove leftover scripts during cleanup, verify script persistence and placement data after unmount, and verify replacement when a new instance mounts.

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

Merge Risk: ⚪ Minimal · up to 0d126

This localized lifecycle fix keeps the script reference resolvable after unmount and adds coverage for the behavior; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the Carbon Ads fix and states the main behavior change after unmount.
Description check ✅ Passed The description explains the unmount crash, the root ref correction, the remaining ID collision, and the validation performed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/carbon-ads-unmount-id

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.

@harlan-zw
harlan-zw merged commit e3e7200 into main Aug 20, 2026
24 of 26 checks passed
@harlan-zw
harlan-zw deleted the fix/carbon-ads-unmount-id branch August 20, 2026 03:33

This branch was successfully deployed

1 active deployment
Preview — 0d12693f Deployed Aug 20, 2026 by vercel[bot]
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