From ce48b26a8813588e2d7ff4efe5d6e3a3fb63110d Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 6 Mar 2026 02:26:21 +1100 Subject: [PATCH] refactor: auto-enable image proxy handlers from registry config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove separate `googleStaticMapsProxy` module option — the handler is now registered automatically when `registry.googleMaps` is set. The API key and cacheMaxAge are injected into runtime config from the registry entry, removing the need for explicit opt-in. --- src/module.ts | 43 ++++++------------- .../GoogleMaps/ScriptGoogleMaps.vue | 2 +- .../server/google-static-maps-proxy.ts | 7 --- 3 files changed, 13 insertions(+), 39 deletions(-) diff --git a/src/module.ts b/src/module.ts index 4ea01480..5bbd93d9 100644 --- a/src/module.ts +++ b/src/module.ts @@ -256,22 +256,6 @@ export interface ModuleOptions { */ integrity?: boolean | 'sha256' | 'sha384' | 'sha512' } - /** - * Google Static Maps proxy configuration. - * Proxies static map images through your server to fix CORS issues and enable caching. - */ - googleStaticMapsProxy?: { - /** - * Enable proxying Google Static Maps through your own origin. - * @default false - */ - enabled?: boolean - /** - * Cache duration for static map images in seconds. - * @default 3600 (1 hour) - */ - cacheMaxAge?: number - } /** * Whether the module is enabled. * @@ -310,10 +294,6 @@ export default defineNuxtModule({ timeout: 15_000, // Configures the maximum time (in milliseconds) allowed for each fetch attempt. }, }, - googleStaticMapsProxy: { - enabled: false, - cacheMaxAge: 3600, - }, enabled: true, debug: false, }, @@ -337,20 +317,12 @@ export default defineNuxtModule({ } nuxt.options.runtimeConfig['nuxt-scripts'] = { version: version!, - // Private proxy config with API key (server-side only) - googleStaticMapsProxy: config.googleStaticMapsProxy?.enabled - ? { apiKey: (nuxt.options.runtimeConfig.public.scripts as any)?.googleMaps?.apiKey } - : undefined, } as any nuxt.options.runtimeConfig.public['nuxt-scripts'] = { // expose for devtools version: nuxt.options.dev ? version : undefined, defaultScriptOptions: config.defaultScriptOptions as any, - // Only expose enabled and cacheMaxAge to client, not apiKey - googleStaticMapsProxy: config.googleStaticMapsProxy?.enabled - ? { enabled: true, cacheMaxAge: config.googleStaticMapsProxy.cacheMaxAge } - : undefined, - } + } as any // Merge registry config with existing runtimeConfig.public.scripts for proper env var resolution // Both scripts.registry and runtimeConfig.public.scripts should be supported @@ -695,8 +667,17 @@ export default defineNuxtModule({ }) }) - // Add Google Static Maps proxy handler if enabled - if (config.googleStaticMapsProxy?.enabled) { + // Add Google Static Maps proxy handler when registry.googleMaps is enabled + if (config.registry?.googleMaps) { + const apiKey = (nuxt.options.runtimeConfig.public.scripts as any)?.googleMaps?.apiKey + nuxt.options.runtimeConfig['nuxt-scripts'] = defu( + { googleStaticMapsProxy: { apiKey } }, + nuxt.options.runtimeConfig['nuxt-scripts'] as any, + ) as any + nuxt.options.runtimeConfig.public['nuxt-scripts'] = defu( + { googleStaticMapsProxy: { cacheMaxAge: 3600 } }, + nuxt.options.runtimeConfig.public['nuxt-scripts'] as any, + ) as any addServerHandler({ route: '/_scripts/google-static-maps-proxy', handler: await resolvePath('./runtime/server/google-static-maps-proxy'), diff --git a/src/runtime/components/GoogleMaps/ScriptGoogleMaps.vue b/src/runtime/components/GoogleMaps/ScriptGoogleMaps.vue index f1c89bc3..a74f406c 100644 --- a/src/runtime/components/GoogleMaps/ScriptGoogleMaps.vue +++ b/src/runtime/components/GoogleMaps/ScriptGoogleMaps.vue @@ -477,7 +477,7 @@ const placeholder = computed(() => { .join('|'), }) - const baseUrl = proxyConfig?.enabled + const baseUrl = proxyConfig ? '/_scripts/google-static-maps-proxy' : 'https://maps.googleapis.com/maps/api/staticmap' diff --git a/src/runtime/server/google-static-maps-proxy.ts b/src/runtime/server/google-static-maps-proxy.ts index d85a2ff6..943fcc8c 100644 --- a/src/runtime/server/google-static-maps-proxy.ts +++ b/src/runtime/server/google-static-maps-proxy.ts @@ -8,13 +8,6 @@ export default defineEventHandler(async (event) => { const publicConfig = (runtimeConfig.public['nuxt-scripts'] as any)?.googleStaticMapsProxy const privateConfig = (runtimeConfig['nuxt-scripts'] as any)?.googleStaticMapsProxy - if (!publicConfig?.enabled) { - throw createError({ - statusCode: 404, - statusMessage: 'Google Static Maps proxy is not enabled', - }) - } - // Get API key from private config (server-side only, not exposed to client) const apiKey = privateConfig?.apiKey if (!apiKey) {