// Keeps the site cache purge settings out of the public site config. // // nuxt-site-config (pulled in by @nuxtjs/seo) copies every NUXT_SITE_* and // NUXT_PUBLIC_SITE_* environment variable into the site config, camel-cased, // and Nuxt serializes the site config into every page's payload (and into // window.__NUXT_SITE_CONFIG__ on client-only pages). The purge token's // earlier name NUXT_SITE_CACHE_PURGE_TOKEN therefore reached every page as // cachePurgeToken, and NUXT_SITE_CACHE_ALLOW_PROXIED as cacheAllowProxied. // The settings now live under NUXT_CACHE_PURGE_TOKEN and // NUXT_CACHE_PURGE_ALLOW_PROXIED, outside that prefix. The old names keep // working for one release, so this plugin also removes the two keys from // every entry of the site config stack on nuxt-site-config's // `site-config:init` hook, before anything reads it. The stripping stays // with the new names too: a site that still sets an old variable, or sets // both, must not publish it. // // When a setting comes from an old name, the plugin logs one deprecation // warning per process (globalThis[SITE_PURGE_WARNED], so a site extending // several layers that ship this file warns once). The warning names // variables, never a value. // // Without nuxt-site-config the hook is never called and the plugin does // nothing else. Shared: several layers ship this file byte for byte; // running it more than once is harmless. import { SITE_PURGE_WARNED, redactSiteConfigStack, resolveSitePurgeConfig, sitePurgeDeprecationMessage } from '../lib/siteCachePurge.ts' interface SiteConfigInitContext { siteConfig?: { stack?: unknown } } type LooseHooks = { hook: (name: string, fn: (ctx: SiteConfigInitContext | undefined) => void) => unknown } export default defineNitroPlugin((nitroApp) => { const g = globalThis as Record if (!g[SITE_PURGE_WARNED]) { const message = sitePurgeDeprecationMessage(resolveSitePurgeConfig(useRuntimeConfig()).deprecated) if (message) { g[SITE_PURGE_WARNED] = true console.warn(message) } } // The hook name is untyped in an app without nuxt-site-config. ;(nitroApp.hooks as unknown as LooseHooks).hook('site-config:init', (ctx) => { redactSiteConfigStack(ctx?.siteConfig?.stack) }) })