From d8abcbf74b74869de27a587456a13d4cdf96bcd7 Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Thu, 30 Apr 2026 19:07:42 +0200 Subject: [PATCH] refactor(hooks): move server bootstrap into ServerInit hook Module-level top-level await for db/scheduler init and the cache warmup IIFE move into the canonical export const init hook. Same ordering and non-blocking semantics; makes the lifecycle explicit and works on environments without top-level await. --- package.json | 2 +- src/hooks.server.ts | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index dd760600..dee9ce63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homepage", - "version": "1.52.1", + "version": "1.52.2", "private": true, "type": "module", "scripts": { diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 43d711a7..ab95832f 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,4 +1,4 @@ -import type { Handle, HandleServerError } from "@sveltejs/kit" +import type { Handle, HandleServerError, ServerInit } from "@sveltejs/kit" import { redirect } from "@sveltejs/kit" import { sequence } from "@sveltejs/kit/hooks" import * as auth from "./auth" @@ -32,27 +32,26 @@ async function timing({ event, resolve }: Parameters[0]) { return response; } -// Initialize database connection on server startup -console.log('🚀 Server starting - initializing database connection...'); -await dbConnect().then(() => { - console.log('✅ Database connected successfully'); - // Initialize the recurring payment scheduler after DB is ready - initializeScheduler(); - console.log('✅ Recurring payment scheduler initialized'); -}).catch((error) => { - console.error('❌ Failed to connect to database on startup:', error); - // Don't crash the server - API routes will attempt reconnection -}); +export const init: ServerInit = async () => { + console.log('🚀 Server starting - initializing database connection...'); + try { + await dbConnect(); + console.log('✅ Database connected successfully'); + initializeScheduler(); + console.log('✅ Recurring payment scheduler initialized'); + } catch (error) { + console.error('❌ Failed to connect to database on startup:', error); + // Don't crash the server - API routes will attempt reconnection + } -// Warm liturgical calendar cache in the background — non-blocking so the -// server starts accepting requests immediately; any request arriving before -// warmup completes falls back to lazy computation (still correct, just cold). -{ + // Warm liturgical calendar cache in the background — non-blocking so the + // server starts accepting requests immediately; any request arriving before + // warmup completes falls back to lazy computation (still correct, just cold). const t0 = performance.now(); warmLiturgicalCache() .then(() => console.log(`✅ Liturgical calendar cache warmed in ${Math.round(performance.now() - t0)}ms`)) .catch((error) => console.error('⚠️ Liturgical calendar warmup failed:', error)); -} +}; async function authorization({ event, resolve }: Parameters[0]) { const session = await event.locals.timing.measure('auth', () => event.locals.auth());