react-router-hono-server 2.7.1 → 2.8.1

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
@@ -32,7 +32,7 @@ It presets a default Hono server config that you can [customize](#options)
32
32
  >
33
33
  > Only works with **Vite**
34
34
  >
35
- > Only **Node**, **Bun** and **Cloudflare Workers** are supported
35
+ > Only **Node**, **Bun**, **Cloudflare Workers** and **AWS Lambda** (experimental) are supported
36
36
 
37
37
  > [!TIP]
38
38
  > 👨‍🏫 There is some examples in the [examples](./examples) folder. I hope they will help you.
@@ -165,6 +165,27 @@ export default defineConfig({
165
165
  });
166
166
  ```
167
167
 
168
+ #### AWS Lambda
169
+ > [!TIP]
170
+ > AWS shares the same runtime as Node.
171
+ <!-- > Check this [example](./examples/node/simple/) to see how to use it. -->
172
+
173
+ ```ts
174
+ // vite.config.ts
175
+ import { reactRouter } from "@react-router/dev/vite";
176
+ import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
177
+ import { defineConfig } from "vite";
178
+ import tsconfigPaths from "vite-tsconfig-paths";
179
+
180
+ export default defineConfig({
181
+ plugins: [
182
+ reactRouterHonoServer({ runtime: "aws", dev: { export: "handler" } }), // add this
183
+ reactRouter(),
184
+ tsconfigPaths()
185
+ ],
186
+ });
187
+ ```
188
+
168
189
  ### Create the server
169
190
  > [!TIP]
170
191
  > You can use the CLI to create the server file for you.
@@ -270,6 +291,16 @@ binding = "ASSETS"
270
291
  experimental_serve_directly = false
271
292
  ```
272
293
 
294
+ ### AWS Lambda
295
+ It is not an error, you can keep the React Router defaults for `build` and `dev`!
296
+ ```json
297
+ "scripts": {
298
+ "build": "react-router build",
299
+ "dev": "react-router dev",
300
+ "start": "I don't know how to start it in production mode 😅",
301
+ },
302
+ ```
303
+
273
304
  ## How it works
274
305
 
275
306
  This helper works differently depending on the environment.
@@ -290,7 +321,7 @@ That's all!
290
321
 
291
322
  #### `reactRouterHonoServer` (Vite Plugin)
292
323
  ```ts
293
- type Runtime = "node" | "bun" | "cloudflare";
324
+ type Runtime = "node" | "bun" | "cloudflare" | "aws";
294
325
 
295
326
  type ReactRouterHonoServerPluginOptions = {
296
327
  /**
@@ -321,6 +352,12 @@ type ReactRouterHonoServerPluginOptions = {
321
352
  * Defaults include `appDirectory` content.
322
353
  */
323
354
  exclude?: (string | RegExp)[];
355
+ /**
356
+ * The name of the export to use for the server.
357
+ *
358
+ * Defaults to `default`.
359
+ */
360
+ export?: string;
324
361
  };
325
362
  };
326
363
  ```
@@ -504,6 +541,22 @@ export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerO
504
541
  export interface HonoServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {}
505
542
  ```
506
543
 
544
+ ##### AWS Lambda
545
+ ```ts
546
+ type InvokeMode = "default" | "stream";
547
+
548
+ interface HonoAWSServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {
549
+ /**
550
+ * The invoke mode to use
551
+ *
552
+ * Defaults to `default`
553
+ *
554
+ * {@link https://hono.dev/docs/getting-started/aws-lambda#lambda-response-streaming}
555
+ */
556
+ invokeMode: InvokeMode;
557
+ }
558
+ ```
559
+
507
560
  ## Middleware
508
561
 
509
562
  🚨 Redirecting from a middleware
@@ -732,6 +785,9 @@ Cloudflare requires a different approach to WebSockets, based on Durable Objects
732
785
  >
733
786
  > Work in progress on Cloudflare team: https://github.com/flarelabs-net/vite-plugin-cloudflare
734
787
 
788
+ #### AWS
789
+ Not supported.
790
+
735
791
  ## Basename and Hono sub apps
736
792
 
737
793
  > [!NOTE]
@@ -784,6 +840,7 @@ export default await createHonoServer({
784
840
  ## Pre-rendering
785
841
  You should be able to use pre-rendering with this package.
786
842
 
843
+ ### Node and Bun
787
844
  > [!TIP]
788
845
  > Check this [example](./examples/node/with-pre-render/) to see how to use it.
789
846
 
@@ -792,7 +849,7 @@ You should be able to use pre-rendering with this package.
792
849
  >
793
850
  > The file path is fixed to `assets/server-build.js`.
794
851
 
795
- Add the prerender option to your `react-router.config.ts`
852
+ Add the pre-render option to your `react-router.config.ts`
796
853
 
797
854
  ```ts
798
855
  import type { Config } from "@react-router/dev/config";
@@ -803,6 +860,21 @@ export default {
803
860
  } satisfies Config;
804
861
  ```
805
862
 
863
+ ### Cloudflare
864
+ > [!TIP]
865
+ > Check this [example](./examples/cloudflare/with-pre-render/) to see how to use it.
866
+
867
+ Add the pre-render option to your `react-router.config.ts`
868
+
869
+ ```ts
870
+ import type { Config } from "@react-router/dev/config";
871
+
872
+ export default {
873
+ prerender: ["/"],
874
+ } satisfies Config;
875
+ ```
876
+
877
+
806
878
  ## Migrate from v1
807
879
  _You should not expect any breaking changes._
808
880
 
@@ -0,0 +1,72 @@
1
+ import * as hono_aws_lambda from 'hono/aws-lambda';
2
+ import * as hono_types from 'hono/types';
3
+ import { BlankEnv } from 'hono/types';
4
+ import { Env, Hono } from 'hono';
5
+ import { W as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-BboEKLhp.js';
6
+ export { c as createGetLoadContext } from '../helpers-BboEKLhp.js';
7
+ import 'react-router';
8
+ import 'hono/ws';
9
+
10
+ interface CognitoIdentity {
11
+ cognitoIdentityId: string;
12
+ cognitoIdentityPoolId: string;
13
+ }
14
+ interface ClientContext {
15
+ client: ClientContextClient;
16
+ Custom?: any;
17
+ env: ClientContextEnv;
18
+ }
19
+ interface ClientContextClient {
20
+ installationId: string;
21
+ appTitle: string;
22
+ appVersionName: string;
23
+ appVersionCode: string;
24
+ appPackageName: string;
25
+ }
26
+ interface ClientContextEnv {
27
+ platformVersion: string;
28
+ platform: string;
29
+ make: string;
30
+ model: string;
31
+ locale: string;
32
+ }
33
+ /**
34
+ * {@link Handler} context parameter.
35
+ * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.
36
+ */
37
+ interface LambdaContext {
38
+ callbackWaitsForEmptyEventLoop: boolean;
39
+ functionName: string;
40
+ functionVersion: string;
41
+ invokedFunctionArn: string;
42
+ memoryLimitInMB: string;
43
+ awsRequestId: string;
44
+ logGroupName: string;
45
+ logStreamName: string;
46
+ identity?: CognitoIdentity | undefined;
47
+ clientContext?: ClientContext | undefined;
48
+ getRemainingTimeInMillis(): number;
49
+ }
50
+ type Callback<TResult = any> = (error?: Error | string | null, result?: TResult) => void;
51
+ type Handler<TEvent = any, TResult = any> = (event: TEvent, context: LambdaContext, callback: Callback<TResult>) => void | Promise<TResult>;
52
+
53
+ type InvokeMode = "default" | "stream";
54
+ interface HonoAWSServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {
55
+ /**
56
+ * The invoke mode to use
57
+ *
58
+ * Defaults to `default`
59
+ *
60
+ * {@link https://hono.dev/docs/getting-started/aws-lambda#lambda-response-streaming}
61
+ */
62
+ invokeMode: InvokeMode;
63
+ }
64
+ type HonoServerOptions<E extends Env = BlankEnv> = HonoAWSServerOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
65
+ /**
66
+ * Create a Hono server
67
+ *
68
+ * @param config {@link HonoServerOptions} - The configuration options for the server
69
+ */
70
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E, hono_types.BlankSchema, "/"> | Handler | ((event: hono_aws_lambda.LambdaEvent, lambdaContext?: hono_aws_lambda.LambdaContext) => Promise<hono_aws_lambda.APIGatewayProxyResult>)>;
71
+
72
+ export { type HonoServerOptions, createHonoServer };
@@ -0,0 +1,77 @@
1
+ import {
2
+ bindIncomingRequestSocketInfo,
3
+ createGetLoadContext,
4
+ importBuild
5
+ } from "../chunk-DZZCDCBI.js";
6
+ import {
7
+ cache
8
+ } from "../chunk-V3H4JZ4H.js";
9
+
10
+ // src/adapters/aws-lambda.ts
11
+ import { serveStatic } from "@hono/node-server/serve-static";
12
+ import { Hono } from "hono";
13
+ import { handle, streamHandle } from "hono/aws-lambda";
14
+ import { createMiddleware } from "hono/factory";
15
+ import { logger } from "hono/logger";
16
+ import { createRequestHandler } from "react-router";
17
+ async function createHonoServer(options) {
18
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
19
+ const mergedOptions = {
20
+ ...options,
21
+ invokeMode: options?.invokeMode ?? "default",
22
+ defaultLogger: options?.defaultLogger ?? true
23
+ };
24
+ const mode = import.meta.env.MODE || "production";
25
+ const PRODUCTION = mode === "production";
26
+ const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
27
+ const app = new Hono(mergedOptions.app);
28
+ if (!PRODUCTION) {
29
+ app.use(bindIncomingRequestSocketInfo());
30
+ }
31
+ await mergedOptions.beforeAll?.(app);
32
+ if (!PRODUCTION) {
33
+ app.use(
34
+ `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
35
+ cache(60 * 60 * 24 * 365),
36
+ // 1 year
37
+ serveStatic({ root: clientBuildPath })
38
+ );
39
+ app.use(
40
+ "*",
41
+ cache(60 * 60),
42
+ // 1 hour
43
+ serveStatic({ root: PRODUCTION ? clientBuildPath : "./public" })
44
+ );
45
+ }
46
+ if (mergedOptions.defaultLogger) {
47
+ app.use("*", logger());
48
+ }
49
+ await mergedOptions.configure?.(app);
50
+ const reactRouterApp = new Hono({
51
+ strict: false
52
+ });
53
+ reactRouterApp.use(async (c, next) => {
54
+ const build = await importBuild();
55
+ return createMiddleware(async (c2) => {
56
+ const requestHandler = createRequestHandler(build, mode);
57
+ const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
58
+ return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
59
+ })(c, next);
60
+ });
61
+ app.route(`${basename}`, reactRouterApp);
62
+ if (basename) {
63
+ app.route(`${basename}.data`, reactRouterApp);
64
+ }
65
+ if (PRODUCTION) {
66
+ if (mergedOptions.invokeMode === "stream") {
67
+ return streamHandle(app);
68
+ }
69
+ return handle(app);
70
+ }
71
+ console.log("\u{1F6A7} Dev server started");
72
+ return app;
73
+ }
74
+ export {
75
+ createGetLoadContext,
76
+ createHonoServer
77
+ };
@@ -1,8 +1,8 @@
1
1
  import { Serve, ServeOptions } from 'bun';
