react-router-hono-server 2.25.3 → 2.26.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
@@ -287,6 +287,26 @@ export default defineConfig({
287
287
  });
288
288
  ```
289
289
 
290
+ #### Deno
291
+ > [!TIP]
292
+ > Check this [example](./examples/deno/simple/) to see how to use it.
293
+
294
+ ```ts
295
+ // vite.config.ts
296
+ import { reactRouter } from "@react-router/dev/vite";
297
+ import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
298
+ import { defineConfig } from "vite";
299
+ import tsconfigPaths from "vite-tsconfig-paths";
300
+
301
+ export default defineConfig({
302
+ plugins: [
303
+ reactRouterHonoServer({ runtime: "deno" }), // add this
304
+ reactRouter(),
305
+ tsconfigPaths()
306
+ ],
307
+ });
308
+ ```
309
+
290
310
  ### Create the server
291
311
  > [!TIP]
292
312
  > You can use the CLI to create the server file for you.
@@ -402,6 +422,17 @@ It is not an error, you can keep the React Router defaults for `build` and `dev`
402
422
  },
403
423
  ```
404
424
 
425
+
426
+ ### Deno
427
+ It is not an error, you can keep the React Router defaults for `build`!
428
+ ```json
429
+ "scripts": {
430
+ "build": "react-router build",
431
+ "dev": "deno run -A npm:react-router dev",
432
+ "start": "deno run -A ./build/server/index.js",
433
+ },
434
+ ```
435
+
405
436
  ## How it works
406
437
 
407
438
  This helper works differently depending on the environment.
@@ -422,7 +453,7 @@ That's all!
422
453
 
423
454
  #### `reactRouterHonoServer` (Vite Plugin)
424
455
  ```ts
425
- type Runtime = "node" | "bun" | "cloudflare" | "aws";
456
+ type Runtime = "node" | "bun" | "cloudflare" | "aws" | "deno";
426
457
 
427
458
  type ReactRouterHonoServerPluginOptions = {
428
459
  /**
@@ -717,6 +748,50 @@ interface HonoAWSServerOptions<E extends Env = BlankEnv> extends Omit<HonoServer
717
748
  }
718
749
  ```
719
750
 
751
+
752
+ ##### Deno
753
+ ```ts
754
+ interface HonoDenoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
755
+ /**
756
+ * Customize the deno server
757
+ *
758
+ * {@link https://docs.deno.com/api/deno/~/Deno.serve#parameters}
759
+ */
760
+ customDenoServer?: Deno.ServeOptions;
761
+
762
+ /**
763
+ * Callback executed after server has closed and all inflight requests completed,
764
+ * before process exit. Only applicable in production mode.
765
+ *
766
+ * @example
767
+ * ```ts
768
+ * export default createHonoServer({
769
+ * onGracefulShutdown: async () => {
770
+ * await db.close();
771
+ * },
772
+ * });
773
+ * ```
774
+ */
775
+
776
+ onGracefulShutdown?: () => Promise<void> | void;
777
+ /**
778
+ * Customize the serve static options
779
+ */
780
+ serveStaticOptions?: {
781
+ /**
782
+ * Customize the public assets (what's in your `public` directory) serve static options.
783
+ *
784
+ */
785
+ publicAssets?: Omit<ServeStaticOptions<E>, "root">;
786
+ /**
787
+ * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
788
+ *
789
+ */
790
+ clientAssets?: Omit<ServeStaticOptions<E>, "root">;
791
+ };
792
+ }
793
+ ```
794
+
720
795
  ## Middleware
721
796
 
722
797
  🚨 Redirecting from a middleware
@@ -886,12 +961,15 @@ You can use React Router middleware with this package.
886
961
 
887
962
  Then, you can use the `use` function from `react-router` to access the context in your loaders and actions.
888
963
 
