react-router-hono-server 2.24.1 → 2.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -664,6 +664,20 @@ export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerO
664
664
  * {@link https://bun.sh/docs/api/http#start-a-server-bun-serve}
665
665
  */
666
666
  customBunServer?: Serve.Options<unknown, string>;
667
+ /**
668
+ * Callback executed after server has closed and all inflight requests completed,
669
+ * before process exit. Only applicable in production mode.
670
+ *
671
+ * @example
672
+ * ```ts
673
+ * export default createHonoServer({
674
+ * onGracefulShutdown: async () => {
675
+ * await db.close();
676
+ * },
677
+ * });
678
+ * ```
679
+ */
680
+ onGracefulShutdown?: () => Promise<void> | void;
667
681
  /**
668
682
  * Customize the serve static options
669
683
  */
@@ -15,6 +15,20 @@ interface HonoBunServerOptions<E extends Env = BlankEnv> extends HonoServerOptio
15
15
  * {@link https://bun.sh/docs/api/http#start-a-server-bun-serve}
16
16
  */
17
17
  customBunServer?: Partial<CustomBunServer>;
18
+ /**
19
+ * Callback executed after server has closed and all inflight requests completed,
20
+ * before process exit. Only applicable in production mode.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * export default createHonoServer({
25
+ * onGracefulShutdown: async () => {
26
+ * await db.close();
27
+ * },
28
+ * });
29
+ * ```
30
+ */
31
+ onGracefulShutdown?: () => Promise<void> | void;
18
32
  /**
19
33
  * Customize the serve static options
20
34
  */
@@ -17,6 +17,8 @@ import { serveStatic } from "hono/bun";
17
17
  import { createMiddleware } from "hono/factory";
18
18
  import { logger } from "hono/logger";
19
19
  import { createRequestHandler } from "react-router";
20
+ var serverInstance;
21
+ var shutdownCallback;
20
22
  async function createHonoServer(options) {
21
23
  const build = await importBuild();
22
24
  const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
@@ -28,6 +30,9 @@ async function createHonoServer(options) {
28
30
  const mode = getBuildMode();
29
31
  const PRODUCTION = mode === "production";
30
32
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
33
+ if (PRODUCTION && mergedOptions.onGracefulShutdown) {
34
+ shutdownCallback = mergedOptions.onGracefulShutdown;
35
+ }
31
36
  const app = new Hono(mergedOptions.app);
32
37
  const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
33
38
  app,
@@ -85,6 +90,38 @@ async function createHonoServer(options) {
85
90
  };
86
91
  if (PRODUCTION) {
87
92
  server = injectWebSocket(server);
93
+ serverInstance = Bun.serve(server);
94
+ console.log(`Started server: ${serverInstance.protocol}://${serverInstance.hostname}:${serverInstance.port}`);
95
+ if (shutdownCallback != null) {
96
+ let gracefulShutdownInProgress = false;
97
+ const gracefulShutdown = async (signal) => {
98
+ if (gracefulShutdownInProgress) return;
99
+ gracefulShutdownInProgress = true;
100
+ try {
101
+ if (serverInstance != null) {
102
+ console.log(`
103
+ \u{1F4E1} Received ${signal}, shutting down gracefully...`);
104
+ await serverInstance.stop(false);
105
+ console.log("Server stopped, all requests completed");
106
+ if (shutdownCallback) {
107
+ console.log("\u{1F9F9} Running cleanup tasks...");
108
+ await shutdownCallback();
109
+ console.log("\u2705 Cleanup complete!");
110
+ }
111
+ console.log("\u{1F44B} Graceful shutdown completed");
112
+ }
113
+ process.exit(0);
114
+ } catch (error) {
115
+ console.error("\u274C Shutdown failed:", error);
116
+ process.exit(1);
117
+ }
118
+ };
119
+ if (serverInstance != null) {
120
+ process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
121
+ process.on("SIGINT", () => gracefulShutdown("SIGINT"));
122
+ console.log("\u2705 Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
123
+ }
124
+ }
88
125
  } else if (globalThis.__viteDevServer?.httpServer) {
89
126
  const httpServer = globalThis.__viteDevServer.httpServer;
90
127
  cleanUpgradeListeners(httpServer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "2.24.1",
3
+ "version": "2.25.0",
4
4
  "type": "module",
5
5
  "description": "The Vite plugin you need to create a Hono server for your React Router app in less than 10 seconds.",
6
6
  "publishConfig": {