feat: auto-populate runtimeConfig for .env registry script overrides#634
feat: auto-populate runtimeConfig for .env registry script overrides#634
Conversation
…v overrides When a registry script is enabled (e.g., `googleAnalytics: true`), automatically populate `runtimeConfig.public.scripts.<key>` with empty-string defaults for the script's primary config keys. This allows env vars like `NUXT_PUBLIC_SCRIPTS_GOOGLE_ANALYTICS_ID` to work without manually declaring the runtimeConfig boilerplate. Closes #239
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
📝 WalkthroughWalkthroughAdds a REGISTRY_ENV_DEFAULTS mapping and new logic that builds a registryWithDefaults by iterating config.registry and applying per-script defaults. The code handles boolean, 'mock', object, and array registry values to produce normalized script entries, then merges registryWithDefaults into nuxt.options.runtimeConfig.public.scripts (replacing direct use of config.registry). Comments about ensuring runtimeConfig.public were removed and replaced by this defaulting flow so NUXT_PUBLIC_SCRIPTS_<SCRIPT>_ env vars can merge with generated defaults. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
src/module.ts (1)
162-190: Avoid a second source of truth for env-populated keys.This table duplicates per-script input knowledge that already lives in the registry/types, so new scripts can silently miss env auto-provisioning unless both places are updated. Consider co-locating the env-key metadata with each registry entry or schema and generating this map from that metadata.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/module.ts` around lines 162 - 190, REGISTRY_ENV_DEFAULTS duplicates env-key metadata and should be generated from the registry/type definitions instead of being a separate source; update each registry entry (the registry export or type objects used to register scripts) to include a small envDefaults (or envKeys) metadata field, then replace the hard-coded REGISTRY_ENV_DEFAULTS with a computed map derived from those registry entries (use the existing registry export/registry types to iterate and build the same shape); ensure the symbol REGISTRY_ENV_DEFAULTS is removed or replaced and any consumers read the generated map so new scripts only need to declare env metadata once.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/module.ts`:
- Around line 390-417: The registry defaults are being mirrored from the
pre-normalized config so later syntheses (e.g., config.partytown adding entries)
and consumers (like googleStaticMapsProxy.apiKey reading
public.scripts.googleMaps) miss injected env defaults; instead, compute and use
the final normalized registry first and then derive registryWithDefaults and
nuxt.options.runtimeConfig.public.scripts from that normalized shape.
Concretely: run the registry normalization step that produces the final registry
(the same normalization used later for config.partytown) before the loop that
builds registryWithDefaults (referencing config.registry, REGISTRY_ENV_DEFAULTS
and registryWithDefaults), then apply the env-default-merging logic against that
normalized registry and finally set nuxt.options.runtimeConfig.public.scripts so
later reads (e.g., googleStaticMapsProxy.apiKey and code that inspects
config.partytown) see the fully synthesized defaults.
- Around line 402-403: The current assignment writes the full tuple into runtime
config (registryWithDefaults[key] = [defu(...), value[1]]), which nests the real
script fields under index 0 and breaks runtimeConfig.public.scripts reads in
useRegistryScript(); instead, mirror only the flattened script object into the
runtime config and keep the tuple's options in the registry config. Change the
assignment so registryWithDefaults[key] is set to defu(value[0] || {},
envDefaults) (only the script object) and ensure the tuple's second element
(value[1]) remains stored in config.registry (or the existing registry
structure) rather than being placed into runtimeConfig.public.scripts.
---
Nitpick comments:
In `@src/module.ts`:
- Around line 162-190: REGISTRY_ENV_DEFAULTS duplicates env-key metadata and
should be generated from the registry/type definitions instead of being a
separate source; update each registry entry (the registry export or type objects
used to register scripts) to include a small envDefaults (or envKeys) metadata
field, then replace the hard-coded REGISTRY_ENV_DEFAULTS with a computed map
derived from those registry entries (use the existing registry export/registry
types to iterate and build the same shape); ensure the symbol
REGISTRY_ENV_DEFAULTS is removed or replaced and any consumers read the
generated map so new scripts only need to declare env metadata once.
| // Auto-populate env var defaults for enabled registry scripts so that | ||
| // NUXT_PUBLIC_SCRIPTS_<SCRIPT>_<KEY> works without manual runtimeConfig | ||
| const registryWithDefaults: Record<string, any> = {} | ||
| for (const [key, value] of Object.entries(config.registry)) { | ||
| if (value && REGISTRY_ENV_DEFAULTS[key]) { | ||
| const envDefaults = REGISTRY_ENV_DEFAULTS[key] | ||
| if (value === true || value === 'mock') { | ||
| registryWithDefaults[key] = { ...envDefaults } | ||
| } | ||
| else if (typeof value === 'object' && !Array.isArray(value)) { | ||
| registryWithDefaults[key] = defu(value, envDefaults) | ||
| } | ||
| else if (Array.isArray(value)) { | ||
| registryWithDefaults[key] = [defu(value[0] || {}, envDefaults), value[1]] | ||
| } | ||
| else { | ||
| registryWithDefaults[key] = value | ||
| } | ||
| } | ||
| else { | ||
| registryWithDefaults[key] = value | ||
| } | ||
| } | ||
|
|
||
| nuxt.options.runtimeConfig.public.scripts = defu( | ||
| nuxt.options.runtimeConfig.public.scripts || {}, | ||
| config.registry, | ||
| registryWithDefaults, | ||
| ) |
There was a problem hiding this comment.
Normalize the registry before mirroring it into runtime config.
This block runs before config.partytown can synthesize extra registry entries (Lines 443-476), and googleStaticMapsProxy.apiKey has already been read from public.scripts.googleMaps above (Lines 371-372). As a result, partytown: ['googleAnalytics'] still misses the new env defaults, and googleMaps: true still leaves the static-maps proxy without an API key unless the old runtimeConfig boilerplate is present. Please derive both runtime-config objects from the final normalized registry shape.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/module.ts` around lines 390 - 417, The registry defaults are being
mirrored from the pre-normalized config so later syntheses (e.g.,
config.partytown adding entries) and consumers (like
googleStaticMapsProxy.apiKey reading public.scripts.googleMaps) miss injected
env defaults; instead, compute and use the final normalized registry first and
then derive registryWithDefaults and nuxt.options.runtimeConfig.public.scripts
from that normalized shape. Concretely: run the registry normalization step that
produces the final registry (the same normalization used later for
config.partytown) before the loop that builds registryWithDefaults (referencing
config.registry, REGISTRY_ENV_DEFAULTS and registryWithDefaults), then apply the
env-default-merging logic against that normalized registry and finally set
nuxt.options.runtimeConfig.public.scripts so later reads (e.g.,
googleStaticMapsProxy.apiKey and code that inspects config.partytown) see the
fully synthesized defaults.
… to runtimeConfig
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/module.ts (1)
390-417:⚠️ Potential issue | 🟠 MajorOrdering issue: defaults are computed before partytown synthesis and after
googleStaticMapsProxy.apiKeyis read.This block still runs:
- After Line 372 reads
googleStaticMapsProxy.apiKeyfromruntimeConfig.public.scripts?.googleMaps?.apiKey— so if a user has onlygoogleMaps: truein registry (relying on.envfor the API key), the proxy config won't receive the key.- Before Lines 443-476 where
config.partytowncan inject additional registry entries — those entries won't receive env defaults.Proposed fix: move the defaults logic after partytown processing and re-read proxy config
Move the
registryWithDefaultscomputation to after the partytown block (after Line 487), then either:
- Re-assign
googleStaticMapsProxy.apiKeyafter the merge, or- Defer reading
googleStaticMapsProxy.apiKeyuntil after defaults are merged// After Line 487 (end of partytown block): + // Auto-populate env var defaults for enabled registry scripts + if (config.registry) { + nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {} + const registryWithDefaults: Record<string, any> = {} + for (const [key, value] of Object.entries(config.registry)) { + // ... same logic ... + } + nuxt.options.runtimeConfig.public.scripts = defu( + nuxt.options.runtimeConfig.public.scripts || {}, + registryWithDefaults, + ) + // Re-assign googleStaticMapsProxy.apiKey after env defaults are merged + if (config.googleStaticMapsProxy?.enabled) { + (nuxt.options.runtimeConfig['nuxt-scripts'] as any).googleStaticMapsProxy = { + apiKey: (nuxt.options.runtimeConfig.public.scripts as any)?.googleMaps?.apiKey, + } + } + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/module.ts` around lines 390 - 417, The defaults merge for registry env vars (registryWithDefaults using REGISTRY_ENV_DEFAULTS and nuxt.options.runtimeConfig.public.scripts = defu(...)) runs in the wrong place — move the entire registryWithDefaults computation to after the partytown processing that mutates config.partytown (the block that injects registry entries), then recompute/re-read any proxy config values that were read earlier (e.g., googleStaticMapsProxy.apiKey from runtimeConfig.public.scripts?.googleMaps?.apiKey) so the proxy picks up env-defaulted values; alternatively defer reading googleStaticMapsProxy.apiKey until after you call defu into nuxt.options.runtimeConfig.public.scripts.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@src/module.ts`:
- Around line 390-417: The defaults merge for registry env vars
(registryWithDefaults using REGISTRY_ENV_DEFAULTS and
nuxt.options.runtimeConfig.public.scripts = defu(...)) runs in the wrong place —
move the entire registryWithDefaults computation to after the partytown
processing that mutates config.partytown (the block that injects registry
entries), then recompute/re-read any proxy config values that were read earlier
(e.g., googleStaticMapsProxy.apiKey from
runtimeConfig.public.scripts?.googleMaps?.apiKey) so the proxy picks up
env-defaulted values; alternatively defer reading googleStaticMapsProxy.apiKey
until after you call defu into nuxt.options.runtimeConfig.public.scripts.
🔗 Linked issue
Resolves #239
❓ Type of change
📚 Description
Users had to manually define verbose
runtimeConfig.public.scriptsentries for .env overrides to work with registry scripts. The module now auto-populates runtimeConfig defaults (empty string keys) when a registry script is enabled, soNUXT_PUBLIC_SCRIPTS_GOOGLE_ANALYTICS_IDworks without any runtimeConfig boilerplate.