react-router-hono-server 1.2.0 → 2.0.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 +256 -100
- package/dist/chunk-ENK5WJB4.mjs +18 -0
- package/dist/cli.mjs +35 -0
- package/dist/dev.d.mts +17 -28
- package/dist/dev.mjs +129 -23
- package/dist/middleware.d.mts +10 -0
- package/dist/middleware.mjs +6 -0
- package/dist/node.d.mts +13 -33
- package/dist/node.mjs +75 -66
- package/package.json +24 -23
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,122 +7,184 @@ 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
35
|
> Only works for **node**
|
|
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
|
|
|
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
|
+
> ```
|
|
61
|
+
|
|
62
|
+
## Easy mode
|
|
63
|
+
In your `vite.config.ts`, add the `reactRouterHonoServer` plugin.
|
|
64
|
+
|
|
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";
|
|
70
|
+
|
|
71
|
+
export default defineConfig({
|
|
72
|
+
plugins: [
|
|
73
|
+
reactRouterHonoServer(), // add this
|
|
74
|
+
reactRouter(),
|
|
75
|
+
tsconfigPaths()
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
**That's all!**
|
|
80
|
+
|
|
81
|
+
Wait, what?
|
|
82
|
+
|
|
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`).
|
|
85
|
+
|
|
39
86
|
## Configuration
|
|
87
|
+
Ok, by default it works, but you may want to customize the server and use some middleware.
|
|
88
|
+
|
|
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.
|
|
93
|
+
|
|
40
94
|
### Create the server
|
|
41
|
-
|
|
42
|
-
|
|
95
|
+
> [!TIP]
|
|
96
|
+
> You can use the CLI to create the server file for you.
|
|
97
|
+
>
|
|
98
|
+
> ```bash
|
|
99
|
+
> npx react-router-hono-server reveal file
|
|
100
|
+
> ```
|
|
43
101
|
|
|
44
|
-
|
|
45
|
-
|
|
102
|
+
In your `app` folder, create a file named `server.ts` and export **as default** the server created by `createHonoServer`.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
touch app/server.ts
|
|
106
|
+
```
|
|
46
107
|
|
|
108
|
+
```ts
|
|
109
|
+
// app/server.ts
|
|
47
110
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
48
111
|
|
|
49
|
-
export
|
|
112
|
+
export default await createHonoServer({/* options */});
|
|
50
113
|
```
|
|
51
114
|
|
|
52
115
|
#### Alternative
|
|
53
|
-
You can
|
|
116
|
+
You can define your server in `app/server/index.ts`.
|
|
54
117
|
|
|
55
|
-
|
|
118
|
+
> [!TIP]
|
|
119
|
+
> You can use the CLI to create the server file for you.
|
|
120
|
+
>
|
|
121
|
+
> ```bash
|
|
122
|
+
> npx react-router-hono-server reveal folder
|
|
123
|
+
> ```
|
|
124
|
+
|
|
125
|
+
It is useful if you have many middleware and want to keep your server file clean.
|
|
56
126
|
|
|
57
127
|
```ts
|
|
58
|
-
// app/server.ts
|
|
128
|
+
// app/server/index.ts
|
|
59
129
|
|
|
60
130
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
61
131
|
|
|
62
|
-
export
|
|
132
|
+
export default await createHonoServer({/* options */});
|
|
63
133
|
```
|
|
64
|
-
```ts
|
|
65
|
-
// app/entry.server.tsx
|
|
66
134
|
|
|
67
|
-
|
|
135
|
+
#### I don't like this default
|
|
136
|
+
No problem, you can define your files wherever you want.
|
|
68
137
|
|
|
69
|
-
|
|
138
|
+
Use the `serverEntryPoint` option of `reactRouterHonoServer` to point to your server file.
|
|
70
139
|
|
|
71
|
-
### Add the Vite plugin
|
|
140
|
+
### Add the Vite plugin (if not already)
|
|
72
141
|
```ts
|
|
73
142
|
// vite.config.ts
|
|
74
|
-
|
|
75
|
-
import {
|
|
76
|
-
import { installGlobals } from "@remix-run/node";
|
|
77
|
-
import { devServer } from "react-router-hono-server/dev";
|
|
143
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
144
|
+
import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
|
|
78
145
|
import { defineConfig } from "vite";
|
|
79
146
|
import tsconfigPaths from "vite-tsconfig-paths";
|
|
80
147
|
|
|
81
|
-
installGlobals();
|
|
82
|
-
|
|
83
148
|
export default defineConfig({
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
149
|
+
plugins: [
|
|
150
|
+
reactRouterHonoServer(), // add this
|
|
151
|
+
reactRouter(),
|
|
152
|
+
tsconfigPaths()
|
|
153
|
+
],
|
|
88
154
|
});
|
|
89
155
|
```
|
|
90
|
-
> [!
|
|
91
|
-
>
|
|
92
|
-
>
|
|
93
|
-
> ```ts
|
|
94
|
-
> build: {
|
|
95
|
-
> target: "esnext",
|
|
96
|
-
> },
|
|
97
|
-
> ```
|
|
156
|
+
> [!NOTE]
|
|
157
|
+
> It uses the `reactRouter` plugin to build your app and will automatically load its config.
|
|
98
158
|
|
|
99
159
|
|
|
100
|
-
### Update package.json scripts
|
|
160
|
+
### Update your package.json scripts
|
|
161
|
+
It is not an error, you can keep the React Router defaults for `build` and `dev`!
|
|
101
162
|
```json
|
|
102
163
|
"scripts": {
|
|
103
|
-
"build": "
|
|
104
|
-
"dev": "
|
|
105
|
-
"start": "NODE_ENV=production node ./build/server/index.js"
|
|
164
|
+
"build": "react-router build",
|
|
165
|
+
"dev": "react-router dev",
|
|
166
|
+
"start": "NODE_ENV=production node ./build/server/index.js",
|
|
106
167
|
},
|
|
107
168
|
```
|
|
108
169
|
|
|
109
170
|
## How it works
|
|
110
171
|
|
|
111
|
-
This helper
|
|
172
|
+
This helper works differently depending on the environment.
|
|
112
173
|
|
|
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.
|
|
174
|
+
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')`.
|
|
175
|
+
It can be configured in `vite.config.ts`.
|
|
116
176
|
|
|
117
|
-
|
|
177
|
+
In `production`, it will create a standard node HTTP server listening at `HOST:PORT`.
|
|
118
178
|
You can customize the production server port using the `port` option of `createHonoServer`.
|
|
119
179
|
|
|
120
|
-
When building for production, the Hono server is compiled
|
|
180
|
+
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
181
|
|
|
122
182
|
To run the server in production, use `NODE_ENV=production node ./build/server/index.js`.
|
|
123
183
|
|
|
124
184
|
That's all!
|
|
125
185
|
|
|
126
186
|
### Options
|
|
127
|
-
|
|
187
|
+
#### `createHonoServer`
|
|
128
188
|
```ts
|
|
129
189
|
export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
130
190
|
/**
|
|
@@ -139,52 +199,32 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
139
199
|
* Defaults to `process.env.PORT || 3000`
|
|
140
200
|
*/
|
|
141
201
|
port?: number;
|
|
142
|
-
/**
|
|
143
|
-
* The directory where the server build files are located (defined in vite.config)
|
|
144
|
-
*
|
|
145
|
-
* Defaults to `build/server`
|
|
146
|
-
*
|
|
147
|
-
* See https://remix.run/docs/en/main/file-conventions/vite-config#builddirectory
|
|
148
|
-
*/
|
|
149
|
-
buildDirectory?: string;
|
|
150
|
-
/**
|
|
151
|
-
* The file name of the server build file (defined in vite.config)
|
|
152
|
-
*
|
|
153
|
-
* Defaults to `index.js`
|
|
154
|
-
*
|
|
155
|
-
* See https://remix.run/docs/en/main/file-conventions/vite-config#serverbuildfile
|
|
156
|
-
*/
|
|
157
|
-
serverBuildFile?: `${string}.js`;
|
|
158
|
-
/**
|
|
159
|
-
* The directory where the assets are located (defined in vite.config, build.assetsDir)
|
|
160
|
-
*
|
|
161
|
-
* Defaults to `assets`
|
|
162
|
-
*
|
|
163
|
-
* See https://vitejs.dev/config/build-options#build-assetsdir
|
|
164
|
-
*/
|
|
165
|
-
assetsDir?: string;
|
|
166
202
|
/**
|
|
167
203
|
* Customize the Hono server, for example, adding middleware
|
|
168
204
|
*
|
|
169
|
-
* It is applied after the default middleware and before the
|
|
205
|
+
* It is applied after the default middleware and before the React Router middleware
|
|
170
206
|
*/
|
|
171
207
|
configure?: <E extends Env = BlankEnv>(server: Hono<E>) => Promise<void> | void;
|
|
172
208
|
/**
|
|
173
|
-
* Augment the
|
|
209
|
+
* Augment the React Router AppLoadContext
|
|
174
210
|
*
|
|
175
211
|
* Don't forget to declare the AppLoadContext in your app, next to where you create the Hono server
|
|
176
212
|
*
|
|
177
213
|
* ```ts
|
|
178
|
-
* declare module "
|
|
214
|
+
* declare module "react-router" {
|
|
179
215
|
* interface AppLoadContext {
|
|
180
216
|
* // Add your custom context here
|
|
217
|
+
* whatever: string;
|
|
181
218
|
* }
|
|
182
219
|
* }
|
|
183
220
|
* ```
|
|
184
221
|
*/
|
|
185
222
|
getLoadContext?: (
|
|
186
223
|
c: Context,
|
|
187
|
-
options:
|
|
224
|
+
options: {
|
|
225
|
+
build: ServerBuild;
|
|
226
|
+
mode: "development" | "production" | "test";
|
|
227
|
+
}
|
|
188
228
|
) => Promise<AppLoadContext> | AppLoadContext;
|
|
189
229
|
/**
|
|
190
230
|
* Listening listener (production mode only)
|
|
@@ -193,37 +233,43 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
193
233
|
*
|
|
194
234
|
* Defaults log the port
|
|
195
235
|
*/
|
|
196
|
-
listeningListener?: (info:
|
|
236
|
+
listeningListener?: (info: AddressInfo) => void;
|
|
197
237
|
/**
|
|
198
238
|
* Hono constructor options
|
|
199
239
|
*
|
|
200
240
|
* {@link HonoOptions}
|
|
201
241
|
*/
|
|
202
242
|
honoOptions?: HonoOptions<E>;
|
|
243
|
+
/**
|
|
244
|
+
* Customize the node server (ex: using http2)
|
|
245
|
+
*
|
|
246
|
+
* {@link https://hono.dev/docs/getting-started/nodejs#http2}
|
|
247
|
+
*/
|
|
248
|
+
customNodeServer?: CreateNodeServerOptions;
|
|
203
249
|
};
|
|
204
250
|
```
|
|
205
251
|
|
|
206
|
-
You can add additional Hono middleware with the `configure` function. If you
|
|
207
|
-
|
|
252
|
+
You can add additional Hono middleware with the `configure` function. If you do not provide a function, it will create a default Hono server.
|
|
253
|
+
|
|
208
254
|
The `configure` function can be async. So, make sure to `await createHonoServer()`.
|
|
209
255
|
|
|
210
|
-
If you want to set up the
|
|
256
|
+
If you want to set up the React Router `AppLoadContext`, pass in a function to `getLoadContext`.
|
|
257
|
+
|
|
211
258
|
Modify the `AppLoadContext` interface used in your app.
|
|
212
259
|
|
|
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.
|
|
260
|
+
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
261
|
|
|
216
|
-
|
|
262
|
+
##### Example
|
|
217
263
|
|
|
218
264
|
```ts
|
|
219
|
-
// app/
|
|
265
|
+
// app/server.ts
|
|
220
266
|
|
|
221
267
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
222
268
|
|
|
223
269
|
/**
|
|
224
270
|
* Declare our loaders and actions context type
|
|
225
271
|
*/
|
|
226
|
-
declare module "
|
|
272
|
+
declare module "react-router" {
|
|
227
273
|
interface AppLoadContext {
|
|
228
274
|
/**
|
|
229
275
|
* The app version from the build assets
|
|
@@ -232,7 +278,7 @@ declare module "@remix-run/node" {
|
|
|
232
278
|
}
|
|
233
279
|
}
|
|
234
280
|
|
|
235
|
-
export
|
|
281
|
+
export default await createHonoServer({
|
|
236
282
|
getLoadContext(_, { build, mode }) {
|
|
237
283
|
const isProductionMode = mode === "production";
|
|
238
284
|
return {
|
|
@@ -244,16 +290,45 @@ export const server = await createHonoServer({
|
|
|
244
290
|
|
|
245
291
|
```ts
|
|
246
292
|
// app/routes/test.tsx
|
|
247
|
-
|
|
248
|
-
export async function loader({ context }:
|
|
293
|
+
import type { Route } from "./+types/test";
|
|
294
|
+
export async function loader({ context }: Route.LoaderArgs) {
|
|
249
295
|
// get the context provided from `getLoadContext`
|
|
250
296
|
return { appVersion: context.appVersion }
|
|
251
297
|
}
|
|
252
298
|
```
|
|
253
299
|
|
|
300
|
+
#### `reactRouterHonoServer`
|
|
301
|
+
```ts
|
|
302
|
+
type ReactRouterHonoServerPluginOptions = {
|
|
303
|
+
/**
|
|
304
|
+
* The path to the server file, relative to `vite.config.ts`.
|
|
305
|
+
*
|
|
306
|
+
* If it is a folder (`app/server`), it will look for an `index.ts` file.
|
|
307
|
+
*
|
|
308
|
+
* Defaults to `${appDirectory}/server[.ts | /index.ts]` if present.
|
|
309
|
+
*
|
|
310
|
+
* Fallback to a virtual module `virtual:react-router-hono-server/server`.
|
|
311
|
+
*/
|
|
312
|
+
serverEntryPoint?: string;
|
|
313
|
+
/**
|
|
314
|
+
* The paths that are not served by the dev-server.
|
|
315
|
+
*
|
|
316
|
+
* Defaults include `appDirectory` content.
|
|
317
|
+
*/
|
|
318
|
+
dev?: {
|
|
319
|
+
/**
|
|
320
|
+
* The paths that are not served by the dev-server.
|
|
321
|
+
*
|
|
322
|
+
* Defaults include `appDirectory` content.
|
|
323
|
+
*/
|
|
324
|
+
exclude?: (string | RegExp)[];
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
```
|
|
328
|
+
|
|
254
329
|
## Middleware
|
|
255
330
|
|
|
256
|
-
Middleware are functions that are called before
|
|
331
|
+
Middleware are functions that are called before React Router calls your loader/action.
|
|
257
332
|
|
|
258
333
|
Hono is the perfect tool for this, as it supports middleware out of the box.
|
|
259
334
|
|
|
@@ -266,16 +341,16 @@ See how [Shelf.nu](https://github.com/Shelf-nu/shelf.nu/blob/main/server/middlew
|
|
|
266
341
|
> [!TIP]
|
|
267
342
|
> This lib exports one middleware `cache` (`react-router-hono-server/middleware`) that you can use to cache your responses.
|
|
268
343
|
|
|
269
|
-
### Using
|
|
344
|
+
### Using remix-hono middleware
|
|
270
345
|
|
|
271
346
|
It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono) middleware with this package.
|
|
272
347
|
|
|
273
348
|
```ts
|
|
274
|
-
import { createCookieSessionStorage } from "
|
|
349
|
+
import { createCookieSessionStorage } from "react-router";
|
|
275
350
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
276
351
|
import { session } from "remix-hono/session";
|
|
277
352
|
|
|
278
|
-
export
|
|
353
|
+
export default await createHonoServer({
|
|
279
354
|
configure: (server) => {
|
|
280
355
|
server.use(
|
|
281
356
|
session({
|
|
@@ -308,7 +383,6 @@ export const server = await createHonoServer({
|
|
|
308
383
|
});
|
|
309
384
|
```
|
|
310
385
|
|
|
311
|
-
|
|
312
386
|
### Creating custom Middleware
|
|
313
387
|
|
|
314
388
|
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 +393,7 @@ Then, use them with the `configure` function of `createHonoServer`.
|
|
|
319
393
|
import { createMiddleware } from "hono/factory";
|
|
320
394
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
321
395
|
|
|
322
|
-
export
|
|
396
|
+
export default await createHonoServer({
|
|
323
397
|
configure: (server) => {
|
|
324
398
|
server.use(
|
|
325
399
|
createMiddleware(async (c, next) => {
|
|
@@ -331,6 +405,88 @@ export const server = await createHonoServer({
|
|
|
331
405
|
});
|
|
332
406
|
```
|
|
333
407
|
|
|
408
|
+
### Migrate from v1
|
|
409
|
+
_You should not expect any breaking changes._
|
|
410
|
+
|
|
411
|
+
#### Install the latest version
|
|
412
|
+
|
|
413
|
+
```bash
|
|
414
|
+
npm install react-router-hono-server@latest
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
#### Create the server file
|
|
418
|
+
##### Option 1 - You previously had all your server code in `app/entry.server.tsx`
|
|
419
|
+
```bash
|
|
420
|
+
touch app/server.ts
|
|
421
|
+
```
|
|
422
|
+
or
|
|
423
|
+
```bash
|
|
424
|
+
npx react-router-hono-server reveal file
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
##### Option 2 - You previously had your server code in a `server` folder
|
|
428
|
+
```bash
|
|
429
|
+
mkdir app/server
|
|
430
|
+
touch app/server/index.ts
|
|
431
|
+
```
|
|
432
|
+
or
|
|
433
|
+
```bash
|
|
434
|
+
npx react-router-hono-server reveal folder
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
#### Move your server code
|
|
438
|
+
Move your previous server code to the new file you created in the previous step.
|
|
439
|
+
|
|
440
|
+
> [!NOTE]
|
|
441
|
+
> You can remove the import from `react-router-hono-server/node` in your `entry.server.tsx` file and any other server code.
|
|
442
|
+
|
|
443
|
+
Many options are gone, `serverBuildFile` `assetsDir` and `buildDirectory`.
|
|
444
|
+
|
|
445
|
+
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.
|
|
446
|
+
|
|
447
|
+
> [!IMPORTANT]
|
|
448
|
+
> You now need to export the server created by `createHonoServer` as **default**.
|
|
449
|
+
>
|
|
450
|
+
> ```ts
|
|
451
|
+
> import { createHonoServer } from "react-router-hono-server/node";
|
|
452
|
+
>
|
|
453
|
+
> export default await createHonoServer({/* other options */});
|
|
454
|
+
> ```
|
|
455
|
+
|
|
456
|
+
#### Update your `vite.config.ts`
|
|
457
|
+
|
|
458
|
+
> [!IMPORTANT]
|
|
459
|
+
> `devServer` is now `reactRouterHonoServer`.
|
|
460
|
+
|
|
461
|
+
Many options are gone or have changed.
|
|
462
|
+
|
|
463
|
+
`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`.
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
##### You used `buildEnd` from `remix()` plugin or a custom `buildDirectory` option
|
|
467
|
+
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).
|
|
468
|
+
|
|
469
|
+
If you used this hook for Sentry, check this [example](./examples/react-router-sentry/react-router.config.ts) to see how to migrate.
|
|
470
|
+
|
|
471
|
+
If you used a custom `buildDirectory` option, check this [example](./examples/react-router-custom-build/react-router.config.ts) to see how to migrate.
|
|
472
|
+
|
|
473
|
+
#### Update your package.json scripts
|
|
474
|
+
```json
|
|
475
|
+
"scripts": {
|
|
476
|
+
"build": "react-router build",
|
|
477
|
+
"dev": "react-router dev",
|
|
478
|
+
"start": "NODE_ENV=production node ./build/server/index.js",
|
|
479
|
+
},
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
## Special Thanks
|
|
483
|
+
|
|
484
|
+
Inspired by [remix-express-vite-plugin](https://github.com/kiliman/remix-express-vite-plugin) from [@kiliman](https://github.com/kiliman)
|
|
485
|
+
|
|
486
|
+
`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.
|
|
487
|
+
|
|
488
|
+
I will still help maintain it.
|
|
489
|
+
|
|
334
490
|
## Contributors ✨
|
|
335
491
|
|
|
336
492
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/middleware.ts
|
|
2
|
+
import { createMiddleware } from "hono/factory";
|
|
3
|
+
function cache(seconds) {
|
|
4
|
+
return createMiddleware(async (c, next) => {
|
|
5
|
+
if (!c.req.path.match(/\.[a-zA-Z0-9]+$/) || c.req.path.endsWith(".data")) {
|
|
6
|
+
return next();
|
|
7
|
+
}
|
|
8
|
+
await next();
|
|
9
|
+
if (!c.res.ok) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
c.res.headers.set("cache-control", `public, max-age=${seconds}`);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
cache
|
|
18
|
+
};
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// src/cli.ts
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { command, run } from "@drizzle-team/brocli";
|
|
6
|
+
var serverFile = `// generated by react-router-hono-server/dev
|
|
7
|
+
import { createHonoServer } from "react-router-hono-server/node";
|
|
8
|
+
|
|
9
|
+
export default await createHonoServer();`;
|
|
10
|
+
var file = command({
|
|
11
|
+
name: "file",
|
|
12
|
+
async handler() {
|
|
13
|
+
const serverFilePath = path.resolve(process.cwd(), "app", "server.ts");
|
|
14
|
+
fs.writeFileSync(serverFilePath, serverFile);
|
|
15
|
+
console.log(`\x1B[33m\u2728 File created at ${serverFilePath}\x1B[0m`);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var folder = command({
|
|
19
|
+
name: "folder",
|
|
20
|
+
async handler() {
|
|
21
|
+
const serverFilePath = path.resolve(process.cwd(), "app", "server", "index.ts");
|
|
22
|
+
if (!fs.existsSync(serverFilePath)) {
|
|
23
|
+
fs.mkdirSync(path.dirname(serverFilePath), { recursive: true });
|
|
24
|
+
}
|
|
25
|
+
fs.writeFileSync(serverFilePath, serverFile);
|
|
26
|
+
console.log(`\x1B[33m\u2728 Folder and file created at ${serverFilePath}\x1B[0m`);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
var reveal = command({
|
|
30
|
+
name: "reveal",
|
|
31
|
+
subcommands: [file, folder]
|
|
32
|
+
});
|
|
33
|
+
run([reveal], {
|
|
34
|
+
name: "React Router Hono Server CLI"
|
|
35
|
+
});
|
package/dist/dev.d.mts
CHANGED
|
@@ -1,41 +1,30 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
2
|
|
|
3
|
-
type
|
|
3
|
+
type ReactRouterHonoServerPluginOptions = {
|
|
4
4
|
/**
|
|
5
|
-
* The path to the
|
|
5
|
+
* The path to the server file, relative to `vite.config.ts`.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
appDirectory?: string;
|
|
10
|
-
/**
|
|
11
|
-
* The path to the entry file, relative to `vite.config.ts`.
|
|
12
|
-
*
|
|
13
|
-
* Defaults to `"app/entry.server.tsx"`.
|
|
14
|
-
*/
|
|
15
|
-
entry?: string;
|
|
16
|
-
/**
|
|
17
|
-
* The name of the export from the entry file.
|
|
7
|
+
* If it is a folder (`app/server`), it will look for an `index.ts` file.
|
|
18
8
|
*
|
|
19
|
-
* Defaults to
|
|
9
|
+
* Defaults to `${appDirectory}/server[.ts | /index.ts]` if present.
|
|
20
10
|
*
|
|
21
|
-
*
|
|
22
|
-
* ```ts
|
|
23
|
-
* export const server = createHonoServer();
|
|
24
|
-
* ```
|
|
11
|
+
* Fallback to a virtual module `virtual:react-router-hono-server/server`.
|
|
25
12
|
*/
|
|
26
|
-
|
|
13
|
+
serverEntryPoint?: string;
|
|
27
14
|
/**
|
|
28
15
|
* The paths that are not served by the dev-server.
|
|
29
16
|
*
|
|
30
17
|
* Defaults include `appDirectory` content.
|
|
31
18
|
*/
|
|
32
|
-
|
|
19
|
+
dev?: {
|
|
20
|
+
/**
|
|
21
|
+
* The paths that are not served by the dev-server.
|
|
22
|
+
*
|
|
23
|
+
* Defaults include `appDirectory` content.
|
|
24
|
+
*/
|
|
25
|
+
exclude?: (string | RegExp)[];
|
|
26
|
+
};
|
|
33
27
|
};
|
|
34
|
-
|
|
35
|
-
* Create a dev server for the Hono server
|
|
36
|
-
*
|
|
37
|
-
* @param config {@link HonoDevServerOptions} - The configuration options for the dev server
|
|
38
|
-
*/
|
|
39
|
-
declare function devServer(config?: HonoDevServerOptions): vite.Plugin<any>;
|
|
28
|
+
declare function reactRouterHonoServer(options?: ReactRouterHonoServerPluginOptions): Plugin;
|
|
40
29
|
|
|
41
|
-
export {
|
|
30
|
+
export { reactRouterHonoServer };
|
package/dist/dev.mjs
CHANGED
|
@@ -1,28 +1,134 @@
|
|
|
1
1
|
// src/dev.ts
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import honoDevServer from "@hono/vite-dev-server";
|
|
5
|
+
var virtualModuleId = "\0virtual:react-router-hono-server/server";
|
|
6
|
+
function reactRouterHonoServer(options = {}) {
|
|
7
|
+
let pluginConfig;
|
|
8
|
+
let devServerPlugin;
|
|
9
|
+
return {
|
|
10
|
+
name: "react-router-hono-server",
|
|
11
|
+
enforce: "post",
|
|
12
|
+
resolveId(id) {
|
|
13
|
+
if (id === virtualModuleId) {
|
|
14
|
+
return id;
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
load(id) {
|
|
18
|
+
if (id === virtualModuleId) {
|
|
19
|
+
return `
|
|
20
|
+
// generated by react-router-hono-server/dev
|
|
21
|
+
import { createHonoServer } from "react-router-hono-server/node";
|
|
22
|
+
|
|
23
|
+
export default await createHonoServer();
|
|
24
|
+
`;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
config(config) {
|
|
28
|
+
pluginConfig = resolvePluginConfig(config, options);
|
|
29
|
+
if (!pluginConfig) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const baseConfig = {
|
|
33
|
+
// Define environment variables that are hot-swapped during development and SSR build
|
|
34
|
+
define: {
|
|
35
|
+
"import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY": JSON.stringify(pluginConfig.buildDirectory),
|
|
36
|
+
"import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR": JSON.stringify(pluginConfig.assetsDir)
|
|
37
|
+
},
|
|
38
|
+
ssr: {
|
|
39
|
+
// Ensure our package is not externalized during SSR build
|
|
40
|
+
// This is necessary because we are using a virtual import to load the React Router server entry point
|
|
41
|
+
noExternal: ["react-router-hono-server"]
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
if (!pluginConfig.isSsrBuild) {
|
|
45
|
+
return baseConfig;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
...baseConfig,
|
|
49
|
+
build: {
|
|
50
|
+
target: "esnext",
|
|
51
|
+
rollupOptions: {
|
|
52
|
+
input: pluginConfig.serverEntryPoint
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
configureServer(server) {
|
|
58
|
+
if (!pluginConfig) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (devServerPlugin) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
devServerPlugin = honoDevServer({
|
|
65
|
+
injectClientScript: false,
|
|
66
|
+
entry: pluginConfig.serverEntryPoint,
|
|
67
|
+
export: "default",
|
|
68
|
+
exclude: [
|
|
69
|
+
`/${pluginConfig.appDirectory}/**/*`,
|
|
70
|
+
`/${pluginConfig.appDirectory}/**/.*/**`,
|
|
71
|
+
/^\/@.+$/,
|
|
72
|
+
/^\/node_modules\/.*/,
|
|
73
|
+
/\?import$/,
|
|
74
|
+
...pluginConfig.dev?.exclude || []
|
|
75
|
+
]
|
|
76
|
+
});
|
|
77
|
+
if (typeof devServerPlugin.configureServer === "function") {
|
|
78
|
+
devServerPlugin.configureServer(server);
|
|
79
|
+
} else {
|
|
80
|
+
console.error("Dev server plugin configureServer hook is not a function. This is likely a bug, I guess \u{1F605}\n");
|
|
81
|
+
throw new Error("Cannot apply dev server plugin configureServer hook");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
10
84
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
85
|
+
}
|
|
86
|
+
function resolvePluginConfig(config, options) {
|
|
87
|
+
if (!("__reactRouterPluginContext" in config)) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const reactRouterConfig = config.__reactRouterPluginContext;
|
|
91
|
+
const rootDirectory = reactRouterConfig.rootDirectory;
|
|
92
|
+
const buildDirectory = path.relative(rootDirectory, reactRouterConfig.reactRouterConfig.buildDirectory);
|
|
93
|
+
const appDirectory = path.relative(rootDirectory, reactRouterConfig.reactRouterConfig.appDirectory);
|
|
94
|
+
const isSsrBuild = reactRouterConfig.isSsrBuild;
|
|
95
|
+
const assetsDir = config.build?.assetsDir || "assets";
|
|
96
|
+
const serverEntryPoint = options.serverEntryPoint || findDefaultServerEntry(appDirectory);
|
|
97
|
+
return {
|
|
98
|
+
rootDirectory,
|
|
99
|
+
buildDirectory,
|
|
100
|
+
appDirectory,
|
|
101
|
+
isSsrBuild,
|
|
102
|
+
assetsDir,
|
|
103
|
+
serverEntryPoint,
|
|
104
|
+
dev: options.dev
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
var warned = false;
|
|
108
|
+
function findDefaultServerEntry(appDirectory) {
|
|
109
|
+
const fileWay = `${appDirectory}/server.ts`;
|
|
110
|
+
const folderWay = `${appDirectory}/server/index.ts`;
|
|
111
|
+
if (fs.existsSync(fileWay)) {
|
|
112
|
+
return fileWay;
|
|
113
|
+
}
|
|
114
|
+
if (fs.existsSync(folderWay)) {
|
|
115
|
+
return folderWay;
|
|
116
|
+
}
|
|
117
|
+
if (!warned) {
|
|
118
|
+
console.warn(
|
|
119
|
+
`\x1B[33mNo server entry point found.
|
|
120
|
+
Will use a virtual module (${virtualModuleId}) with a default Hono server.
|
|
121
|
+
|
|
122
|
+
To customize the server, create one of the following files:
|
|
123
|
+
- ${fileWay} (npx react-router-hono-server reveal file)
|
|
124
|
+
- ${folderWay} (npx react-router-hono-server reveal folder)
|
|
125
|
+
You can also set the \`serverEntryPoint\` option in the reactRouterHonoServer plugin for more control.\x1B[0m
|
|
126
|
+
`
|
|
127
|
+
);
|
|
128
|
+
warned = true;
|
|
129
|
+
}
|
|
130
|
+
return virtualModuleId;
|
|
25
131
|
}
|
|
26
132
|
export {
|
|
27
|
-
|
|
133
|
+
reactRouterHonoServer
|
|
28
134
|
};
|
package/dist/node.d.mts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import * as hono_types from 'hono/types';
|
|
2
2
|
import { BlankEnv } from 'hono/types';
|
|
3
|
-
import { ServerOptions, createServer } from 'node:http';
|
|
4
|
-
import { ServerOptions as ServerOptions$2, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
5
|
-
import { ServerOptions as ServerOptions$1, createServer as createServer$1 } from 'node:https';
|
|
6
3
|
import { AddressInfo } from 'node:net';
|
|
7
|
-
import { AppLoadContext } from '@remix-run/node';
|
|
8
4
|
import { Env, Hono, Context } from 'hono';
|
|
9
5
|
import { HonoOptions } from 'hono/hono-base';
|
|
10
|
-
import {
|
|
6
|
+
import { ServerBuild, AppLoadContext } from 'react-router';
|
|
7
|
+
import { ServerOptions, createServer } from 'node:http';
|
|
8
|
+
import { ServerOptions as ServerOptions$2, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
9
|
+
import { ServerOptions as ServerOptions$1, createServer as createServer$1 } from 'node:https';
|
|
11
10
|
|
|
12
11
|
type createHttpOptions = {
|
|
13
12
|
serverOptions?: ServerOptions;
|
|
@@ -26,6 +25,7 @@ type createSecureHttp2Options = {
|
|
|
26
25
|
createServer?: typeof createSecureServer;
|
|
27
26
|
};
|
|
28
27
|
type CreateNodeServerOptions = createHttpOptions | createHttpsOptions | createHttp2Options | createSecureHttp2Options;
|
|
28
|
+
|
|
29
29
|
type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
30
30
|
/**
|
|
31
31
|
* Enable the default logger
|
|
@@ -39,50 +39,30 @@ type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
39
39
|
* Defaults to `process.env.PORT || 3000`
|
|
40
40
|
*/
|
|
41
41
|
port?: number;
|
|
42
|
-
/**
|
|
43
|
-
* The directory where the server build files are located (defined in vite.config)
|
|
44
|
-
*
|
|
45
|
-
* Defaults to `build`
|
|
46
|
-
*
|
|
47
|
-
* See https://remix.run/docs/en/main/file-conventions/vite-config#builddirectory
|
|
48
|
-
*/
|
|
49
|
-
buildDirectory?: string;
|
|
50
|
-
/**
|
|
51
|
-
* The file name of the server build file (defined in vite.config)
|
|
52
|
-
*
|
|
53
|
-
* Defaults to `index.js`
|
|
54
|
-
*
|
|
55
|
-
* See https://remix.run/docs/en/main/file-conventions/vite-config#serverbuildfile
|
|
56
|
-
*/
|
|
57
|
-
serverBuildFile?: `${string}.js` | `${string}.mjs` | `${string}.cjs`;
|
|
58
|
-
/**
|
|
59
|
-
* The directory where the assets are located (defined in vite.config, build.assetsDir)
|
|
60
|
-
*
|
|
61
|
-
* Defaults to `assets`
|
|
62
|
-
*
|
|
63
|
-
* See https://vitejs.dev/config/build-options#build-assetsdir
|
|
64
|
-
*/
|
|
65
|
-
assetsDir?: string;
|
|
66
42
|
/**
|
|
67
43
|
* Customize the Hono server, for example, adding middleware
|
|
68
44
|
*
|
|
69
|
-
* It is applied after the default middleware and before the
|
|
45
|
+
* It is applied after the default middleware and before the React Router middleware
|
|
70
46
|
*/
|
|
71
47
|
configure?: <E extends Env = BlankEnv>(server: Hono<E>) => Promise<void> | void;
|
|
72
48
|
/**
|
|
73
|
-
* Augment the
|
|
49
|
+
* Augment the React Router AppLoadContext
|
|
74
50
|
*
|
|
75
51
|
* Don't forget to declare the AppLoadContext in your app, next to where you create the Hono server
|
|
76
52
|
*
|
|
77
53
|
* ```ts
|
|
78
|
-
* declare module "
|
|
54
|
+
* declare module "react-router" {
|
|
79
55
|
* interface AppLoadContext {
|
|
80
56
|
* // Add your custom context here
|
|
57
|
+
* whatever: string;
|
|
81
58
|
* }
|
|
82
59
|
* }
|
|
83
60
|
* ```
|
|
84
61
|
*/
|
|
85
|
-
getLoadContext?: (c: Context, options:
|
|
62
|
+
getLoadContext?: (c: Context, options: {
|
|
63
|
+
build: ServerBuild;
|
|
64
|
+
mode: "development" | "production" | "test";
|
|
65
|
+
}) => Promise<AppLoadContext> | AppLoadContext;
|
|
86
66
|
/**
|
|
87
67
|
* Listening listener (production mode only)
|
|
88
68
|
*
|
package/dist/node.mjs
CHANGED
|
@@ -1,78 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { serve as g } from "@hono/node-server";
|
|
5
|
-
import { serveStatic as v } from "@hono/node-server/serve-static";
|
|
6
|
-
import { Hono as y } from "hono";
|
|
7
|
-
import { logger as E } from "hono/logger";
|
|
8
|
-
import { remix as x } from "remix-hono/handler";
|
|
1
|
+
import {
|
|
2
|
+
cache
|
|
3
|
+
} from "./chunk-ENK5WJB4.mjs";
|
|
9
4
|
|
|
10
|
-
// src/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
async function a() {
|
|
18
|
-
return S?.ssrLoadModule("virtual:remix/server-build");
|
|
19
|
-
}
|
|
5
|
+
// src/node.ts
|
|
6
|
+
import { serve } from "@hono/node-server";
|
|
7
|
+
import { serveStatic } from "@hono/node-server/serve-static";
|
|
8
|
+
import { Hono } from "hono";
|
|
9
|
+
import { createMiddleware } from "hono/factory";
|
|
10
|
+
import { logger } from "hono/logger";
|
|
11
|
+
import { createRequestHandler } from "react-router";
|
|
20
12
|
|
|
21
|
-
// src/
|
|
22
|
-
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
13
|
+
// src/utils.ts
|
|
14
|
+
var envs = ["test", "development", "production"];
|
|
15
|
+
function getMode() {
|
|
16
|
+
const NODE_ENV = process.env.NODE_ENV?.toLowerCase();
|
|
17
|
+
if (!NODE_ENV || !envs.includes(NODE_ENV)) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`NODE_ENV is not set or is not valid: ${NODE_ENV}. Please set it to one of the following: ${envs.join(", ")}`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return NODE_ENV;
|
|
29
23
|
}
|
|
30
24
|
|
|
31
25
|
// src/node.ts
|
|
32
|
-
var
|
|
33
|
-
defaultLogger:
|
|
26
|
+
var defaultOptions = {
|
|
27
|
+
defaultLogger: true,
|
|
34
28
|
port: Number(process.env.PORT) || 3e3,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
listeningListener: (info) => {
|
|
30
|
+
console.log(`\u{1F680} Server started on port ${info.port}`);
|
|
31
|
+
console.log(`\u{1F30D} http://127.0.0.1:${info.port}`);
|
|
32
|
+
},
|
|
33
|
+
getLoadContext() {
|
|
34
|
+
return {};
|
|
40
35
|
}
|
|
41
36
|
};
|
|
42
|
-
async function
|
|
43
|
-
|
|
44
|
-
...
|
|
45
|
-
...
|
|
46
|
-
defaultLogger:
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
async function createHonoServer(options = {}) {
|
|
38
|
+
const mergedOptions = {
|
|
39
|
+
...defaultOptions,
|
|
40
|
+
...options,
|
|
41
|
+
defaultLogger: options.defaultLogger ?? true
|
|
42
|
+
};
|
|
43
|
+
const mode = getMode();
|
|
44
|
+
const PRODUCTION = mode === "production";
|
|
45
|
+
const server = new Hono(mergedOptions.honoOptions);
|
|
46
|
+
const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
|
|
47
|
+
server.use(
|
|
48
|
+
`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
|
|
49
|
+
cache(60 * 60 * 24 * 365),
|
|
51
50
|
// 1 year
|
|
52
|
-
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
51
|
+
serveStatic({ root: clientBuildPath })
|
|
52
|
+
);
|
|
53
|
+
server.use("*", cache(60 * 60), serveStatic({ root: PRODUCTION ? clientBuildPath : "./public" }));
|
|
54
|
+
if (mergedOptions.defaultLogger) {
|
|
55
|
+
server.use("*", logger());
|
|
56
|
+
}
|
|
57
|
+
if (mergedOptions.configure) {
|
|
58
|
+
await mergedOptions.configure(server);
|
|
59
|
+
}
|
|
60
|
+
server.use(async (c, next) => {
|
|
61
|
+
const build = await import(
|
|
62
|
+
// @ts-expect-error - Virtual module provided by React Router at build time
|
|
63
|
+
"virtual:react-router/server-build"
|
|
64
|
+
);
|
|
65
|
+
return createMiddleware(async (c2) => {
|
|
66
|
+
const requestHandler = createRequestHandler(build, mode);
|
|
67
|
+
const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
|
|
68
|
+
return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
|
|
69
|
+
})(c, next);
|
|
70
|
+
});
|
|
71
|
+
if (PRODUCTION) {
|
|
72
|
+
serve(
|
|
73
|
+
{
|
|
74
|
+
...server,
|
|
75
|
+
...mergedOptions.customNodeServer,
|
|
76
|
+
port: mergedOptions.port
|
|
77
|
+
},
|
|
78
|
+
mergedOptions.listeningListener
|
|
79
|
+
);
|
|
80
|
+
} else {
|
|
81
|
+
console.log("\u{1F6A7} Dev server started");
|
|
82
|
+
}
|
|
83
|
+
return server;
|
|
75
84
|
}
|
|
76
85
|
export {
|
|
77
|
-
|
|
86
|
+
createHonoServer
|
|
78
87
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-router-hono-server",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "This package includes helper function to create an Hono app in your entry.server.tsx file. It allows you to customize your server.",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./node": {
|
|
@@ -28,11 +28,10 @@
|
|
|
28
28
|
"scripts": {
|
|
29
29
|
"test": "vitest run",
|
|
30
30
|
"test:cov": "vitest run --coverage",
|
|
31
|
-
"build": "tsup
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"dev": "npm-run-all -s build:dev -p remix:dev build:dev:watch",
|
|
31
|
+
"build": "tsup",
|
|
32
|
+
"build:watch": "npm run build -- --watch",
|
|
33
|
+
"example:dev": "npm run dev -w react-router-example",
|
|
34
|
+
"dev": "npm-run-all -s build -p example:dev build:watch",
|
|
36
35
|
"prepublishOnly": "npm run build",
|
|
37
36
|
"check": "biome check .",
|
|
38
37
|
"check:fix": "biome check --fix .",
|
|
@@ -48,31 +47,33 @@
|
|
|
48
47
|
"bugs": {
|
|
49
48
|
"url": "https://github.com/rphlmr/react-router-hono-server/issues"
|
|
50
49
|
},
|
|
50
|
+
"bin": {
|
|
51
|
+
"react-router-hono-server": "dist/cli.mjs"
|
|
52
|
+
},
|
|
51
53
|
"files": ["dist"],
|
|
52
54
|
"workspaces": [".", "examples/*"],
|
|
53
55
|
"homepage": "https://github.com/rphlmr/react-router-hono-server#readme",
|
|
54
56
|
"dependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"@hono/
|
|
57
|
-
"hono": "^
|
|
58
|
-
"
|
|
57
|
+
"@drizzle-team/brocli": "^0.11.0",
|
|
58
|
+
"@hono/node-server": "^1.13.7",
|
|
59
|
+
"@hono/vite-dev-server": "^0.17.0",
|
|
60
|
+
"hono": "^4.6.11"
|
|
59
61
|
},
|
|
60
62
|
"devDependencies": {
|
|
61
|
-
"@biomejs/biome": "^1.
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"
|
|
65
|
-
"happy-dom": "^14.7.1",
|
|
66
|
-
"lefthook": "^1.7.2",
|
|
63
|
+
"@biomejs/biome": "^1.9.4",
|
|
64
|
+
"@types/node": "^22.9.3",
|
|
65
|
+
"@vitest/coverage-v8": "^2.1.5",
|
|
66
|
+
"lefthook": "^1.8.4",
|
|
67
67
|
"npm-run-all": "^4.1.5",
|
|
68
|
-
"tsup": "^8.
|
|
69
|
-
"typescript": "^5.
|
|
70
|
-
"vitest": "^1.5
|
|
68
|
+
"tsup": "^8.3.5",
|
|
69
|
+
"typescript": "^5.7.2",
|
|
70
|
+
"vitest": "^2.1.5"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
"react-router": "^7.0.1",
|
|
74
|
+
"vite": "^5.4.0"
|
|
75
|
+
},
|
|
76
|
+
"engines": {
|
|
77
|
+
"node": ">=20.0.0"
|
|
77
78
|
}
|
|
78
79
|
}
|