react-router-hono-server 1.2.0 → 2.1.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 +383 -102
- package/dist/adapters/bun.d.mts +107 -0
- package/dist/adapters/bun.mjs +57 -0
- package/dist/adapters/cloudflare.d.mts +17 -0
- package/dist/adapters/cloudflare.mjs +38 -0
- package/dist/adapters/node.d.mts +57 -0
- package/dist/adapters/node.mjs +66 -0
- package/dist/chunk-ENK5WJB4.mjs +18 -0
- package/dist/cli.mjs +45 -0
- package/dist/dev.d.mts +22 -25
- package/dist/dev.mjs +149 -23
- package/dist/hono-server-options-base.d-DChVrA1u.d.mts +54 -0
- package/dist/middleware.d.mts +10 -0
- package/dist/middleware.mjs +6 -0
- package/package.json +44 -27
- package/dist/node.d.mts +0 -114
- package/dist/node.mjs +0 -78
package/README.md
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
# React Router Hono Server
|
|
2
|
-
|
|
3
|
-
Inspired by [remix-express-vite-plugin](https://github.com/kiliman/remix-express-vite-plugin) from [@kiliman](https://github.com/kiliman)
|
|
1
|
+
# React Router v7 Hono Server
|
|
4
2
|
|
|
5
3
|
<!-- 
|
|
6
4
|

|
|
@@ -9,182 +7,347 @@ Inspired by [remix-express-vite-plugin](https://github.com/kiliman/remix-express
|
|
|
9
7
|

|
|
10
8
|
 -->
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
## Psst!
|
|
11
|
+
> [!IMPORTANT]
|
|
12
|
+
> This package is only compatible with React Router v7
|
|
13
|
+
>
|
|
14
|
+
> You can still use the v1 with @remix-run. [Previous docs](https://github.com/rphlmr/react-router-hono-server/tree/v1.2.0)
|
|
15
|
+
>
|
|
16
|
+
> Migration guide from v1 [here](#migrate-from-v1)
|
|
17
|
+
|
|
18
|
+
## TLDR
|
|
19
|
+
This package contains a helper function `createHonoServer` that enables you to create a Hono
|
|
20
|
+
server bound to your React Router v7 app.
|
|
21
|
+
|
|
22
|
+
Since the Hono server is built along with the rest of your app, you may import app modules as needed.
|
|
23
|
+
|
|
24
|
+
It also supports Vite HMR via the `react-router-hono-server/dev` plugin (which is required
|
|
16
25
|
for this to function).
|
|
17
26
|
|
|
18
|
-
It
|
|
27
|
+
It presets a default Hono server config that you can [customize](#options)
|
|
28
|
+
|
|
19
29
|
|
|
20
30
|
> [!IMPORTANT]
|
|
21
|
-
> Only works with
|
|
31
|
+
> Only works with React Router v7 in **ESM mode**
|
|
22
32
|
>
|
|
23
33
|
> Only works with **Vite**
|
|
24
34
|
>
|
|
25
|
-
> Only
|
|
35
|
+
> Only **Node**, **Bun** and **Cloudflare Workers** are not supported
|
|
26
36
|
|
|
27
37
|
> [!TIP]
|
|
38
|
+
> 👨🏫 There is some examples in the [examples](./examples) folder. I hope they will help you.
|
|
39
|
+
>
|
|
28
40
|
> You can use [remix-hono](https://github.com/sergiodxa/remix-hono) to add cool middleware like [`session`](https://github.com/sergiodxa/remix-hono?tab=readme-ov-file#session-management)
|
|
29
41
|
|
|
30
42
|
## Installation
|
|
31
43
|
|
|
32
|
-
Install the following npm package.
|
|
33
|
-
|
|
44
|
+
Install the following npm package.
|
|
45
|
+
|
|
46
|
+
> [!NOTE]
|
|
47
|
+
> This is not a dev dependency, as it creates the Hono server used in production.
|
|
34
48
|
|
|
35
49
|
```bash
|
|
36
50
|
npm install react-router-hono-server
|
|
37
51
|
```
|
|
38
52
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
53
|
+
> [!TIP]
|
|
54
|
+
> You don't need to install `hono` as it is included in this package.
|
|
55
|
+
>
|
|
56
|
+
> If you use `pnpm`, and want to use some imports from `hono`, you may need to install `hono` manually or create a `.npmrc` file in your project with the following content:
|
|
57
|
+
>
|
|
58
|
+
> ```
|
|
59
|
+
> public-hoist-pattern[]=hono
|
|
60
|
+
> ```
|
|
43
61
|
|
|
44
|
-
|
|
45
|
-
|
|
62
|
+
## Easy mode
|
|
63
|
+
In your `vite.config.ts`, add the `reactRouterHonoServer` plugin.
|
|
46
64
|
|
|
47
|
-
|
|
65
|
+
```ts
|
|
66
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
67
|
+
import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
|
|
68
|
+
import { defineConfig } from "vite";
|
|
69
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
48
70
|
|
|
49
|
-
export
|
|
71
|
+
export default defineConfig({
|
|
72
|
+
plugins: [
|
|
73
|
+
reactRouterHonoServer(), // add this
|
|
74
|
+
reactRouter(),
|
|
75
|
+
tsconfigPaths()
|
|
76
|
+
],
|
|
77
|
+
});
|
|
50
78
|
```
|
|
79
|
+
**That's all!**
|
|
51
80
|
|
|
52
|
-
|
|
53
|
-
You can create your server in a separate file and export it from your _entry.server.tsx_.
|
|
81
|
+
Wait, what?
|
|
54
82
|
|
|
55
|
-
|
|
83
|
+
For really simple apps, that's all you need to do. Behind the hood, it will create a virtual module with a default Hono server.
|
|
84
|
+
When building for production, it will create the server file at `build/server/index.js` and import your React Router app from `virtual:react-router/server-build` module (replacing it with the real file located in `build/server/assets/server-build-[hash].js`).
|
|
56
85
|
|
|
57
|
-
|
|
58
|
-
|
|
86
|
+
## Configuration
|
|
87
|
+
Ok, by default it works, but you may want to customize the server and use some middleware.
|
|
59
88
|
|
|
60
|
-
|
|
89
|
+
> [!IMPORTANT]
|
|
90
|
+
> Until you define your own `serverEntryPoint`, the file name `${appDirectory}/server.ts` and the folder name `${appDirectory}/server` are reserved words.
|
|
91
|
+
>
|
|
92
|
+
> `reactRouterHonoServer` plugin is looking for them to find your server file.
|
|
61
93
|
|
|
62
|
-
export const server = await createHonoServer();
|
|
63
|
-
```
|
|
64
|
-
```ts
|
|
65
|
-
// app/entry.server.tsx
|
|
66
94
|
|
|
67
|
-
|
|
95
|
+
### Add the Vite plugin
|
|
96
|
+
> [!NOTE]
|
|
97
|
+
> It uses the `reactRouter` plugin to build your app and will automatically load its config.
|
|
68
98
|
|
|
69
|
-
|
|
99
|
+
#### Node
|
|
100
|
+
> [!TIP]
|
|
101
|
+
> Check this [example](./examples/react-router) to see how to use it.
|
|
70
102
|
|
|
71
|
-
### Add the Vite plugin
|
|
72
103
|
```ts
|
|
73
104
|
// vite.config.ts
|
|
74
|
-
|
|
75
|
-
import {
|
|
76
|
-
import { installGlobals } from "@remix-run/node";
|
|
77
|
-
import { devServer } from "react-router-hono-server/dev";
|
|
105
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
106
|
+
import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
|
|
78
107
|
import { defineConfig } from "vite";
|
|
79
108
|
import tsconfigPaths from "vite-tsconfig-paths";
|
|
80
109
|
|
|
81
|
-
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
plugins: [
|
|
112
|
+
reactRouterHonoServer(), // add this
|
|
113
|
+
reactRouter(),
|
|
114
|
+
tsconfigPaths()
|
|
115
|
+
],
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### Bun
|
|
120
|
+
> [!TIP]
|
|
121
|
+
> Check this [example](./examples/react-router-bun) to see how to use it.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
// vite.config.ts
|
|
125
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
126
|
+
import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
|
|
127
|
+
import { defineConfig } from "vite";
|
|
128
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
82
129
|
|
|
83
130
|
export default defineConfig({
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
131
|
+
plugins: [
|
|
132
|
+
reactRouterHonoServer({ runtime: "bun"} ), // add this
|
|
133
|
+
reactRouter(),
|
|
134
|
+
tsconfigPaths()
|
|
135
|
+
],
|
|
88
136
|
});
|
|
89
137
|
```
|
|
138
|
+
|
|
139
|
+
#### Cloudflare Workers
|
|
140
|
+
> [!TIP]
|
|
141
|
+
> Check this [example](./examples/react-router-cloudflare) to see how to use it.
|
|
142
|
+
|
|
90
143
|
> [!IMPORTANT]
|
|
91
|
-
>
|
|
144
|
+
> You need to add the `cloudflareDevProxy` plugin to use the Cloudflare Workers runtime on dev.
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// vite.config.ts
|
|
149
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
150
|
+
import { cloudflareDevProxy } from "@react-router/dev/vite/cloudflare"; // add this
|
|
151
|
+
import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
|
|
152
|
+
import { defineConfig } from "vite";
|
|
153
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
154
|
+
|
|
155
|
+
export default defineConfig({
|
|
156
|
+
plugins: [
|
|
157
|
+
cloudflareDevProxy(),
|
|
158
|
+
reactRouterHonoServer({ runtime: "cloudflare"} ), // add this
|
|
159
|
+
reactRouter(),
|
|
160
|
+
tsconfigPaths()
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Create the server
|
|
166
|
+
> [!TIP]
|
|
167
|
+
> You can use the CLI to create the server file for you.
|
|
92
168
|
>
|
|
93
|
-
> ```
|
|
94
|
-
>
|
|
95
|
-
> target: "esnext",
|
|
96
|
-
> },
|
|
169
|
+
> ```bash
|
|
170
|
+
> npx react-router-hono-server reveal file
|
|
97
171
|
> ```
|
|
98
172
|
|
|
173
|
+
In your `app` folder, create a file named `server.ts` and export **as default** the server created by `createHonoServer`.
|
|
99
174
|
|
|
100
|
-
|
|
175
|
+
```bash
|
|
176
|
+
touch app/server.ts
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
// app/server.ts
|
|
181
|
+
import { createHonoServer } from "react-router-hono-server/node";
|
|
182
|
+
|
|
183
|
+
export default await createHonoServer({/* options */});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
#### Alternative
|
|
187
|
+
You can define your server in `app/server/index.ts`.
|
|
188
|
+
|
|
189
|
+
> [!TIP]
|
|
190
|
+
> You can use the CLI to create the server file for you.
|
|
191
|
+
>
|
|
192
|
+
> ```bash
|
|
193
|
+
> npx react-router-hono-server reveal folder
|
|
194
|
+
> ```
|
|
195
|
+
|
|
196
|
+
It is useful if you have many middleware and want to keep your server file clean.
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
// app/server/index.ts
|
|
200
|
+
|
|
201
|
+
import { createHonoServer } from "react-router-hono-server/node";
|
|
202
|
+
|
|
203
|
+
export default await createHonoServer({/* options */});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### I don't like this default
|
|
207
|
+
No problem, you can define your files wherever you want.
|
|
208
|
+
|
|
209
|
+
Use the `serverEntryPoint` option of the Vite plugin `reactRouterHonoServer` to point to your server file.
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
### Update your package.json scripts
|
|
213
|
+
### Node
|
|
214
|
+
It is not an error, you can keep the React Router defaults for `build` and `dev`!
|
|
215
|
+
```json
|
|
216
|
+
"scripts": {
|
|
217
|
+
"build": "react-router build",
|
|
218
|
+
"dev": "react-router dev",
|
|
219
|
+
"start": "node ./build/server/index.js",
|
|
220
|
+
},
|
|
221
|
+
```
|
|
222
|
+
### Bun
|
|
223
|
+
It is not an error, you can keep the React Router defaults for `build`!
|
|
101
224
|
```json
|
|
102
225
|
"scripts": {
|
|
103
|
-
"build": "
|
|
104
|
-
"dev": "
|
|
105
|
-
"start": "
|
|
226
|
+
"build": "react-router build",
|
|
227
|
+
"dev": "bunx --bun vite",
|
|
228
|
+
"start": "bun ./build/server/index.js",
|
|
106
229
|
},
|
|
107
230
|
```
|
|
108
231
|
|
|
232
|
+
### Cloudflare
|
|
233
|
+
It is not an error, you can keep the React Router defaults for `build` and `dev`!
|
|
234
|
+
```json
|
|
235
|
+
"scripts": {
|
|
236
|
+
"build": "react-router build",
|
|
237
|
+
"dev": "react-router dev",
|
|
238
|
+
"start": "wrangler dev",
|
|
239
|
+
},
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### Wrangler
|
|
243
|
+
Add a file named `wrangler.toml` at the root of your project (close to `package.json`).
|
|
244
|
+
|
|
245
|
+
Adapt the `main` and `assets` fields based on your build output, if you changed them from the defaults.
|
|
246
|
+
```toml
|
|
247
|
+
workers_dev = true
|
|
248
|
+
name = "my-worker"
|
|
249
|
+
compatibility_date = "2024-11-18"
|
|
250
|
+
main = "./build/server/index.js"
|
|
251
|
+
assets = { directory = "./build/client/" }
|
|
252
|
+
```
|
|
253
|
+
|
|
109
254
|
## How it works
|
|
110
255
|
|
|
111
|
-
This helper
|
|
256
|
+
This helper works differently depending on the environment.
|
|
112
257
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
The actual server is controlled by Vite through `@hono/vite-dev-server`, and can be configured via _vite.config.ts_ `server` options.
|
|
258
|
+
In development, it uses [@hono/vite-dev-server](https://github.com/honojs/vite-plugins/tree/main/packages/dev-server) and loads your server and React Router app with `import('virtual:react-router/server-build')`.
|
|
259
|
+
It can be configured in `vite.config.ts`.
|
|
116
260
|
|
|
117
|
-
|
|
261
|
+
In `production`, it will create a standard node HTTP server listening at `HOST:PORT`.
|
|
118
262
|
You can customize the production server port using the `port` option of `createHonoServer`.
|
|
119
263
|
|
|
120
|
-
When building for production, the Hono server is compiled
|
|
264
|
+
When building for production, the Hono server is compiled as `build/server/index.js` and imports your React Router app from `assets/server-build-[hash].js`.
|
|
121
265
|
|
|
122
|
-
To run the server in production, use `
|
|
266
|
+
To run the server in production, use `node ./build/server/index.js`.
|
|
123
267
|
|
|
124
268
|
That's all!
|
|
125
269
|
|
|
126
270
|
### Options
|
|
127
271
|
|
|
272
|
+
#### `reactRouterHonoServer` (Vite Plugin)
|
|
128
273
|
```ts
|
|
129
|
-
|
|
274
|
+
type Runtime = "node" | "bun" | "cloudflare";
|
|
275
|
+
|
|
276
|
+
type ReactRouterHonoServerPluginOptions = {
|
|
130
277
|
/**
|
|
131
|
-
*
|
|
278
|
+
* The runtime to use for the server.
|
|
132
279
|
*
|
|
133
|
-
* Defaults to `
|
|
280
|
+
* Defaults to `node`.
|
|
134
281
|
*/
|
|
135
|
-
|
|
282
|
+
runtime?: Runtime;
|
|
136
283
|
/**
|
|
137
|
-
* The
|
|
284
|
+
* The path to the server file, relative to `vite.config.ts`.
|
|
138
285
|
*
|
|
139
|
-
*
|
|
140
|
-
*/
|
|
141
|
-
port?: number;
|
|
142
|
-
/**
|
|
143
|
-
* The directory where the server build files are located (defined in vite.config)
|
|
286
|
+
* If it is a folder (`app/server`), it will look for an `index.ts` file.
|
|
144
287
|
*
|
|
145
|
-
* Defaults to
|
|
288
|
+
* Defaults to `${appDirectory}/server[.ts | /index.ts]` if present.
|
|
146
289
|
*
|
|
147
|
-
*
|
|
290
|
+
* Fallback to a virtual module `virtual:react-router-hono-server/server`.
|
|
148
291
|
*/
|
|
149
|
-
|
|
292
|
+
serverEntryPoint?: string;
|
|
150
293
|
/**
|
|
151
|
-
* The
|
|
152
|
-
*
|
|
153
|
-
* Defaults to `index.js`
|
|
294
|
+
* The paths that are not served by the dev-server.
|
|
154
295
|
*
|
|
155
|
-
*
|
|
296
|
+
* Defaults include `appDirectory` content.
|
|
156
297
|
*/
|
|
157
|
-
|
|
298
|
+
dev?: {
|
|
299
|
+
/**
|
|
300
|
+
* The paths that are not served by the dev-server.
|
|
301
|
+
*
|
|
302
|
+
* Defaults include `appDirectory` content.
|
|
303
|
+
*/
|
|
304
|
+
exclude?: (string | RegExp)[];
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### `createHonoServer`
|
|
310
|
+
##### All adapters
|
|
311
|
+
```ts
|
|
312
|
+
export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
158
313
|
/**
|
|
159
|
-
*
|
|
314
|
+
* Enable the default logger
|
|
160
315
|
*
|
|
161
|
-
* Defaults to `
|
|
316
|
+
* Defaults to `true`
|
|
317
|
+
*/
|
|
318
|
+
defaultLogger?: boolean;
|
|
319
|
+
/**
|
|
320
|
+
* The port to start the server on
|
|
162
321
|
*
|
|
163
|
-
*
|
|
322
|
+
* Defaults to `process.env.PORT || 3000`
|
|
164
323
|
*/
|
|
165
|
-
|
|
324
|
+
port?: number;
|
|
166
325
|
/**
|
|
167
326
|
* Customize the Hono server, for example, adding middleware
|
|
168
327
|
*
|
|
169
|
-
* It is applied after the default middleware and before the
|
|
328
|
+
* It is applied after the default middleware and before the React Router middleware
|
|
170
329
|
*/
|
|
171
330
|
configure?: <E extends Env = BlankEnv>(server: Hono<E>) => Promise<void> | void;
|
|
172
331
|
/**
|
|
173
|
-
* Augment the
|
|
332
|
+
* Augment the React Router AppLoadContext
|
|
174
333
|
*
|
|
175
334
|
* Don't forget to declare the AppLoadContext in your app, next to where you create the Hono server
|
|
176
335
|
*
|
|
177
336
|
* ```ts
|
|
178
|
-
* declare module "
|
|
337
|
+
* declare module "react-router" {
|
|
179
338
|
* interface AppLoadContext {
|
|
180
339
|
* // Add your custom context here
|
|
340
|
+
* whatever: string;
|
|
181
341
|
* }
|
|
182
342
|
* }
|
|
183
343
|
* ```
|
|
184
344
|
*/
|
|
185
345
|
getLoadContext?: (
|
|
186
346
|
c: Context,
|
|
187
|
-
options:
|
|
347
|
+
options: {
|
|
348
|
+
build: ServerBuild;
|
|
349
|
+
mode: "development" | "production" | "test";
|
|
350
|
+
}
|
|
188
351
|
) => Promise<AppLoadContext> | AppLoadContext;
|
|
189
352
|
/**
|
|
190
353
|
* Listening listener (production mode only)
|
|
@@ -193,7 +356,7 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
193
356
|
*
|
|
194
357
|
* Defaults log the port
|
|
195
358
|
*/
|
|
196
|
-
listeningListener?: (info:
|
|
359
|
+
listeningListener?: (info: AddressInfo) => void;
|
|
197
360
|
/**
|
|
198
361
|
* Hono constructor options
|
|
199
362
|
*
|
|
@@ -203,27 +366,27 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
203
366
|
};
|
|
204
367
|
```
|
|
205
368
|
|
|
206
|
-
You can add additional Hono middleware with the `configure` function. If you
|
|
207
|
-
|
|
369
|
+
You can add additional Hono middleware with the `configure` function. If you do not provide a function, it will create a default Hono server.
|
|
370
|
+
|
|
208
371
|
The `configure` function can be async. So, make sure to `await createHonoServer()`.
|
|
209
372
|
|
|
210
|
-
If you want to set up the
|
|
373
|
+
If you want to set up the React Router `AppLoadContext`, pass in a function to `getLoadContext`.
|
|
374
|
+
|
|
211
375
|
Modify the `AppLoadContext` interface used in your app.
|
|
212
376
|
|
|
213
|
-
Since the Hono server is compiled in the same bundle as the rest of your
|
|
214
|
-
app, you can import app modules just like you normally would.
|
|
377
|
+
Since the Hono server is compiled in the same bundle as the rest of your React Router app, you can import app modules just like you normally would.
|
|
215
378
|
|
|
216
|
-
|
|
379
|
+
###### Example
|
|
217
380
|
|
|
218
381
|
```ts
|
|
219
|
-
// app/
|
|
382
|
+
// app/server.ts
|
|
220
383
|
|
|
221
384
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
222
385
|
|
|
223
386
|
/**
|
|
224
387
|
* Declare our loaders and actions context type
|
|
225
388
|
*/
|
|
226
|
-
declare module "
|
|
389
|
+
declare module "react-router" {
|
|
227
390
|
interface AppLoadContext {
|
|
228
391
|
/**
|
|
229
392
|
* The app version from the build assets
|
|
@@ -232,7 +395,7 @@ declare module "@remix-run/node" {
|
|
|
232
395
|
}
|
|
233
396
|
}
|
|
234
397
|
|
|
235
|
-
export
|
|
398
|
+
export default await createHonoServer({
|
|
236
399
|
getLoadContext(_, { build, mode }) {
|
|
237
400
|
const isProductionMode = mode === "production";
|
|
238
401
|
return {
|
|
@@ -244,16 +407,53 @@ export const server = await createHonoServer({
|
|
|
244
407
|
|
|
245
408
|
```ts
|
|
246
409
|
// app/routes/test.tsx
|
|
247
|
-
|
|
248
|
-
export async function loader({ context }:
|
|
410
|
+
import type { Route } from "./+types/test";
|
|
411
|
+
export async function loader({ context }: Route.LoaderArgs) {
|
|
249
412
|
// get the context provided from `getLoadContext`
|
|
250
413
|
return { appVersion: context.appVersion }
|
|
251
414
|
}
|
|
252
415
|
```
|
|
253
416
|
|
|
417
|
+
##### Node
|
|
418
|
+
```ts
|
|
419
|
+
export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
|
|
420
|
+
/**
|
|
421
|
+
* Listening listener (production mode only)
|
|
422
|
+
*
|
|
423
|
+
* It is called when the server is listening
|
|
424
|
+
*
|
|
425
|
+
* Defaults log the port
|
|
426
|
+
*/
|
|
427
|
+
listeningListener?: (info: AddressInfo) => void;
|
|
428
|
+
/**
|
|
429
|
+
* Customize the node server (ex: using http2)
|
|
430
|
+
*
|
|
431
|
+
* {@link https://hono.dev/docs/getting-started/nodejs#http2}
|
|
432
|
+
*/
|
|
433
|
+
customNodeServer?: CreateNodeServerOptions;
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
##### Bun
|
|
438
|
+
```ts
|
|
439
|
+
export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
|
|
440
|
+
/**
|
|
441
|
+
* Customize the bun server
|
|
442
|
+
*
|
|
443
|
+
* {@link https://bun.sh/docs/api/http#start-a-server-bun-serve}
|
|
444
|
+
*/
|
|
445
|
+
customBunServer?: Serve & ServeOptions;
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
##### Cloudflare Workers
|
|
450
|
+
```ts
|
|
451
|
+
export interface HonoServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {}
|
|
452
|
+
```
|
|
453
|
+
|
|
254
454
|
## Middleware
|
|
255
455
|
|
|
256
|
-
Middleware are functions that are called before
|
|
456
|
+
Middleware are functions that are called before React Router calls your loader/action.
|
|
257
457
|
|
|
258
458
|
Hono is the perfect tool for this, as it supports middleware out of the box.
|
|
259
459
|
|
|
@@ -266,16 +466,16 @@ See how [Shelf.nu](https://github.com/Shelf-nu/shelf.nu/blob/main/server/middlew
|
|
|
266
466
|
> [!TIP]
|
|
267
467
|
> This lib exports one middleware `cache` (`react-router-hono-server/middleware`) that you can use to cache your responses.
|
|
268
468
|
|
|
269
|
-
### Using
|
|
469
|
+
### Using remix-hono middleware
|
|
270
470
|
|
|
271
471
|
It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono) middleware with this package.
|
|
272
472
|
|
|
273
473
|
```ts
|
|
274
|
-
import { createCookieSessionStorage } from "
|
|
474
|
+
import { createCookieSessionStorage } from "react-router";
|
|
275
475
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
276
476
|
import { session } from "remix-hono/session";
|
|
277
477
|
|
|
278
|
-
export
|
|
478
|
+
export default await createHonoServer({
|
|
279
479
|
configure: (server) => {
|
|
280
480
|
server.use(
|
|
281
481
|
session({
|
|
@@ -308,7 +508,6 @@ export const server = await createHonoServer({
|
|
|
308
508
|
});
|
|
309
509
|
```
|
|
310
510
|
|
|
311
|
-
|
|
312
511
|
### Creating custom Middleware
|
|
313
512
|
|
|
314
513
|
You can create middleware using the [`createMiddleware`](https://hono.dev/docs/helpers/factory#createmiddleware) or [`createFactory`](https://hono.dev/docs/helpers/factory#createfactory) functions from `hono/factory`.
|
|
@@ -319,7 +518,7 @@ Then, use them with the `configure` function of `createHonoServer`.
|
|
|
319
518
|
import { createMiddleware } from "hono/factory";
|
|
320
519
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
321
520
|
|
|
322
|
-
export
|
|
521
|
+
export default await createHonoServer({
|
|
323
522
|
configure: (server) => {
|
|
324
523
|
server.use(
|
|
325
524
|
createMiddleware(async (c, next) => {
|
|
@@ -331,6 +530,88 @@ export const server = await createHonoServer({
|
|
|
331
530
|
});
|
|
332
531
|
```
|
|
333
532
|
|
|
533
|
+
### Migrate from v1
|
|
534
|
+
_You should not expect any breaking changes._
|
|
535
|
+
|
|
536
|
+
#### Install the latest version
|
|
537
|
+
|
|
538
|
+
```bash
|
|
539
|
+
npm install react-router-hono-server@latest
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### Create the server file
|
|
543
|
+
##### Option 1 - You previously had all your server code in `app/entry.server.tsx`
|
|
544
|
+
```bash
|
|
545
|
+
touch app/server.ts
|
|
546
|
+
```
|
|
547
|
+
or
|
|
548
|
+
```bash
|
|
549
|
+
npx react-router-hono-server reveal file
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
##### Option 2 - You previously had your server code in a `server` folder
|
|
553
|
+
```bash
|
|
554
|
+
mkdir app/server
|
|
555
|
+
touch app/server/index.ts
|
|
556
|
+
```
|
|
557
|
+
or
|
|
558
|
+
```bash
|
|
559
|
+
npx react-router-hono-server reveal folder
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
#### Move your server code
|
|
563
|
+
Move your previous server code to the new file you created in the previous step.
|
|
564
|
+
|
|
565
|
+
> [!NOTE]
|
|
566
|
+
> You can remove the import from `react-router-hono-server/node` in your `entry.server.tsx` file and any other server code.
|
|
567
|
+
|
|
568
|
+
Many options are gone, `serverBuildFile` `assetsDir` and `buildDirectory`.
|
|
569
|
+
|
|
570
|
+
We now use the Vite virtual import `virtual:react-router/server-build` to load the server build and we read the Vite config thanks to the `reactRouterHonoServer` plugin.
|
|
571
|
+
|
|
572
|
+
> [!IMPORTANT]
|
|
573
|
+
> You now need to export the server created by `createHonoServer` as **default**.
|
|
574
|
+
>
|
|
575
|
+
> ```ts
|
|
576
|
+
> import { createHonoServer } from "react-router-hono-server/node";
|
|
577
|
+
>
|
|
578
|
+
> export default await createHonoServer({/* other options */});
|
|
579
|
+
> ```
|
|
580
|
+
|
|
581
|
+
#### Update your `vite.config.ts`
|
|
582
|
+
|
|
583
|
+
> [!IMPORTANT]
|
|
584
|
+
> `devServer` is now `reactRouterHonoServer`.
|
|
585
|
+
|
|
586
|
+
Many options are gone or have changed.
|
|
587
|
+
|
|
588
|
+
`exportName` (`reactRouterHonoServer` expects a default export from your server file), `entry` is now `serverEntryPoint`. `appDirectory` is removed (read from `vite.config.ts`), and `exclude` has been moved under `dev`.
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
##### You used `buildEnd` from `remix()` plugin or a custom `buildDirectory` option
|
|
592
|
+
You may know that it has been moved to `react-router.config.ts` (see [here](https://reactrouter.com/upgrading/remix#5-add-a-react-router-config) for more information).
|
|
593
|
+
|
|
594
|
+
If you used this hook for Sentry, check this [example](./examples/react-router-sentry/react-router.config.ts) to see how to migrate.
|
|
595
|
+
|
|
596
|
+
If you used a custom `buildDirectory` option, check this [example](./examples/react-router-custom-build/react-router.config.ts) to see how to migrate.
|
|
597
|
+
|
|
598
|
+
#### Update your package.json scripts
|
|
599
|
+
```json
|
|
600
|
+
"scripts": {
|
|
601
|
+
"build": "react-router build",
|
|
602
|
+
"dev": "react-router dev",
|
|
603
|
+
"start": "node ./build/server/index.js",
|
|
604
|
+
},
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
## Special Thanks
|
|
608
|
+
|
|
609
|
+
Inspired by [remix-express-vite-plugin](https://github.com/kiliman/remix-express-vite-plugin) from [@kiliman](https://github.com/kiliman)
|
|
610
|
+
|
|
611
|
+
`remix` handler was forked from [remix-hono](https://github.com/sergiodxa/remix-hono) by [@sergiodxa](https://github.com/sergiodxa) as it is a small and simple core dependency of this library.
|
|
612
|
+
|
|
613
|
+
I will still help maintain it.
|
|
614
|
+
|
|
334
615
|
## Contributors ✨
|
|
335
616
|
|
|
336
617
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|