2
2
  import { Env } from 'hono';
3
3
  import { BlankEnv } from 'hono/types';
4
- import { W as WithWebsocket, a as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-F2RS9Syt.js';
5
- export { c as createGetLoadContext } from '../helpers-F2RS9Syt.js';
4
+ import { a as WithWebsocket, W as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-BboEKLhp.js';
5
+ export { c as createGetLoadContext } from '../helpers-BboEKLhp.js';
6
6
  import 'react-router';
7
7
  import 'hono/ws';
8
8
 
@@ -1,8 +1,8 @@
1
1
  import * as hono_types from 'hono/types';
2
2
  import { BlankEnv } from 'hono/types';
3
3
  import { Env, Hono } from 'hono';
4
- import { a as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-F2RS9Syt.js';
5
- export { c as createGetLoadContext } from '../helpers-F2RS9Syt.js';
4
+ import { W as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-BboEKLhp.js';
5
+ export { c as createGetLoadContext } from '../helpers-BboEKLhp.js';
6
6
  import 'react-router';
7
7
  import 'hono/ws';
8
8
 
@@ -2,8 +2,8 @@ import { AddressInfo } from 'node:net';
2
2
  import { ServerType } from '@hono/node-server';
3
3
  import { Env, Hono } from 'hono';
4
4
  import { BlankEnv } from 'hono/types';
5
- import { W as WithWebsocket, a as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-F2RS9Syt.js';
6
- export { c as createGetLoadContext } from '../helpers-F2RS9Syt.js';
5
+ import { a as WithWebsocket, W as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-BboEKLhp.js';
6
+ export { c as createGetLoadContext } from '../helpers-BboEKLhp.js';
7
7
  import { ServerOptions, createServer } from 'node:http';
8
8
  import { ServerOptions as ServerOptions$2, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
9
9
  import { ServerOptions as ServerOptions$1, createServer as createServer$1 } from 'node:https';
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";
4
+ type Runtime = "node" | "bun" | "cloudflare" | "aws";
5
5
 
6
6
  type ReactRouterHonoServerPluginOptions = {
7
7
  /**
@@ -32,6 +32,12 @@ type ReactRouterHonoServerPluginOptions = {
32
32
  * Defaults include `appDirectory` content.
33
33
  */
34
34
  exclude?: DevServerOptions["exclude"];
35
+ /**
36
+ * The name of the export to use for the server.
37
+ *
38
+ * Defaults to `default`.
39
+ */
40
+ export?: DevServerOptions["export"];
35
41
  };
36
42
  };
37
43
  declare function reactRouterHonoServer(options?: ReactRouterHonoServerPluginOptions): Plugin;
package/dist/dev.js CHANGED
@@ -3,9 +3,9 @@ import fs from "node:fs";
3
3
  import path from "node:path";
4
4
  import honoDevServer from "@hono/vite-dev-server";
5
5
  import bunAdapter from "@hono/vite-dev-server/bun";
6
- import cloudflareAdapter from "@hono/vite-dev-server/cloudflare";
7
6
  import nodeAdapter from "@hono/vite-dev-server/node";
8
7
  var virtualModuleId = "\0virtual:react-router-hono-server/server";
8
+ var reactRouterExport = "export {serverManifest as assets, assetsBuildDirectory, basename, entry, future, isSpaMode, publicPath, routes };";
9
9
  function reactRouterHonoServer(options = {}) {
10
10
  const runtime = options.runtime || "node";
11
11
  let pluginConfig;
@@ -64,6 +64,11 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
64
64
  }
65
65
  return {
66
66
  ...baseConfig,
67
+ resolve: {
68
+ alias: runtime === "cloudflare" ? {
69
+ "react-dom/server": "react-dom/server.browser"
70
+ } : void 0
71
+ },
67
72
  build: {
68
73
  // https://vite.dev/config/build-options#build-target
69
74
  cssTarget: ["es2020", "edge88", "firefox78", "chrome87", "safari14"],
@@ -77,12 +82,24 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
77
82
  return reactRouterBuildFile;
78
83
  }
79
84
  return "assets/[name]-[hash].js";
80
- }
85
+ },
86
+ footer: runtime === "cloudflare" ? reactRouterExport : void 0
81
87
  }
82
88
  }
83
89
  }
84
90
  };
85
91
  },
