refactor: add nitro bootstrap plugin to ensure app is started before first call
Some checks failed
Release Workflow / determine-release-type (push) Has been cancelled
Release Workflow / checks (push) Has been cancelled
Release Workflow / e2e-tests (push) Has been cancelled
Release Workflow / build-images (push) Has been cancelled
Release Workflow / publish-release (push) Has been cancelled

This commit is contained in:
Nicolas Meienberger 2026-02-13 20:20:16 +01:00
parent 1fea2a729a
commit 436a63a4b4
4 changed files with 43 additions and 11 deletions

View file

@ -1,24 +1,16 @@
import * as schema from "./server/db/schema";
import { setSchema, runDbMigrations } from "./server/db/db";
import { startup } from "./server/modules/lifecycle/startup";
import { logger } from "./server/utils/logger";
import { shutdown } from "./server/modules/lifecycle/shutdown";
import { runCLI } from "./server/cli";
import { runMigrations } from "./server/modules/lifecycle/migrations";
import { bootstrapApplication } from "./server/modules/lifecycle/bootstrap";
import { createStartHandler, defaultStreamHandler, defineHandlerCallback } from "@tanstack/react-start/server";
import { createServerEntry } from "@tanstack/react-start/server-entry";
setSchema(schema);
const cliRun = await runCLI(Bun.argv);
if (cliRun) {
process.exit(0);
}
runDbMigrations();
await runMigrations();
await startup();
await bootstrapApplication();
const customHandler = defineHandlerCallback((ctx) => {
return defaultStreamHandler(ctx);

View file

@ -0,0 +1,26 @@
import * as schema from "../../db/schema";
import { runDbMigrations, setSchema } from "../../db/db";
import { runMigrations } from "./migrations";
import { startup } from "./startup";
let bootstrapPromise: Promise<void> | undefined;
const runBootstrap = async () => {
setSchema(schema);
runDbMigrations();
await runMigrations();
await startup();
};
export const bootstrapApplication = async () => {
if (!bootstrapPromise) {
bootstrapPromise = runBootstrap();
}
try {
await bootstrapPromise;
} catch (err) {
bootstrapPromise = undefined;
throw err;
}
};

View file

@ -0,0 +1,11 @@
import { definePlugin } from "nitro";
import { bootstrapApplication } from "../modules/lifecycle/bootstrap";
import { logger } from "../utils/logger";
import { toMessage } from "../utils/errors";
export default definePlugin(() => {
void bootstrapApplication().catch((err) => {
logger.error(`Bootstrap failed: ${toMessage(err)}`);
process.exit(1);
});
});

View file

@ -14,7 +14,10 @@ export default defineConfig({
routesDirectory: "routes",
},
}),
nitro({ preset: "bun" }),
nitro({
preset: "bun",
plugins: ["./app/server/plugins/bootstrap.ts"],
}),
viteReact({
babel: {
plugins: ["babel-plugin-react-compiler"],