react-router-hono-server 2.5.0 → 2.6.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 +87 -20
- package/dist/adapters/bun.d.ts +3 -3
- package/dist/adapters/bun.js +21 -8
- package/dist/adapters/cloudflare.d.ts +4 -4
- package/dist/adapters/cloudflare.js +69 -12
- package/dist/adapters/node.d.ts +3 -3
- package/dist/adapters/node.js +24 -8
- package/dist/{chunk-D4WESSFB.js → chunk-DZZCDCBI.js} +12 -1
- package/dist/dev.js +8 -5
- package/dist/{hono-server-options-base.d-CPG630Yc.d.ts → helpers-F2RS9Syt.d.ts} +16 -8
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -254,6 +254,22 @@ main = "./build/server/index.js"
|
|
|
254
254
|
assets = { directory = "./build/client/" }
|
|
255
255
|
```
|
|
256
256
|
|
|
257
|
+
##### Custom assets serving
|
|
258
|
+
You can set Cloudflare `experimental_serve_directly` and delegate assets serving to Hono, like for Node and Bun.
|
|
259
|
+
|
|
260
|
+
> [!TIP]
|
|
261
|
+
> Check https://developers.cloudflare.com/workers/static-assets/binding/#experimental_serve_directly
|
|
262
|
+
|
|
263
|
+
> [!TIP]
|
|
264
|
+
> Check this [example](./examples/cloudflare/simple/) to see how to use it.
|
|
265
|
+
|
|
266
|
+
```toml
|
|
267
|
+
[assets]
|
|
268
|
+
directory = "./build/client/"
|
|
269
|
+
binding = "ASSETS"
|
|
270
|
+
experimental_serve_directly = false
|
|
271
|
+
```
|
|
272
|
+
|
|
257
273
|
## How it works
|
|
258
274
|
|
|
259
275
|
This helper works differently depending on the environment.
|
|
@@ -313,8 +329,11 @@ type ReactRouterHonoServerPluginOptions = {
|
|
|
313
329
|
##### All adapters
|
|
314
330
|
```ts
|
|
315
331
|
export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
316
|
-
|
|
317
|
-
* Hono app to use
|
|
332
|
+
/**
|
|
333
|
+
* The base Hono app to use
|
|
334
|
+
*
|
|
335
|
+
* It will be used to mount the React Router server on the `basename` path
|
|
336
|
+
* defined in the [React Router config](https://api.reactrouter.com/v7/types/_react_router_dev.config.Config.html)
|
|
318
337
|
*
|
|
319
338
|
* {@link Hono}
|
|
320
339
|
*/
|
|
@@ -359,13 +378,11 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
359
378
|
}
|
|
360
379
|
) => Promise<AppLoadContext> | AppLoadContext;
|
|
361
380
|
/**
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
* It is called when the server is listening
|
|
381
|
+
* Hook to add middleware that runs before any built-in middleware, including assets serving.
|
|
365
382
|
*
|
|
366
|
-
*
|
|
383
|
+
* You can use it to add protection middleware, for example.
|
|
367
384
|
*/
|
|
368
|
-
|
|
385
|
+
beforeAll?: (app: Hono<E>) => Promise<void> | void;
|
|
369
386
|
};
|
|
370
387
|
```
|
|
371
388
|
|
|
@@ -417,6 +434,19 @@ export async function loader({ context }: Route.LoaderArgs) {
|
|
|
417
434
|
}
|
|
418
435
|
```
|
|
419
436
|
|
|
437
|
+
> [!TIP]
|
|
438
|
+
> If you declare your `getLoadContext` function in a separate file, you can use the helper `createGetLoadContext` from `react-router-hono-server/{adapter}`
|
|
439
|
+
> ```ts
|
|
440
|
+
> import { createGetLoadContext } from "react-router-hono-server/node";
|
|
441
|
+
>
|
|
442
|
+
> export const getLoadContext = createGetLoadContext((c, { mode, build }) => {
|
|
443
|
+
> const isProductionMode = mode === "production";
|
|
444
|
+
> return {
|
|
445
|
+
> appVersion: isProductionMode ? build.assets.version : "dev",
|
|
446
|
+
> };
|
|
447
|
+
> });
|
|
448
|
+
> ```
|
|
449
|
+
|
|
420
450
|
##### Node
|
|
421
451
|
```ts
|
|
422
452
|
export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
|
|
@@ -454,12 +484,6 @@ export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerO
|
|
|
454
484
|
* @default false
|
|
455
485
|
*/
|
|
456
486
|
overrideGlobalObjects?: boolean;
|
|
457
|
-
/**
|
|
458
|
-
* Hook to add middleware that runs before any built-in middleware, including assets serving.
|
|
459
|
-
*
|
|
460
|
-
* You can use it to add protection middleware, for example.
|
|
461
|
-
*/
|
|
462
|
-
beforeAll?: (app: Hono<E>) => Promise<void> | void;
|
|
463
487
|
}
|
|
464
488
|
```
|
|
465
489
|
|
|
@@ -472,18 +496,12 @@ export interface HonoServerOptions<E extends Env = BlankEnv> extends HonoServerO
|
|
|
472
496
|
* {@link https://bun.sh/docs/api/http#start-a-server-bun-serve}
|
|
473
497
|
*/
|
|
474
498
|
customBunServer?: Serve & ServeOptions;
|
|
475
|
-
/**
|
|
476
|
-
* Hook to add middleware that runs before any built-in middleware, including assets serving.
|
|
477
|
-
*
|
|
478
|
-
* You can use it to add protection middleware, for example.
|
|
479
|
-
*/
|
|
480
|
-
beforeAll?: (app: Hono<E>) => Promise<void> | void;
|
|
481
499
|
}
|
|
482
500
|
```
|
|
483
501
|
|
|
484
502
|
##### Cloudflare Workers
|
|
485
503
|
```ts
|
|
486
|
-
export interface HonoServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"
|
|
504
|
+
export interface HonoServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {}
|
|
487
505
|
```
|
|
488
506
|
|
|
489
507
|
## Middleware
|
|
@@ -706,6 +724,55 @@ Cloudflare requires a different approach to WebSockets, based on Durable Objects
|
|
|
706
724
|
>
|
|
707
725
|
> Work in progress on Cloudflare team: https://github.com/flarelabs-net/vite-plugin-cloudflare
|
|
708
726
|
|
|
727
|
+
## Basename and Hono sub apps
|
|
728
|
+
|
|
729
|
+
> [!NOTE]
|
|
730
|
+
> By default, the React Router app is mounted at `/` (default `basename` value).
|
|
731
|
+
>
|
|
732
|
+
> You may not need to use this option. It's for advanced use cases.
|
|
733
|
+
|
|
734
|
+
> [!TIP]
|
|
735
|
+
> Check this [example](./examples/node/custom-mount/) to see how to use it.
|
|
736
|
+
|
|
737
|
+
You can use the `basename` option in your React Router config (`react-router.config.ts`) to mount your React Router app on a subpath.
|
|
738
|
+
|
|
739
|
+
It will automatically mount the app on the subpath.
|
|
740
|
+
|
|
741
|
+
```ts
|
|
742
|
+
// react-router.config.ts
|
|
743
|
+
import type { Config } from "@react-router/dev/config";
|
|
744
|
+
|
|
745
|
+
export default {
|
|
746
|
+
basename: "/app", // Now the React Router app will be mounted on /app
|
|
747
|
+
} satisfies Config;
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
Then, you can use the `app` option in `createHonoServer` to pass your "root" Hono app. This will be used to mount the React Router app on the `basename` path.
|
|
751
|
+
|
|
752
|
+
```ts
|
|
753
|
+
import { Hono } from "hono";
|
|
754
|
+
import { createHonoServer } from "react-router-hono-server/node";
|
|
755
|
+
import { API_BASENAME, api } from "./api";
|
|
756
|
+
import { getLoadContext } from "./context";
|
|
757
|
+
|
|
758
|
+
// Create a root Hono app
|
|
759
|
+
const app = new Hono();
|
|
760
|
+
|
|
761
|
+
// Mount the API app at /api
|
|
762
|
+
app.route(API_BASENAME, api);
|
|
763
|
+
|
|
764
|
+
export default await createHonoServer({
|
|
765
|
+
// Pass the root Hono app to the server.
|
|
766
|
+
// It will be used to mount the React Router app on the `basename` defined in react-router.config.ts
|
|
767
|
+
app,
|
|
768
|
+
getLoadContext,
|
|
769
|
+
});
|
|
770
|
+
```
|
|
771
|
+
> [!NOTE]
|
|
772
|
+
> You now have two entry points!
|
|
773
|
+
> - `/api` - for your API
|
|
774
|
+
> - `/app` - for your React Router app
|
|
775
|
+
|
|
709
776
|
## Pre-rendering
|
|
710
777
|
You should be able to use pre-rendering with this package.
|
|
711
778
|
|
package/dist/adapters/bun.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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 '../
|
|
5
|
-
|
|
6
|
-
import 'hono/ws';
|
|
4
|
+
import { W as WithWebsocket, a as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-F2RS9Syt.js';
|
|
5
|
+
export { c as createGetLoadContext } from '../helpers-F2RS9Syt.js';
|
|
7
6
|
import 'react-router';
|
|
7
|
+
import 'hono/ws';
|
|
8
8
|
|
|
9
9
|
type CustomBunServer = Serve & ServeOptions & {
|
|
10
10
|
websocket?: unknown;
|
package/dist/adapters/bun.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
bindIncomingRequestSocketInfo,
|
|
3
3
|
cleanUpgradeListeners,
|
|
4
|
+
createGetLoadContext,
|
|
4
5
|
createWebSocket,
|
|
6
|
+
importBuild,
|
|
5
7
|
patchUpgradeListener
|
|
6
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-DZZCDCBI.js";
|
|
7
9
|
import {
|
|
8
10
|
cache
|
|
9
11
|
} from "../chunk-V3H4JZ4H.js";
|
|
@@ -15,6 +17,7 @@ import { createMiddleware } from "hono/factory";
|
|
|
15
17
|
import { logger } from "hono/logger";
|
|
16
18
|
import { createRequestHandler } from "react-router";
|
|
17
19
|
async function createHonoServer(options) {
|
|
20
|
+
const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
|
|
18
21
|
const mergedOptions = {
|
|
19
22
|
...options,
|
|
20
23
|
port: options?.port || Number(options?.customBunServer?.port) || Number(process.env.PORT) || 3e3,
|
|
@@ -22,8 +25,8 @@ async function createHonoServer(options) {
|
|
|
22
25
|
};
|
|
23
26
|
const mode = import.meta.env.MODE || "production";
|
|
24
27
|
const PRODUCTION = mode === "production";
|
|
25
|
-
const app = new Hono(mergedOptions.honoOptions || mergedOptions.app);
|
|
26
28
|
const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
|
|
29
|
+
const app = new Hono(mergedOptions.app);
|
|
27
30
|
const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
|
|
28
31
|
app,
|
|
29
32
|
enabled: mergedOptions.useWebSocket ?? false
|
|
@@ -38,7 +41,12 @@ async function createHonoServer(options) {
|
|
|
38
41
|
// 1 year
|
|
39
42
|
serveStatic({ root: clientBuildPath })
|
|
40
43
|
);
|
|
41
|
-
app.use(
|
|
44
|
+
app.use(
|
|
45
|
+
"*",
|
|
46
|
+
cache(60 * 60),
|
|
47
|
+
// 1 hour
|
|
48
|
+
serveStatic({ root: PRODUCTION ? clientBuildPath : "./public" })
|
|
49
|
+
);
|
|
42
50
|
if (mergedOptions.defaultLogger) {
|
|
43
51
|
app.use("*", logger());
|
|
44
52
|
}
|
|
@@ -47,17 +55,21 @@ async function createHonoServer(options) {
|
|
|
47
55
|
} else {
|
|
48
56
|
await mergedOptions.configure?.(app);
|
|
49
57
|
}
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
)
|
|
54
|
-
|
|
58
|
+
const reactRouterApp = new Hono({
|
|
59
|
+
strict: false
|
|
60
|
+
});
|
|
61
|
+
reactRouterApp.use(async (c, next) => {
|
|
62
|
+
const build = await importBuild();
|
|
55
63
|
return createMiddleware(async (c2) => {
|
|
56
64
|
const requestHandler = createRequestHandler(build, mode);
|
|
57
65
|
const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
|
|
58
66
|
return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
|
|
59
67
|
})(c, next);
|
|
60
68
|
});
|
|
69
|
+
app.route(`${basename}`, reactRouterApp);
|
|
70
|
+
if (basename) {
|
|
71
|
+
app.route(`${basename}.data`, reactRouterApp);
|
|
72
|
+
}
|
|
61
73
|
let server = {
|
|
62
74
|
...mergedOptions.customBunServer,
|
|
63
75
|
fetch: app.fetch,
|
|
@@ -76,5 +88,6 @@ async function createHonoServer(options) {
|
|
|
76
88
|
return server;
|
|
77
89
|
}
|
|
78
90
|
export {
|
|
91
|
+
createGetLoadContext,
|
|
79
92
|
createHonoServer
|
|
80
93
|
};
|
|
@@ -1,12 +1,12 @@
|
|
|
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 '../
|
|
5
|
-
|
|
6
|
-
import 'hono/ws';
|
|
4
|
+
import { a as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-F2RS9Syt.js';
|
|
5
|
+
export { c as createGetLoadContext } from '../helpers-F2RS9Syt.js';
|
|
7
6
|
import 'react-router';
|
|
7
|
+
import 'hono/ws';
|
|
8
8
|
|
|
9
|
-
interface HonoCloudflareOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"
|
|
9
|
+
interface HonoCloudflareOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {
|
|
10
10
|
}
|
|
11
11
|
type HonoServerOptions<E extends Env = BlankEnv> = HonoCloudflareOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
|
|
12
12
|
/**
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
bindIncomingRequestSocketInfo
|
|
3
|
-
|
|
2
|
+
bindIncomingRequestSocketInfo,
|
|
3
|
+
createGetLoadContext,
|
|
4
|
+
importBuild
|
|
5
|
+
} from "../chunk-DZZCDCBI.js";
|
|
4
6
|
import {
|
|
5
7
|
cache
|
|
6
8
|
} from "../chunk-V3H4JZ4H.js";
|
|
@@ -11,38 +13,93 @@ import { createMiddleware } from "hono/factory";
|
|
|
11
13
|
import { logger } from "hono/logger";
|
|
12
14
|
import { createRequestHandler } from "react-router";
|
|
13
15
|
async function createHonoServer(options) {
|
|
16
|
+
const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
|
|
14
17
|
const mergedOptions = {
|
|
15
18
|
...options,
|
|
16
19
|
defaultLogger: options?.defaultLogger ?? true
|
|
17
20
|
};
|
|
18
21
|
const mode = import.meta.env.MODE || "production";
|
|
19
|
-
const
|
|
20
|
-
const app = new Hono(mergedOptions.
|
|
21
|
-
|
|
22
|
+
const PRODUCTION = mode === "production";
|
|
23
|
+
const app = new Hono(mergedOptions.app);
|
|
24
|
+
await mergedOptions.beforeAll?.(app);
|
|
25
|
+
app.use(
|
|
26
|
+
// https://developers.cloudflare.com/workers/static-assets/binding/#experimental_serve_directly
|
|
27
|
+
`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
|
|
28
|
+
cache(60 * 60 * 24 * 365),
|
|
29
|
+
// 1 year
|
|
30
|
+
serveCloudflareAssets()
|
|
31
|
+
);
|
|
32
|
+
if (PRODUCTION) {
|
|
33
|
+
app.use(
|
|
34
|
+
// https://developers.cloudflare.com/workers/static-assets/binding/#experimental_serve_directly
|
|
35
|
+
"*",
|
|
36
|
+
cache(60 * 60),
|
|
37
|
+
// 1 hour
|
|
38
|
+
serveCloudflareAssets()
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
22
41
|
const { serveStatic } = await import("@hono/node-server/serve-static");
|
|
23
|
-
app.use(
|
|
42
|
+
app.use(
|
|
43
|
+
"*",
|
|
44
|
+
cache(60 * 60),
|
|
45
|
+
// 1 hour
|
|
46
|
+
serveStatic({ root: "./public" })
|
|
47
|
+
);
|
|
24
48
|
app.use(bindIncomingRequestSocketInfo());
|
|
25
49
|
}
|
|
26
50
|
if (mergedOptions.defaultLogger) {
|
|
27
51
|
app.use("*", logger());
|
|
28
52
|
}
|
|
29
53
|
await mergedOptions.configure?.(app);
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
)
|
|
34
|
-
|
|
54
|
+
const reactRouterApp = new Hono({
|
|
55
|
+
strict: false
|
|
56
|
+
});
|
|
57
|
+
reactRouterApp.use(async (c, next) => {
|
|
58
|
+
const build = await importBuild();
|
|
35
59
|
return createMiddleware(async (c2) => {
|
|
36
60
|
const requestHandler = createRequestHandler(build, mode);
|
|
37
61
|
const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
|
|
38
62
|
return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
|
|
39
63
|
})(c, next);
|
|
40
64
|
});
|
|
41
|
-
|
|
65
|
+
app.route(`${basename}`, reactRouterApp);
|
|
66
|
+
if (basename) {
|
|
67
|
+
app.route(`${basename}.data`, reactRouterApp);
|
|
68
|
+
}
|
|
69
|
+
if (!PRODUCTION) {
|
|
42
70
|
console.log("\u{1F6A7} Running in development mode");
|
|
43
71
|
}
|
|
44
72
|
return app;
|
|
45
73
|
}
|
|
74
|
+
var warned = false;
|
|
75
|
+
function serveCloudflareAssets() {
|
|
76
|
+
return createMiddleware(async (c, next) => {
|
|
77
|
+
const binding = c.env?.ASSETS;
|
|
78
|
+
if (!binding) {
|
|
79
|
+
if (!warned) {
|
|
80
|
+
console.info(
|
|
81
|
+
"\x1B[33m\nThe binding ASSETS is not set. Falling back to Cloudflare serving.\nhttps://developers.cloudflare.com/workers/static-assets/binding/#binding\n\x1B[0m"
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
warned = true;
|
|
85
|
+
return next();
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
let response = await binding.fetch(
|
|
89
|
+
c.req.url,
|
|
90
|
+
c.req.raw.clone()
|
|
91
|
+
);
|
|
92
|
+
if (response.status >= 400) {
|
|
93
|
+
return next();
|
|
94
|
+
}
|
|
95
|
+
response = new Response(response.body, response);
|
|
96
|
+
return response;
|
|
97
|
+
} catch {
|
|
98
|
+
return next();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
46
102
|
export {
|
|
103
|
+
createGetLoadContext,
|
|
47
104
|
createHonoServer
|
|
48
105
|
};
|
package/dist/adapters/node.d.ts
CHANGED
|
@@ -2,13 +2,13 @@ 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 '../
|
|
5
|
+
import { W as WithWebsocket, a as WithoutWebsocket, H as HonoServerOptionsBase } from '../helpers-F2RS9Syt.js';
|
|
6
|
+
export { c as createGetLoadContext } from '../helpers-F2RS9Syt.js';
|
|
6
7
|
import { ServerOptions, createServer } from 'node:http';
|
|
7
8
|
import { ServerOptions as ServerOptions$2, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
8
9
|
import { ServerOptions as ServerOptions$1, createServer as createServer$1 } from 'node:https';
|
|
9
|
-
import 'hono/hono-base';
|
|
10
|
-
import 'hono/ws';
|
|
11
10
|
import 'react-router';
|
|
11
|
+
import 'hono/ws';
|
|
12
12
|
|
|
13
13
|
type createHttpOptions = {
|
|
14
14
|
serverOptions?: ServerOptions;
|
package/dist/adapters/node.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
bindIncomingRequestSocketInfo,
|
|
3
3
|
cleanUpgradeListeners,
|
|
4
|
+
createGetLoadContext,
|
|
4
5
|
createWebSocket,
|
|
6
|
+
importBuild,
|
|
5
7
|
patchUpgradeListener
|
|
6
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-DZZCDCBI.js";
|
|
7
9
|
import {
|
|
8
10
|
cache
|
|
9
11
|
} from "../chunk-V3H4JZ4H.js";
|
|
@@ -16,11 +18,15 @@ import { createMiddleware } from "hono/factory";
|
|
|
16
18
|
import { logger } from "hono/logger";
|
|
17
19
|
import { createRequestHandler } from "react-router";
|
|
18
20
|
async function createHonoServer(options) {
|
|
21
|
+
const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
|
|
19
22
|
const mergedOptions = {
|
|
20
23
|
...options,
|
|
21
24
|
listeningListener: options?.listeningListener || ((info) => {
|
|
22
25
|
console.log(`\u{1F680} Server started on port ${info.port}`);
|
|
23
26
|
console.log(`\u{1F30D} http://127.0.0.1:${info.port}`);
|
|
27
|
+
if (basename !== "/") {
|
|
28
|
+
console.log(`\u{1F517} http://127.0.0.1:${info.port}${basename}`);
|
|
29
|
+
}
|
|
24
30
|
}),
|
|
25
31
|
port: options?.port || Number(process.env.PORT) || 3e3,
|
|
26
32
|
defaultLogger: options?.defaultLogger ?? true,
|
|
@@ -28,8 +34,8 @@ async function createHonoServer(options) {
|
|
|
28
34
|
};
|
|
29
35
|
const mode = import.meta.env.MODE || "production";
|
|
30
36
|
const PRODUCTION = mode === "production";
|
|
31
|
-
const app = new Hono(mergedOptions.honoOptions || mergedOptions.app);
|
|
32
37
|
const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
|
|
38
|
+
const app = new Hono(mergedOptions.app);
|
|
33
39
|
const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
|
|
34
40
|
app,
|
|
35
41
|
enabled: mergedOptions.useWebSocket ?? false
|
|
@@ -44,7 +50,12 @@ async function createHonoServer(options) {
|
|
|
44
50
|
// 1 year
|
|
45
51
|
serveStatic({ root: clientBuildPath })
|
|
46
52
|
);
|
|
47
|
-
app.use(
|
|
53
|
+
app.use(
|
|
54
|
+
"*",
|
|
55
|
+
cache(60 * 60),
|
|
56
|
+
// 1 hour
|
|
57
|
+
serveStatic({ root: PRODUCTION ? clientBuildPath : "./public" })
|
|
58
|
+
);
|
|
48
59
|
if (mergedOptions.defaultLogger) {
|
|
49
60
|
app.use("*", logger());
|
|
50
61
|
}
|
|
@@ -53,17 +64,21 @@ async function createHonoServer(options) {
|
|
|
53
64
|
} else {
|
|
54
65
|
await mergedOptions.configure?.(app);
|
|
55
66
|
}
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
)
|
|
60
|
-
|
|
67
|
+
const reactRouterApp = new Hono({
|
|
68
|
+
strict: false
|
|
69
|
+
});
|
|
70
|
+
reactRouterApp.use(async (c, next) => {
|
|
71
|
+
const build = await importBuild();
|
|
61
72
|
return createMiddleware(async (c2) => {
|
|
62
73
|
const requestHandler = createRequestHandler(build, mode);
|
|
63
74
|
const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
|
|
64
75
|
return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
|
|
65
76
|
})(c, next);
|
|
66
77
|
});
|
|
78
|
+
app.route(`${basename}`, reactRouterApp);
|
|
79
|
+
if (basename) {
|
|
80
|
+
app.route(`${basename}.data`, reactRouterApp);
|
|
81
|
+
}
|
|
67
82
|
if (PRODUCTION) {
|
|
68
83
|
const server = serve(
|
|
69
84
|
{
|
|
@@ -87,5 +102,6 @@ async function createHonoServer(options) {
|
|
|
87
102
|
return app;
|
|
88
103
|
}
|
|
89
104
|
export {
|
|
105
|
+
createGetLoadContext,
|
|
90
106
|
createHonoServer
|
|
91
107
|
};
|
|
@@ -78,10 +78,21 @@ function bindIncomingRequestSocketInfo() {
|
|
|
78
78
|
return next();
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
|
+
async function importBuild() {
|
|
82
|
+
return import(
|
|
83
|
+
// @ts-expect-error - Virtual module provided by React Router at build time
|
|
84
|
+
"virtual:react-router/server-build"
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function createGetLoadContext(getLoadContext) {
|
|
88
|
+
return getLoadContext;
|
|
89
|
+
}
|
|
81
90
|
|
|
82
91
|
export {
|
|
83
92
|
createWebSocket,
|
|
84
93
|
cleanUpgradeListeners,
|
|
85
94
|
patchUpgradeListener,
|
|
86
|
-
bindIncomingRequestSocketInfo
|
|
95
|
+
bindIncomingRequestSocketInfo,
|
|
96
|
+
importBuild,
|
|
97
|
+
createGetLoadContext
|
|
87
98
|
};
|
package/dist/dev.js
CHANGED
|
@@ -45,7 +45,8 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
|
|
|
45
45
|
define: {
|
|
46
46
|
"import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY": JSON.stringify(pluginConfig.buildDirectory),
|
|
47
47
|
"import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR": JSON.stringify(pluginConfig.assetsDir),
|
|
48
|
-
"import.meta.env.REACT_ROUTER_HONO_SERVER_RUNTIME": JSON.stringify(runtime)
|
|
48
|
+
"import.meta.env.REACT_ROUTER_HONO_SERVER_RUNTIME": JSON.stringify(runtime),
|
|
49
|
+
"import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME": JSON.stringify(pluginConfig.basename)
|
|
49
50
|
},
|
|
50
51
|
ssr: {
|
|
51
52
|
target: runtime === "cloudflare" ? "webworker" : void 0,
|
|
@@ -103,11 +104,11 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
|
|
|
103
104
|
entry: pluginConfig.serverEntryPoint,
|
|
104
105
|
export: "default",
|
|
105
106
|
exclude: [
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
/.*\.(ts|js|tsx|jsx|css)(\?.*)?$/,
|
|
108
|
+
/\?import(\?.*)?$/,
|
|
108
109
|
/^\/@.+$/,
|
|
109
110
|
/^\/node_modules\/.*/,
|
|
110
|
-
|
|
111
|
+
`/${pluginConfig.appDirectory}/**/.*/**`,
|
|
111
112
|
...pluginConfig.dev?.exclude || []
|
|
112
113
|
]
|
|
113
114
|
});
|
|
@@ -138,6 +139,7 @@ function resolvePluginConfig(config, options) {
|
|
|
138
139
|
const assetsDir = config.build?.assetsDir || "assets";
|
|
139
140
|
const serverEntryPoint = options.serverEntryPoint || findDefaultServerEntry(appDirectory);
|
|
140
141
|
const serverBuildFile = reactRouterConfig.reactRouterConfig.serverBuildFile;
|
|
142
|
+
const basename = reactRouterConfig.reactRouterConfig.basename;
|
|
141
143
|
return {
|
|
142
144
|
rootDirectory,
|
|
143
145
|
buildDirectory,
|
|
@@ -146,7 +148,8 @@ function resolvePluginConfig(config, options) {
|
|
|
146
148
|
assetsDir,
|
|
147
149
|
serverEntryPoint,
|
|
148
150
|
dev: options.dev,
|
|
149
|
-
serverBuildFile
|
|
151
|
+
serverBuildFile,
|
|
152
|
+
basename
|
|
150
153
|
};
|
|
151
154
|
}
|
|
152
155
|
var warned = false;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
import * as react_router from 'react-router';
|
|
2
|
+
import { ServerBuild, AppLoadContext } from 'react-router';
|
|
3
|
+
import * as hono from 'hono';
|
|
1
4
|
import { Env, Hono, Context } from 'hono';
|
|
2
|
-
import { HonoOptions } from 'hono/hono-base';
|
|
3
5
|
import { UpgradeWebSocket } from 'hono/ws';
|
|
4
|
-
import { ServerBuild, AppLoadContext } from 'react-router';
|
|
5
6
|
|
|
6
7
|
interface HonoServerOptionsBase<E extends Env> {
|
|
7
8
|
/**
|
|
8
|
-
* Hono app to use
|
|
9
|
+
* The base Hono app to use as a replacement for the default one created automatically
|
|
10
|
+
*
|
|
11
|
+
* It will be used to mount the React Router server on the `basename` path
|
|
12
|
+
* defined in the [React Router config](https://api.reactrouter.com/v7/types/_react_router_dev.config.Config.html)
|
|
9
13
|
*
|
|
10
14
|
* {@link Hono}
|
|
11
15
|
*/
|
|
@@ -43,10 +47,6 @@ interface HonoServerOptionsBase<E extends Env> {
|
|
|
43
47
|
mode: string;
|
|
44
48
|
}
|
|
45
49
|
) => Promise<AppLoadContext> | AppLoadContext;
|
|
46
|
-
/**
|
|
47
|
-
* @deprecated Use `app` instead
|
|
48
|
-
*/
|
|
49
|
-
honoOptions?: HonoOptions<E>;
|
|
50
50
|
/**
|
|
51
51
|
* Hook to add middleware that runs before any built-in middleware, including assets serving.
|
|
52
52
|
*
|
|
@@ -89,4 +89,12 @@ interface WithoutWebsocket<E extends Env> {
|
|
|
89
89
|
configure?: (app: Hono<E>) => Promise<void> | void;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Helper to create a getLoadContext function fully typed
|
|
94
|
+
*/
|
|
95
|
+
declare function createGetLoadContext(getLoadContext: HonoServerOptionsBase<Env>["getLoadContext"]): ((c: hono.Context, options: {
|
|
96
|
+
build: ServerBuild;
|
|
97
|
+
mode: string;
|
|
98
|
+
}) => Promise<react_router.AppLoadContext> | react_router.AppLoadContext) | undefined;
|
|
99
|
+
|
|
100
|
+
export { type HonoServerOptionsBase as H, type WithWebsocket as W, type WithoutWebsocket 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.
|
|
3
|
+
"version": "2.6.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": {
|
|
@@ -96,10 +96,10 @@
|
|
|
96
96
|
"vitest": "^2.1.5"
|
|
97
97
|
},
|
|
98
98
|
"peerDependencies": {
|
|
99
|
-
"react-router": "^7.0
|
|
100
|
-
"vite": "^5.
|
|
99
|
+
"react-router": "^7.1.0",
|
|
100
|
+
"vite": "^5.1.0 || ^6.0.0"
|
|
101
101
|
},
|
|
102
102
|
"engines": {
|
|
103
|
-
"node": ">=
|
|
103
|
+
"node": ">=22.12.0"
|
|
104
104
|
}
|
|
105
105
|
}
|