feat: build-time debug flag with script-lifecycle tracing#760
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
📝 WalkthroughWalkthroughThis 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)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
- 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).
b710d2c to
bb54b77
Compare
There was a problem hiding this comment.
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
📒 Files selected for processing (4)
packages/script/src/module.tspackages/script/src/runtime/composables/useScript.tspackages/script/src/runtime/debug.tspackages/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
`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.
❓ Type of change
📚 Description
Adds an opt-in debug surface for the script registry. A build-time
__NUXT_SCRIPTS_DEBUG__constant is injected via Vitedefineand Nitroreplace, so debug branches DCE in production whenscripts.debug: false(the default).When
scripts.debug: true,useScripttraces every script's lifecycle through consola:registered— fired on script registration with{ src, trigger, loadedFrom }.script:updatedstatus change (awaitingLoad → loading → loaded,error,removed, …).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 unheadscript:updatedhook — no new hook surface.GCMv2 consent-key validation that originally rode in this branch was extracted and merged separately via #772.