react-router-hono-server 2.2.1 → 2.4.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
@@ -645,17 +645,39 @@ Cloudflare requires a different approach to WebSockets, based on Durable Objects
645
645
  >
646
646
  > Work in progress on Cloudflare team: https://github.com/flarelabs-net/vite-plugin-cloudflare
647
647
 
648
- ### Migrate from v1
648
+ ## Pre-rendering
649
+ You should be able to use pre-rendering with this package.
650
+
651
+ > [!TIP]
652
+ > Check this [example](./examples/node/with-pre-render/) to see how to use it.
653
+
654
+ > [!IMPORTANT]
655
+ > You need to add the `serverBuildFile` option to your `react-router.config.ts` file.
656
+ >
657
+ > The file path is fixed to `assets/server-build.js`.
658
+
659
+ Add the prerender option to your `react-router.config.ts`
660
+
661
+ ```ts
662
+ import type { Config } from "@react-router/dev/config";
663
+
664
+ export default {
665
+ serverBuildFile: "assets/server-build.js", // 🚨 Dont forget this
666
+ prerender: ["/"],
667
+ } satisfies Config;
668
+ ```
669
+
670
+ ## Migrate from v1
649
671
  _You should not expect any breaking changes._
650
672
 
651
- #### Install the latest version
673
+ ### Install the latest version
652
674
 
653
675
  ```bash
654
676
  npm install react-router-hono-server@latest
655
677
  ```
656
678
 
657
- #### Create the server file
658
- ##### Option 1 - You previously had all your server code in `app/entry.server.tsx`
679
+ ### Create the server file
680
+ #### Option 1 - You previously had all your server code in `app/entry.server.tsx`
659
681
  ```bash
660
682
  touch app/server.ts
661
683
  ```
@@ -664,7 +686,7 @@ or
664
686
  npx react-router-hono-server reveal file
665
687
  ```
666
688
 
667
- ##### Option 2 - You previously had your server code in a `server` folder
689
+ #### Option 2 - You previously had your server code in a `server` folder
668
690
  ```bash
669
691
  mkdir app/server
670
692
  touch app/server/index.ts
@@ -674,7 +696,7 @@ or
674
696
  npx react-router-hono-server reveal folder
675
697
  ```
676
698
 
677
- #### Move your server code
699
+ ### Move your server code
678
700
  Move your previous server code to the new file you created in the previous step.
679
701
 
680
702
  > [!NOTE]
@@ -693,7 +715,7 @@ We now use the Vite virtual import `virtual:react-router/server-build` to load t
693
715
  > export default await createHonoServer({/* other options */});
694
716
  > ```
695
717
 
696
- #### Update your `vite.config.ts`
718
+ ### Update your `vite.config.ts`
697
719
 
698
720
  > [!IMPORTANT]
699
721
  > `devServer` is now `reactRouterHonoServer`.
