import { getTranslationBundleBySlug } from '$lib/cms'; import { TRANSLATION_BUNDLE_SLUG } from '$lib/constants'; import type { Handle } from '@sveltejs/kit'; /** * Page-Routen: kurze Edge-Cache + SWR. Webhook purged App-Cache, * Edge läuft via stale-while-revalidate auf den warmen App-Cache. */ const PAGE_CACHE_CONTROL = 'public, max-age=0, s-maxage=30, stale-while-revalidate=300'; function isCacheablePath(pathname: string): boolean { if (pathname.startsWith('/api/')) return false; if (pathname.startsWith('/cms-images/')) return false; if (pathname.startsWith('/_app/')) return false; return true; } export const handle: Handle = async ({ event, resolve }) => { try { const bundle = await getTranslationBundleBySlug(TRANSLATION_BUNDLE_SLUG, { locale: 'de' }); if (bundle?.strings && typeof bundle.strings === 'object') { const normalized: Record = {}; for (const [key, val] of Object.entries(bundle.strings)) { const v = val as unknown; const str = typeof v === 'string' ? v : typeof v === 'object' && v !== null && 'value' in v ? (v as { value?: unknown }).value : (v as { text?: unknown })?.text ?? (v as { label?: unknown })?.label; if (typeof str === 'string') normalized[key] = str; } event.locals.translations = normalized; } else { event.locals.translations = {}; } } catch { event.locals.translations = {}; } const response = await resolve(event); if ( event.request.method === 'GET' && isCacheablePath(event.url.pathname) && !response.headers.has('cache-control') ) { response.headers.set('cache-control', PAGE_CACHE_CONTROL); } return response; };