UNPKG

nuxt-site-config

Version:

Shared site configuration for Nuxt 3 modules.

20 lines (19 loc) 1.04 kB
import devalue from "@nuxt/devalue"; import { defineNitroPlugin, getRouteRules } from "nitropack/runtime"; import { toValue } from "vue"; import { useSiteConfig } from "../composables/useSiteConfig.js"; const PRERENDER_NO_SSR_ROUTES = /* @__PURE__ */ new Set(["/index.html", "/200.html", "/404.html"]); export default defineNitroPlugin(async (nitroApp) => { nitroApp.hooks.hook("render:html", async (ctx, { event }) => { const routeOptions = getRouteRules(event); const isIsland = process.env.NUXT_COMPONENT_ISLANDS && event.path.startsWith("/__nuxt_island"); const url = event.path; const noSSR = !!process.env.NUXT_NO_SSR || event.context.nuxt?.noSSR || routeOptions.ssr === false && !isIsland || (import.meta.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false); if (noSSR) { const siteConfig = Object.fromEntries( Object.entries(useSiteConfig(event)).map(([k, v]) => [k, toValue(v)]) ); ctx.body.push(`<script>window.__NUXT_SITE_CONFIG__=${devalue(siteConfig)}<\/script>`); } }); });