@@ -710,7 +732,7 @@ If you used this hook for Sentry, check this [example](./examples/node/with-sent
710
732
 
711
733
  If you used a custom `buildDirectory` option, check this [example](./examples/node/custom-build/react-router.config.ts) to see how to migrate.
712
734
 
713
- #### Update your package.json scripts
735
+ ### Update your package.json scripts
714
736
  ```json
715
737
  "scripts": {
716
738
  "build": "react-router build",
@@ -1,8 +1,9 @@
1
1
  import {
2
+ bindIncomingRequestSocketInfo,
2
3
  cleanUpgradeListeners,
3
4
  createWebSocket,
4
5
  patchUpgradeListener
5
- } from "../chunk-27A6I7NN.js";
6
+ } from "../chunk-D4WESSFB.js";
6
7
  import {
7
8
  cache
8
9
  } from "../chunk-V3H4JZ4H.js";
@@ -27,6 +28,9 @@ async function createHonoServer(options) {
27
28
  app,
28
29
  enabled: mergedOptions.useWebSocket ?? false
29
30
  });
31
+ if (!PRODUCTION) {
32
+ app.use(bindIncomingRequestSocketInfo());
33
+ }
30
34
  app.use(
31
35
  `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
32
36
  cache(60 * 60 * 24 * 365),
@@ -1,3 +1,6 @@
1
+ import {
2
+ bindIncomingRequestSocketInfo
3
+ } from "../chunk-D4WESSFB.js";
1
4
  import {
2
5
  cache
3
6
  } from "../chunk-V3H4JZ4H.js";
@@ -18,6 +21,7 @@ async function createHonoServer(options) {
18
21
  if (DEV) {
19
22
  const { serveStatic } = await import("@hono/node-server/serve-static");
20
23
  app.use("*", cache(60 * 60), serveStatic({ root: "./public" }));
24
+ app.use(bindIncomingRequestSocketInfo());
21
25
  }
22
26
  if (mergedOptions.defaultLogger) {
23
27
  app.use("*", logger());
@@ -1,8 +1,9 @@
1
1
  import {
2
+ bindIncomingRequestSocketInfo,
2
3
  cleanUpgradeListeners,
3
4
  createWebSocket,
4
5
  patchUpgradeListener
5
- } from "../chunk-27A6I7NN.js";
6
+ } from "../chunk-D4WESSFB.js";
6
7
  import {
7
8
  cache
8
9
  } from "../chunk-V3H4JZ4H.js";
@@ -32,6 +33,9 @@ async function createHonoServer(options) {
32
33
  app,
33
34
  enabled: mergedOptions.useWebSocket ?? false
34
35
  });
36
+ if (!PRODUCTION) {
37
+ app.use(bindIncomingRequestSocketInfo());
38
+ }
35
39
  app.use(
36
40
  `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
37
41
  cache(60 * 60 * 24 * 365),
@@ -1,4 +1,5 @@
1
1
  // src/helpers.ts
2
+ import { createMiddleware } from "hono/factory";
2
3
  var defaultWebSocket = {
3
4
  upgradeWebSocket: () => async () => {
4
5
  },
@@ -63,9 +64,24 @@ function patchUpgradeListener(httpServer) {
63
64
  });
64
65
  }
65
66
  }
67
+ function bindIncomingRequestSocketInfo() {
68
+ return createMiddleware(async (c, next) => {
69
+ c.env.server = {
70
+ incoming: {
71
+ socket: {
72
+ remoteAddress: c.req.raw.headers.get("x-remote-address") || void 0,
73
+ remotePort: Number(c.req.raw.headers.get("x-remote-port")) || void 0,
74
+ remoteFamily: c.req.raw.headers.get("x-remote-family") || void 0
75
+ }
76
+ }
77
+ };
78
+ return next();
79
+ });
80
+ }
66
81
 
67
82
  export {
68
83
  createWebSocket,
69
84
  cleanUpgradeListeners,
70
- patchUpgradeListener
85
+ patchUpgradeListener,
86
+ bindIncomingRequestSocketInfo
71
87
  };
package/dist/dev.js CHANGED
@@ -4,6 +4,7 @@ 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
6
  import cloudflareAdapter from "@hono/vite-dev-server/cloudflare";
7
+ import nodeAdapter from "@hono/vite-dev-server/node";
7
8
  var virtualModuleId = "\0virtual:react-router-hono-server/server";
8
9
  function reactRouterHonoServer(options = {}) {
9
10
  const runtime = options.runtime || "node";
@@ -56,12 +57,25 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
56
57
  if (!pluginConfig.isSsrBuild) {
57
58
  return baseConfig;
58
59
  }
60
+ let reactRouterBuildFile = pluginConfig.serverBuildFile;
61
+ if (reactRouterBuildFile === "index.js") {
62
+ reactRouterBuildFile = "assets/server-build.js";
63
+ }
59
64
  return {
60
65
  ...baseConfig,
61
66
  build: {
62
67
  target: "esnext",
63
68
  rollupOptions: {
64
- input: pluginConfig.serverEntryPoint
69
+ input: pluginConfig.serverEntryPoint,
70
+ output: {
71
+ entryFileNames: "index.js",
72
+ chunkFileNames: (chunk) => {
73
+ if (chunk.name === "server-build") {
74
+ return reactRouterBuildFile;
75
+ }
76
+ return "assets/[name]-[hash].js";
77
+ }
78
+ }
65
79
  }
66
80
  }
67
81
  };
@@ -74,7 +88,7 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
74
88
  if (devServerPlugin) {
75
89
  return;
76
90
  }
77
- let adapter;
91
+ let adapter = nodeAdapter;
78
92
  if (runtime === "bun") {
79
93
  adapter = bunAdapter;
80
94
  }
@@ -95,6 +109,12 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
95
109
  ...pluginConfig.dev?.exclude || []
96
110
  ]
97
111
  });
112
+ server.middlewares.use((req, _res, next) => {
113
+ req.rawHeaders.push("x-remote-address", req.socket.remoteAddress || "unknown");
114
+ req.rawHeaders.push("x-remote-port", String(req.socket.remotePort || "unknown"));
115
+ req.rawHeaders.push("x-remote-family", req.socket.remoteFamily || "unknown");
116
+ next();
117
+ });
98
118
  if (typeof devServerPlugin.configureServer === "function") {
99
119
  devServerPlugin.configureServer(server);
100
120
  } else {
@@ -115,6 +135,7 @@ function resolvePluginConfig(config, options) {
115
135
  const isSsrBuild = reactRouterConfig.isSsrBuild;
116
136
  const assetsDir = config.build?.assetsDir || "assets";
117
137
  const serverEntryPoint = options.serverEntryPoint || findDefaultServerEntry(appDirectory);
138
+ const serverBuildFile = reactRouterConfig.reactRouterConfig.serverBuildFile;
118
139
  return {
119
140
  rootDirectory,
120
141
  buildDirectory,
@@ -122,7 +143,8 @@ function resolvePluginConfig(config, options) {
122
143
  isSsrBuild,
123
144
  assetsDir,
124
145
  serverEntryPoint,
125
- dev: options.dev
146
+ dev: options.dev,
147
+ serverBuildFile
126
148
  };
127
149
  }
128
150
  var warned = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "2.2.1",
3
+ "version": "2.4.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
  "exports": {
@@ -45,8 +45,6 @@
45
45
  "test:cov": "vitest run --coverage",
46
46
  "build": "tsup",
47
47
  "build:watch": "npm run build -- --watch",
48
- "example:dev": "npm run dev -w react-router-example",
49
- "dev": "npm-run-all -s build -p example:dev build:watch",
50
48
  "prepublishOnly": "npm run build",
51
49
  "check": "biome check .",
52
50
  "check:fix": "biome check --fix .",