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.
This commit is contained in:
2026-04-30 19:07:42 +02:00
parent 4ad218cc39
commit d8abcbf74b
2 changed files with 17 additions and 18 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "homepage",
"version": "1.52.1",
"version": "1.52.2",
"private": true,
"type": "module",
"scripts": {
+16 -17
View File
@@ -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<Handle>[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<Handle>[0]) {
const session = await event.locals.timing.measure('auth', () => event.locals.auth());