Bake PUBLIC_SSR into the build instead of reading it at runtime

This commit is contained in:
dtonon 2026-08-18 22:31:56 +01:00
parent 3b79271874
commit 7f0d823d6c
4 changed files with 20 additions and 7 deletions

3
src/app.d.ts vendored
View file

@ -9,6 +9,9 @@ import type {
import type { ThreadDetail } from "$lib/forum/thread";
declare global {
// Injected by vite.config.ts from PUBLIC_SSR
const __SQUALK_SSR__: boolean;
namespace App {
// interface Error {}
// interface Locals {}

View file

@ -18,9 +18,9 @@ export const GROUP_ID = env.PUBLIC_GROUP_ID ?? "";
if (MODE === "simple" && !GROUP_ID) {
throw new Error("PUBLIC_GROUP_ID is required in simple mode");
}
// Server-side rendering: opt-in, and only meaningful when the build targets the
// Node adapter (svelte.config.js reads the same variable).
export const SSR_ENABLED = env.PUBLIC_SSR === "yes";
// Server-side rendering: opt-in, decided at build time (vite.config.ts bakes
// PUBLIC_SSR in; svelte.config.js picks the matching adapter).
export const SSR_ENABLED = __SQUALK_SSR__;
// Server-rendered pages and snapshots are anonymous, so a shared cache may
// hold them: fresh for 5 minutes, served stale for an hour while revalidating.
// Browsers always revalidate (max-age=0) so a login shows its content at once.

View file

@ -3,8 +3,8 @@ import adapterNode from "@sveltejs/adapter-node";
import adapterStatic from "@sveltejs/adapter-static";
// PUBLIC_SSR picks the deployment target: a Node server that renders pages
// (yes) or a static single-page bundle (no, the default). The same flag turns
// SSR on in +layout.ts.
// (yes) or a static single-page bundle (no, the default). vite.config.ts bakes
// the same flag into the app as SSR_ENABLED.
const env = loadEnv(
process.env.NODE_ENV ?? "production",
process.cwd(),

View file

@ -1,5 +1,15 @@
import tailwindcss from "@tailwindcss/vite";
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import { defineConfig, loadEnv } from "vite";
export default defineConfig({ plugins: [tailwindcss(), sveltekit()] });
// PUBLIC_SSR is a build-time decision (it also picks the adapter in
// svelte.config.js), so it is compiled in as a constant rather than read from
// the environment when the server starts.
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "PUBLIC_");
const ssr = (process.env.PUBLIC_SSR ?? env.PUBLIC_SSR) === "yes";
return {
plugins: [tailwindcss(), sveltekit()],
define: { __SQUALK_SSR__: JSON.stringify(ssr) },
};
});