889
- ```ts
890
-
891
-
892
964
  ### Using WebSockets
893
965
  #### Node
894
- This package has a built-in helper to use `@hono/node-ws`
966
+ This package has a built-in helper to use `@hono/node-ws`.
967
+
968
+ When using WebSockets with the Node adapter, install `@hono/node-ws` in your app:
969
+
970
+ ```bash
971
+ npm install @hono/node-ws
972
+ ```
895
973
 
896
974
  > [!TIP]
897
975
  > Check this [example](./examples/node/websocket/) to see how to use it.
@@ -0,0 +1,53 @@
1
+ import { Env, Hono } from 'hono';
2
+ import { ServeStaticOptions } from 'hono/serve-static';
3
+ import { BlankEnv } from 'hono/types';
4
+ import { H as HonoServerOptionsBase, W as WithoutWebsocket } from '../helpers-BXdVojwF.js';
5
+ export { c as createGetLoadContext } from '../helpers-BXdVojwF.js';
6
+ import 'hono/ws';
7
+ import 'react-router';
8
+
9
+ type CustomDenoServer = Deno.ServeTcpOptions;
10
+ interface HonoDenoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
11
+ /**
12
+ * Customize the deno server
13
+ *
14
+ * {@link https://docs.deno.com/api/deno/~/Deno.serve#parameters}
15
+ */
16
+ customDenoServer?: Partial<CustomDenoServer>;
17
+ /**
18
+ * Callback executed after server has closed and all inflight requests completed,
19
+ * before process exit. Only applicable in production mode.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * export default createHonoServer({
24
+ * onGracefulShutdown: async () => {
25
+ * await db.close();
26
+ * },
27
+ * });
28
+ * ```
29
+ */
30
+ onGracefulShutdown?: () => Promise<void> | void;
31
+ serveStaticOptions?: {
32
+ /**
33
+ * Customize the public assets (what's in your `public` directory) serve static options.
34
+ *
35
+ */
36
+ publicAssets?: Omit<ServeStaticOptions<E>, "root">;
37
+ /**
38
+ * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
39
+ *
40
+ */
41
+ clientAssets?: Omit<ServeStaticOptions<E>, "root">;
42
+ };
43
+ }
44
+ type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & WithoutWebsocket<E>;
45
+ type HonoServerOptions<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
46
+ /**
47
+ * Create a Hono server
48
+ *
49
+ * @param config {@link HonoServerOptions} - The configuration options for the server
50
+ */
51
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithoutWebSocket<E>): Promise<Hono<E>>;
52
+
53
+ export { type HonoServerOptions, createHonoServer };
@@ -0,0 +1,110 @@
1
+ import {
2
+ bindIncomingRequestSocketInfo,
3
+ createGetLoadContext,
4
+ getBuildMode,
5
+ importBuild
6
+ } from "../chunk-BH64OQMW.js";
7
+ import {
8
+ cache
9
+ } from "../chunk-YI5FED4X.js";
10
+
11
+ // src/adapters/deno.ts
12
+ import { Hono } from "hono";
13
+ import { serveStatic } from "hono/deno";
14
+ import { createMiddleware } from "hono/factory";
15
+ import { logger } from "hono/logger";
16
+ import { createRequestHandler } from "react-router";
17
+ var serverInstance;
18
+ var shutdownCallback;
19
+ async function createHonoServer(options) {
20
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
21
+ const mergedOptions = {
22
+ ...options,
23
+ port: options?.port || Number(Deno.env.get("PORT")) || 3e3,
24
+ defaultLogger: options?.defaultLogger ?? true
25
+ };
26
+ const mode = getBuildMode();
27
+ const PRODUCTION = mode === "production";
28
+ const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
29
+ const app = new Hono(mergedOptions.app);
30
+ if (PRODUCTION && mergedOptions.onGracefulShutdown) {
31
+ shutdownCallback = mergedOptions.onGracefulShutdown;
32
+ }
33
+ if (!PRODUCTION) {
34
+ app.use(bindIncomingRequestSocketInfo());
35
+ }
36
+ await mergedOptions.beforeAll?.(app);
37
+ app.use(
38
+ `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
39
+ cache(60 * 60 * 24 * 365),
40
+ // 1 year
41
+ serveStatic({ root: clientBuildPath, ...mergedOptions.serveStaticOptions?.clientAssets })
42
+ );
43
+ app.use(
44
+ "*",
45
+ cache(60 * 60),
46
+ // 1 hour
47
+ serveStatic({ root: PRODUCTION ? clientBuildPath : "./public", ...mergedOptions.serveStaticOptions?.publicAssets })
48
+ );
49
+ if (mergedOptions.defaultLogger) {
50
+ app.use("*", logger());
51
+ }
52
+ await mergedOptions.configure?.(app);
53
+ const reactRouterApp = new Hono({
54
+ strict: false
55
+ });
56
+ reactRouterApp.use(async (c, next) => {
57
+ const build = await importBuild();
58
+ return createMiddleware(async (c2) => {
59
+ const requestHandler = createRequestHandler(build, mode);
60
+ const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
61
+ return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
62
+ })(c, next);
63
+ });
64
+ app.route(`${basename}`, reactRouterApp);
65
+ if (basename) {
66
+ app.route(`${basename}.data`, reactRouterApp);
67
+ }
68
+ const server = {
69
+ ...mergedOptions.customDenoServer,
70
+ port: mergedOptions.port
71
+ };
72
+ if (PRODUCTION) {
73
+ serverInstance = Deno.serve(server, app.fetch);
74
+ if (shutdownCallback != null) {
75
+ let gracefulShutdownInProgress = false;
76
+ const gracefulShutdown = async (signal) => {
77
+ if (gracefulShutdownInProgress) return;
78
+ gracefulShutdownInProgress = true;
79
+ try {
80
+ if (serverInstance != null) {
81
+ console.log(`
82
+ \u{1F4E1} Received ${signal}, shutting down gracefully...`);
83
+ await serverInstance.shutdown();
84
+ console.log("Server stopped, all requests completed");
85
+ if (shutdownCallback) {
86
+ console.log("\u{1F9F9} Running cleanup tasks...");
87
+ await shutdownCallback();
88
+ console.log("\u2705 Cleanup complete!");
89
+ }
90
+ console.log("\u{1F44B} Graceful shutdown completed");
91
+ }
92
+ process.exit(0);
93
+ } catch (error) {
94
+ console.error("\u274C Shutdown failed:", error);
95
+ process.exit(1);
96
+ }
97
+ };
98
+ if (serverInstance != null) {
99
+ process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
100
+ process.on("SIGINT", () => gracefulShutdown("SIGINT"));
101
+ console.log("\u2705 Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
102
+ }
103
+ }
104
+ }
105
+ return app;
106
+ }
107
+ export {
108
+ createGetLoadContext,
109
+ createHonoServer
110
+ };
package/dist/dev.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { DevServerOptions } from '@hono/vite-dev-server';
2
2
  import { Plugin } from 'vite';
3
3
 
4
- type Runtime = "node" | "bun" | "cloudflare" | "aws";
4
+ type Runtime = "node" | "bun" | "cloudflare" | "aws" | "deno";
5
5
 
6
6
  type ReactRouterHonoServerPluginOptionsBase = {
7
7
  /**
package/dist/dev.js CHANGED
@@ -89,6 +89,11 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
89
89
  "react-dom/server": "react-dom/server.node"
90
90
  };
91
91
  }
92
+ if (runtime === "deno" && env.command === "build") {
93
+ alias = {
94
+ "react-dom/server": "react-dom/server.node"
95
+ };
96
+ }
92
97
  const ssrConfig = {
93
98
  resolve: {
94
99
  alias
@@ -177,6 +182,23 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
177
182
  const { cloudflareAdapter } = await import("@hono/vite-dev-server/cloudflare");
178
183
  adapter = cloudflareAdapter;
179
184
  }
185
+ if (runtime === "deno") {
186
+ adapter = () => {
187
+ if (typeof globalThis.navigator === "undefined") {
188
+ globalThis.navigator = {
189
+ userAgent: "Deno"
190
+ };
191
+ } else {
192
+ Object.defineProperty(globalThis.navigator, "userAgent", {
193
+ value: "Deno",
194
+ writable: false
195
+ });
196
+ }
197
+ return {
198
+ env: Deno.env.toObject()
199
+ };
200
+ };
201
+ }
180
202
  devServerPlugin = honoDevServer({
181
203
  adapter,
182
204
  injectClientScript: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "2.25.3",
3
+ "version": "2.26.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": {
@@ -36,6 +36,13 @@
36
36
  "default": "./dist/adapters/aws-lambda.js"
37
37
  }
38
38
  },
39
+ "./deno": {
40
+ "import": {
41
+ "types": "./dist/adapters/deno.d.ts",
42
+ "import": "./dist/adapters/deno.js",
43
+ "default": "./dist/adapters/deno.js"
44
+ }
45
+ },
39
46
  "./dev": {
40
47
  "import": {
41
48
  "types": "./dist/dev.d.ts",
@@ -86,6 +93,7 @@
86
93
  "@cloudflare/workers-types": "^4.20250405.0",
87
94
  "@types/bun": "^1.3.6",
88
95
  "@types/node": "^22.19.10",
96
+ "@types/deno": "^2.5.0",
89
97
  "@vitest/coverage-v8": "^4.0.18",
90
98
  "hono": "4.12.8",
91
99
  "lefthook": "^2.0.16",
@@ -126,11 +134,12 @@
126
134
  "test": "vitest run",
127
135
  "test:cov": "vitest run --coverage",
128
136
  "build": "tsup",
129
- "build:watch": "npm run build -- --watch",
137
+ "build:watch": "pnpm run build -- --watch",
138
+ "build:pack": "pnpm pack --pack-destination dist",
130
139
  "check": "biome check .",
131
140
  "check:fix": "biome check --fix .",
132
141
  "typecheck": "tsc",
133
- "validate": "npm run check && npm run tsc",
142
+ "validate": "pnpm run check && pnpm run typecheck",
134
143
  "clear-all": "rm -rf node_modules pnpm-lock.yaml && find ./examples -type d -name 'node_modules' -exec rm -rf {} + && find ./examples -name 'pnpm-lock.yaml' -exec rm -f {} +",
135
144
  "changeset": "changeset",
136
145
  "version": "changeset version && biome format --write package.json",