92
+ async closeBundle() {
93
+ if (!pluginConfig || !pluginConfig.isSsrBuild || runtime !== "cloudflare") {
94
+ return;
95
+ }
96
+ console.log("Cleaning up server exports...");
97
+ const buildPath = path.join(pluginConfig.rootDirectory, pluginConfig.buildDirectory, "server", "index.js");
98
+ let content = await fs.promises.readFile(buildPath, "utf-8");
99
+ content = content.replace(reactRouterExport, "");
100
+ await fs.promises.writeFile(buildPath, content);
101
+ console.log("\x1B[32mAll done!\x1B[0m");
102
+ },
86
103
  async configureServer(server) {
87
104
  globalThis.__viteDevServer = server;
88
105
  if (!pluginConfig) {
@@ -96,16 +113,17 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
96
113
  adapter = bunAdapter;
97
114
  }
98
115
  if (runtime === "cloudflare") {
116
+ const { cloudflareAdapter } = await import("@hono/vite-dev-server/cloudflare");
99
117
  adapter = cloudflareAdapter;
100
118
  }
101
119
  devServerPlugin = honoDevServer({
102
120
  adapter,
103
121
  injectClientScript: false,
104
122
  entry: pluginConfig.serverEntryPoint,
105
- export: "default",
123
+ export: options.dev?.export || "default",
106
124
  exclude: [
107
125
  new RegExp(
108
- `^(?=\\/${pluginConfig.appDirectory.replaceAll("/", "")}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`
126
+ `^(?=\\/${pluginConfig.appDirectory.replace(/^\/+|\/+$/g, "").replaceAll("/", "\\/")}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`
109
127
  ),
110
128
  /\?import(\?.*)?$/,
111
129
  /^\/@.+$/,
@@ -97,4 +97,4 @@ declare function createGetLoadContext(getLoadContext: HonoServerOptionsBase<Env>
97
97
  mode: string;
98
98
  }) => Promise<react_router.AppLoadContext> | react_router.AppLoadContext) | undefined;
99
99
 
100
- export { type HonoServerOptionsBase as H, type WithWebsocket as W, type WithoutWebsocket as a, createGetLoadContext as c };
100
+ export { type HonoServerOptionsBase as H, type WithoutWebsocket as W, type WithWebsocket as a, createGetLoadContext as c };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "2.7.1",
3
+ "version": "2.8.1",
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
  "exports": {
@@ -25,6 +25,13 @@
25
25
  "default": "./dist/adapters/cloudflare.js"
26
26
  }
27
27
  },
28
+ "./aws-lambda": {
29
+ "import": {
30
+ "import": "./dist/adapters/aws-lambda.js",
31
+ "types": "./dist/adapters/aws-lambda.d.ts",
32
+ "default": "./dist/adapters/aws-lambda.js"
33
+ }
34
+ },
28
35
  "./dev": {
29
36
  "import": {
30
37
  "import": "./dist/dev.js",
@@ -80,10 +87,6 @@
80
87
  "@hono/vite-dev-server": "^0.17.0",
81
88
  "hono": "^4.6.11"
82
89
  },
83
- "optionalDependencies": {
84
- "miniflare": "^3.20241205.0",
85
- "wrangler": "^3.91.0"
86
- },
87
90
  "devDependencies": {
88
91
  "@biomejs/biome": "^1.9.4",
89
92
  "@types/bun": "^1.1.14",
@@ -96,8 +99,18 @@
96
99
  "vitest": "^2.1.5"
97
100
  },
98
101
  "peerDependencies": {
102
+ "miniflare": "^3.20241205.0",
99
103
  "react-router": "^7.1.0",
100
- "vite": "^5.1.0 || ^6.0.0"
104
+ "vite": "^5.1.0 || ^6.0.0",
105
+ "wrangler": "^3.91.0"
106
+ },
107
+ "peerDependenciesMeta": {
108
+ "miniflare": {
109
+ "optional": true
110
+ },
111
+ "wrangler": {
112
+ "optional": true
113
+ }
101
114
  },
102
115
  "engines": {
103
116
  "node": ">=22.12.0"