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

fix(carbon-ads): keep the script id resolvable after unmount#872
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

vercelBot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

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

ProjectDeploymentActionsUpdated (UTC)
scripts-playgroundReadyReadyPreviewAug 20, 2026 3:15am

Request Review

@harlan-zw

Copy link
Copy Markdown
CollaboratorAuthor

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

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 outputGzippedRaw
@nuxt/scripts-cli · runtime dependencies72 kB355 kB
@nuxt/scripts-cli · dependency magicast72 kB355 kB
@nuxt/scripts-cli · export .3.4 kB12 kB
@nuxt/scripts-cli · published payload3.4 kB12 kB
@nuxt/scripts · runtime dependencies451 kB2.00 MB
@nuxt/scripts · dependency @nuxt/devtools-kit2.9 kB7.7 kB
@nuxt/scripts · dependency @oxc-project/types0 B0 B
@nuxt/scripts · dependency @vueuse/core174 kB707 kB
@nuxt/scripts · dependency @vueuse/shared39 kB154 kB
@nuxt/scripts · dependency h334 kB146 kB
@nuxt/scripts · dependency magic-string9.4 kB42 kB
@nuxt/scripts · dependency oxc-walker7.6 kB31 kB
@nuxt/scripts · dependency semver25 kB72 kB
@nuxt/scripts · dependency sirv8.8 kB21 kB
@nuxt/scripts · dependency unstorage70 kB225 kB
@nuxt/scripts · dependency valibot80 kB592 kB
@nuxt/scripts · dist/runtime98 kB284 kB
@nuxt/scripts · export .25 kB105 kB
@nuxt/scripts · export ./registry28 kB89 kB
@nuxt/scripts · export ./stats13 kB89 kB
@nuxt/scripts · export ./types-source43 kB222 kB
@nuxt/scripts · published payload208 kB790 kB
@nuxt/scripts · components runtime2.4 kB6.2 kB
@nuxt/scripts · composables runtime7.5 kB24 kB
@nuxt/scripts · registry runtime42 kB123 kB
@nuxt/scripts · server runtime28 kB84 kB
@nuxt/scripts · utils runtime2.5 kB7.4 kB
Runtime dependencies (22)
PackageDependencyRequestedResolvedCost
@nuxt/scripts-climagicast^0.5.40.5.4📦 72 kB gzip
@nuxt/scripts-clipathe^2.0.32.0.3♻️ free via Nuxt 4.5.1
@nuxt/scripts@nuxt/devtools-kit^3.4.13.4.1📦 2.9 kB gzip
@nuxt/scripts@oxc-project/types^0.143.00.143.0📦 0 B gzip
@nuxt/scripts@vueuse/core^14.4.014.4.0📦 174 kB gzip
@nuxt/scripts@vueuse/shared^14.4.014.4.0📦 39 kB gzip
@nuxt/scriptsconsola^3.4.23.4.2♻️ free via Nuxt 4.5.1
@nuxt/scriptsdefu^6.1.76.1.7♻️ free via Nuxt 4.5.1
@nuxt/scriptsh3^1.15.111.15.11📦 34 kB gzip
@nuxt/scriptsmagic-string^1.1.01.1.0📦 9.4 kB gzip, Nuxt has 1.0.0
@nuxt/scriptsofetch^1.5.11.5.1♻️ free via Nuxt 4.5.1
@nuxt/scriptsohash^2.0.112.0.11♻️ free via Nuxt 4.5.1
@nuxt/scriptsoxc-walker^1.1.11.1.1📦 7.6 kB gzip, Nuxt has 1.0.0
@nuxt/scriptspathe^2.0.32.0.3♻️ free via Nuxt 4.5.1
@nuxt/scriptssemver^7.8.57.8.5📦 25 kB gzip
@nuxt/scriptssirv^3.0.23.0.2📦 8.8 kB gzip
@nuxt/scriptsstd-env^4.2.04.2.0♻️ free via Nuxt 4.5.1
@nuxt/scriptsufo^1.6.41.6.4♻️ free via Nuxt 4.5.1
@nuxt/scriptsultrahtml^1.7.01.7.0♻️ free via Nuxt 4.5.1
@nuxt/scriptsunplugin^3.3.03.3.0♻️ free via Nuxt 4.5.1
@nuxt/scriptsunstorage^1.17.51.17.5📦 70 kB gzip
@nuxt/scriptsvalibot^1.4.21.4.2📦 80 kB gzip

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

@coderabbitai

Copy link
Copy Markdown

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 nameStatusExplanation
Title check✅ PassedThe title clearly identifies the Carbon Ads fix and states the main behavior change after unmount.
Description check✅ PassedThe description explains the unmount crash, the root ref correction, the remaining ID collision, and the validation performed.
Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
Out of Scope Changes check✅ PassedCheck 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 mainAug 20, 2026
24 of 26 checks passed
@harlan-zw
harlan-zw deleted the fix/carbon-ads-unmount-id branch August 20, 2026 03:33
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.

1 participant

@harlan-zw