Guides
Vite Feature HMR
Reload Telegram commands and callbacks live without disconnecting long polling or database pools.
Restarting a Telegram bot process on every edit drops active long-polling connections, tears down database clients, and slows down iteration. Use Botify separates the long-lived bot runtime from hot-swappable feature middleware.
How It Works
createBot(@use-botify/core) boots the grammY instance, structured logger, and graceful shutdown hooks once.createHmrDispatcher(@use-botify/core/middleware) mounts a thin, mutable middleware proxy on the bot.startFeatureHmr(@use-botify/tooling/hmr) runs a Vite SSR module loader watchingfeatures/with your configured TypeScript path alias (@/).- Whenever you save a feature file, Vite invalidates only the changed module graph,
createFsRouterbuilds a freshComposer, andhmr.replace(router.composer)swaps it atomically in memory.
Bootstrap Pattern (src/main.ts)
import path from "node:path";
import process from "node:process";
import { createBot } from "@use-botify/core";
import { createHmrDispatcher } from "@use-botify/core/middleware";
import { createFsRouter } from "@use-botify/router/fs";
import { env } from "@/env";
async function bootstrap(): Promise<void> {
const app = createBot({ token: env.BOT_TOKEN });
const sourceDir = import.meta.dirname;
const featuresDir = path.resolve(sourceDir, "features");
if (env.NODE_ENV === "development") {
const hmr = createHmrDispatcher({ logger: app.logger });
app.use(hmr.middleware);
const [{ startFeatureHmr }, { writeRouteTypes }] = await Promise.all([
import("@use-botify/tooling/hmr"),
import("@use-botify/tooling/codegen"),
]);
const watcher = await startFeatureHmr({
projectRoot: process.cwd(),
sourceDir,
featuresDir,
aliasPrefix: "@/",
logger: app.logger,
onReload: async ({ loadModule }) => {
const router = await createFsRouter({
featuresDir,
loadModule,
logger: app.logger,
});
hmr.replace(router.composer);
await writeRouteTypes({
featuresDir,
outputPath: path.resolve(process.cwd(), "src/generated/routes.d.ts"),
logger: app.logger,
});
},
});
app.onShutdown(() => watcher.close());
} else {
const router = await createFsRouter({
featuresDir,
logger: app.logger,
});
app.use(router.composer);
}
await app.start();
}
await bootstrap();Fault Tolerance & Production Isolation
- Reload error survival — If a file edit introduces a syntax or runtime import error during reload,
startFeatureHmrlogs the failure and keeps the previous workingComposeractive until your next save. - Zero Vite in production — Because
@use-botify/tooling/hmris imported dynamically insideif (env.NODE_ENV === "development"), production deployments never load Vite or file watchers. - Clean shutdown —
app.onShutdown(() => watcher.close())ensuresSIGINT/SIGTERMcloses both Telegram polling and the Vite server cleanly.