Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ function fixSelfClosingScriptComponents(nuxt: any) {
}
}

const REGISTRY_ENV_DEFAULTS: Record<string, Record<string, string>> = {
clarity: { id: '' },
cloudflareWebAnalytics: { token: '' },
crisp: { id: '' },
databuddyAnalytics: { clientId: '' },
fathomAnalytics: { site: '' },
googleAdsense: { client: '' },
googleAnalytics: { id: '' },
googleMaps: { apiKey: '' },
googleRecaptcha: { siteKey: '' },
googleSignIn: { clientId: '' },
googleTagManager: { id: '' },
hotjar: { id: '' },
intercom: { app_id: '' },
matomoAnalytics: { matomoUrl: '' },
metaPixel: { id: '' },
paypal: { clientId: '' },
plausibleAnalytics: { domain: '' },
posthog: { apiKey: '' },
redditPixel: { id: '' },
rybbitAnalytics: { siteId: '' },
segment: { writeKey: '' },
snapchatPixel: { id: '' },
stripe: {},
tiktokPixel: { id: '' },
umamiAnalytics: { websiteId: '' },
vercelAnalytics: {},
xPixel: { id: '' },
}

const PARTYTOWN_FORWARDS: Record<string, string[]> = {
googleAnalytics: ['dataLayer.push', 'gtag'],
plausible: ['plausible'],
Expand Down Expand Up @@ -355,12 +385,35 @@ export default defineNuxtModule<ModuleOptions>({
// Merge registry config with existing runtimeConfig.public.scripts for proper env var resolution
// Both scripts.registry and runtimeConfig.public.scripts should be supported
if (config.registry) {
// Ensure runtimeConfig.public exists
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {}

// 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)
}
else {
registryWithDefaults[key] = value
}
}
else {
registryWithDefaults[key] = value
}
}

nuxt.options.runtimeConfig.public.scripts = defu(
nuxt.options.runtimeConfig.public.scripts || {},
config.registry,
registryWithDefaults,
)
Comment on lines +390 to 417
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

}

Expand Down
Loading