Skip to content

Commit 1db6fc1

Browse files
authored
feat: auto-populate runtimeConfig for .env registry script overrides (#634)
1 parent 9431643 commit 1db6fc1

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/module.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ function fixSelfClosingScriptComponents(nuxt: any) {
159159
}
160160
}
161161

162+
const REGISTRY_ENV_DEFAULTS: Record<string, Record<string, string>> = {
163+
clarity: { id: '' },
164+
cloudflareWebAnalytics: { token: '' },
165+
crisp: { id: '' },
166+
databuddyAnalytics: { clientId: '' },
167+
fathomAnalytics: { site: '' },
168+
googleAdsense: { client: '' },
169+
googleAnalytics: { id: '' },
170+
googleMaps: { apiKey: '' },
171+
googleRecaptcha: { siteKey: '' },
172+
googleSignIn: { clientId: '' },
173+
googleTagManager: { id: '' },
174+
hotjar: { id: '' },
175+
intercom: { app_id: '' },
176+
matomoAnalytics: { matomoUrl: '' },
177+
metaPixel: { id: '' },
178+
paypal: { clientId: '' },
179+
plausibleAnalytics: { domain: '' },
180+
posthog: { apiKey: '' },
181+
redditPixel: { id: '' },
182+
rybbitAnalytics: { siteId: '' },
183+
segment: { writeKey: '' },
184+
snapchatPixel: { id: '' },
185+
stripe: {},
186+
tiktokPixel: { id: '' },
187+
umamiAnalytics: { websiteId: '' },
188+
vercelAnalytics: {},
189+
xPixel: { id: '' },
190+
}
191+
162192
const PARTYTOWN_FORWARDS: Record<string, string[]> = {
163193
googleAnalytics: ['dataLayer.push', 'gtag'],
164194
plausible: ['plausible'],
@@ -355,12 +385,35 @@ export default defineNuxtModule<ModuleOptions>({
355385
// Merge registry config with existing runtimeConfig.public.scripts for proper env var resolution
356386
// Both scripts.registry and runtimeConfig.public.scripts should be supported
357387
if (config.registry) {
358-
// Ensure runtimeConfig.public exists
359388
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {}
360389

390+
// Auto-populate env var defaults for enabled registry scripts so that
391+
// NUXT_PUBLIC_SCRIPTS_<SCRIPT>_<KEY> works without manual runtimeConfig
392+
const registryWithDefaults: Record<string, any> = {}
393+
for (const [key, value] of Object.entries(config.registry)) {
394+
if (value && REGISTRY_ENV_DEFAULTS[key]) {
395+
const envDefaults = REGISTRY_ENV_DEFAULTS[key]
396+
if (value === true || value === 'mock') {
397+
registryWithDefaults[key] = { ...envDefaults }
398+
}
399+
else if (typeof value === 'object' && !Array.isArray(value)) {
400+
registryWithDefaults[key] = defu(value, envDefaults)
401+
}
402+
else if (Array.isArray(value)) {
403+
registryWithDefaults[key] = defu(value[0] || {}, envDefaults)
404+
}
405+
else {
406+
registryWithDefaults[key] = value
407+
}
408+
}
409+
else {
410+
registryWithDefaults[key] = value
411+
}
412+
}
413+
361414
nuxt.options.runtimeConfig.public.scripts = defu(
362415
nuxt.options.runtimeConfig.public.scripts || {},
363-
config.registry,
416+
registryWithDefaults,
364417
)
365418
}
366419

0 commit comments

Comments
 (0)