Skip to content

feat: build-time debug flag with script-lifecycle tracing#760

Merged
harlan-zw merged 2 commits into
mainfrom
feat/gtm-consent-debug
May 14, 2026
Merged

feat: build-time debug flag with script-lifecycle tracing#760
harlan-zw merged 2 commits into
mainfrom
feat/gtm-consent-debug

Conversation

@harlan-zw
Copy link
Copy Markdown
Collaborator

@harlan-zw harlan-zw commented May 9, 2026

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

Adds an opt-in debug surface for the script registry. A build-time __NUXT_SCRIPTS_DEBUG__ constant is injected via Vite define and Nitro replace, so debug branches DCE in production when scripts.debug: false (the default).

When scripts.debug: true, useScript traces every script's lifecycle through consola:

  • registered — fired on script registration with { src, trigger, loadedFrom }.
  • Every script:updated status change (awaitingLoad → loading → loaded, error, removed, …).
  • Explicit load() / remove() / reload() invocations.

Each log carries a { id, registryKey, src, loadedFrom, elapsedMs, loadMs } payload and is tagged with the registry key (e.g. googleTagManager) when known. All wired off the existing unhead script:updated hook — no new hook surface.

GCMv2 consent-key validation that originally rode in this branch was extracted and merged separately via #772.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 9, 2026

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

Project Deployment Actions Updated (UTC)
scripts-playground Ready Ready Preview, Comment May 14, 2026 4:22am

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 9, 2026

Open in StackBlitz

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

commit: 51de226

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 9, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds a build-time constant NUXT_SCRIPTS_DEBUG wired into Vite and Nitro, exports a runtime debugEnabled flag derived from that constant, updates the logger to enable debug-level output when the flag is true, and instruments useScript with client-side per-script debug logging (including loadedFrom metadata) guarded by debugEnabled.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description provides detailed context about the debug feature, including implementation details, logging behavior, and clarifies what was extracted to a separate PR.
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.
Title check ✅ Passed The title accurately summarizes the main changes: adding a build-time debug flag and implementing script-lifecycle tracing.

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

✨ 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 feat/gtm-consent-debug

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.

- Adds `scripts.debug` module option that injects a `__NUXT_SCRIPTS_DEBUG__`
  build-time constant via Vite `define` / Nitro `replace`. Debug branches
  DCE in production when `debug: false` (the default).
- New `runtime/debug.ts` exports `debugEnabled` (reads the constant) for
  guard checks in runtime code.
- `runtime/logger.ts` opts the consola instance up to `level: 4` when
  `debugEnabled`, so `logger.debug` calls actually fire.
- `useScript` now traces every script's lifecycle through consola when
  debug is enabled: `registered` (with src + trigger + caller), every
  `script:updated` status change (`awaitingLoad → loading → loaded`,
  `error`, `removed`, …), and explicit `load() / remove() / reload()`
  invocations. Each log carries a `{ id, registryKey, src, loadedFrom,
  elapsedMs, loadMs }` payload, tagged with the registryKey when present.
  All wired off the existing unhead `script:updated` hook (no new hook
  surface).
@harlan-zw harlan-zw force-pushed the feat/gtm-consent-debug branch from b710d2c to bb54b77 Compare May 14, 2026 04:15
@harlan-zw harlan-zw changed the title feat(gtm): debug logging and consent key validation feat(scripts): build-time debug flag with script-lifecycle tracing May 14, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/script/src/runtime/composables/useScript.ts`:
- Around line 302-306: The devtools type is missing the loadedFrom property
causing TypeScript errors when useScript.ts reads options?.devtools?.loadedFrom;
update the NuxtUseScriptOptions.devtools interface/type (the devtools shape in
types.ts) to include loadedFrom?: string (optional) so callers like useScript
(reading loadedFrom), utils (line ~152) and devtools-standalone-bridge.client
(reading loadedFrom) typecheck correctly; ensure the property is optional to
preserve backward compatibility.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6de1b090-b0cc-4030-a2ca-2905380eebc0

📥 Commits

Reviewing files that changed from the base of the PR and between b710d2c and bb54b77.

📒 Files selected for processing (4)
  • packages/script/src/module.ts
  • packages/script/src/runtime/composables/useScript.ts
  • packages/script/src/runtime/debug.ts
  • packages/script/src/runtime/logger.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/script/src/runtime/debug.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/script/src/module.ts

Comment thread packages/script/src/runtime/composables/useScript.ts
`useScript` reads `options?.devtools?.loadedFrom` for debug-log context
and `useRegistryScript`/`devtools-standalone-bridge.client` write to it,
but the field wasn't declared on the `devtools` shape — caused a
typecheck failure flagged by CodeRabbit on #760.
@harlan-zw harlan-zw changed the title feat(scripts): build-time debug flag with script-lifecycle tracing feat: build-time debug flag with script-lifecycle tracing May 14, 2026
@harlan-zw harlan-zw merged commit f6c25a5 into main May 14, 2026
19 checks passed
@harlan-zw harlan-zw deleted the feat/gtm-consent-debug branch May 14, 2026 04:26
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