react-router-hono-server 2.18.0 → 2.19.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 +74 -43
- package/dist/adapters/bun.js +2 -1
- package/dist/adapters/node.js +4 -1
- package/dist/dev.js +11 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -125,7 +125,7 @@ export default defineConfig({
|
|
|
125
125
|
> Check this [example](./examples/bun/simple/) to see how to use it.
|
|
126
126
|
|
|
127
127
|
> [!IMPORTANT]
|
|
128
|
-
> React 19 and `bun --bun` flag
|
|
128
|
+
> React 19 and `bun --bun` flag should work with the same `entry.server.tsx` file as Node. Check [Using `bun --bun` flag?](#using-bun---bun-flag)
|
|
129
129
|
> Check this [example](./examples/bun/simple-bun-runtime/) to see how to use it.
|
|
130
130
|
|
|
131
131
|
```ts
|
|
@@ -148,45 +148,82 @@ export default defineConfig({
|
|
|
148
148
|
> [!TIP]
|
|
149
149
|
> Check this [example](./examples/bun/simple-bun-runtime/) to see how to use it.
|
|
150
150
|
|
|
151
|
-
If you are using the [`bun --bun` flag](https://bun.sh/docs/cli/run#bun), you need to have
|
|
151
|
+
If you are using the [`bun --bun` flag](https://bun.sh/docs/cli/run#bun), you need to have the same `entry.server.tsx` file as Node.
|
|
152
152
|
|
|
153
|
-
When building for production, at least for React 19, it uses a special `react-dom
|
|
153
|
+
When building for production, at least for React 19, it uses a special `react-dom/server.bun` import with only the `renderToReadableStream` function available.
|
|
154
|
+
|
|
155
|
+
React Router Hono Server vite plugin will alias that to `react-dom/server.node`
|
|
154
156
|
|
|
155
157
|
|
|
156
158
|
```ts
|
|
157
|
-
import {
|
|
158
|
-
|
|
159
|
+
import { PassThrough } from "node:stream";
|
|
160
|
+
|
|
159
161
|
import type { AppLoadContext, EntryContext } from "react-router";
|
|
162
|
+
import { createReadableStreamFromReadable } from "@react-router/node";
|
|
160
163
|
import { ServerRouter } from "react-router";
|
|
164
|
+
import { isbot } from "isbot";
|
|
165
|
+
import type { RenderToPipeableStreamOptions } from "react-dom/server";
|
|
166
|
+
import { renderToPipeableStream } from "react-dom/server";
|
|
161
167
|
|
|
162
|
-
export
|
|
168
|
+
export const streamTimeout = 5_000;
|
|
169
|
+
|
|
170
|
+
export default function handleRequest(
|
|
163
171
|
request: Request,
|
|
164
172
|
responseStatusCode: number,
|
|
165
173
|
responseHeaders: Headers,
|
|
166
174
|
routerContext: EntryContext,
|
|
167
|
-
|
|
175
|
+
loadContext: AppLoadContext,
|
|
176
|
+
// If you have middleware enabled:
|
|
177
|
+
// loadContext: unstable_RouterContextProvider
|
|
168
178
|
) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
180
|
+
let shellRendered = false;
|
|
181
|
+
let userAgent = request.headers.get("user-agent");
|
|
182
|
+
|
|
183
|
+
// Ensure requests from bots and SPA Mode renders wait for all content to load before responding
|
|
184
|
+
// https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation
|
|
185
|
+
let readyOption: keyof RenderToPipeableStreamOptions =
|
|
186
|
+
(userAgent && isbot(userAgent)) || routerContext.isSpaMode
|
|
187
|
+
? "onAllReady"
|
|
188
|
+
: "onShellReady";
|
|
189
|
+
|
|
190
|
+
const { pipe, abort } = renderToPipeableStream(
|
|
191
|
+
<ServerRouter context={routerContext} url={request.url} />,
|
|
192
|
+
{
|
|
193
|
+
[readyOption]() {
|
|
194
|
+
shellRendered = true;
|
|
195
|
+
const body = new PassThrough();
|
|
196
|
+
const stream = createReadableStreamFromReadable(body);
|
|
197
|
+
|
|
198
|
+
responseHeaders.set("Content-Type", "text/html");
|
|
199
|
+
|
|
200
|
+
resolve(
|
|
201
|
+
new Response(stream, {
|
|
202
|
+
headers: responseHeaders,
|
|
203
|
+
status: responseStatusCode,
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
pipe(body);
|
|
208
|
+
},
|
|
209
|
+
onShellError(error: unknown) {
|
|
210
|
+
reject(error);
|
|
211
|
+
},
|
|
212
|
+
onError(error: unknown) {
|
|
213
|
+
responseStatusCode = 500;
|
|
214
|
+
// Log streaming rendering errors from inside the shell. Don't log
|
|
215
|
+
// errors encountered during initial shell rendering since they'll
|
|
216
|
+
// reject and get logged in handleDocumentRequest.
|
|
217
|
+
if (shellRendered) {
|
|
218
|
+
console.error(error);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
);
|
|
185
223
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
status: responseStatusCode,
|
|
224
|
+
// Abort the rendering stream after the `streamTimeout` so it has time to
|
|
225
|
+
// flush down the rejected boundaries
|
|
226
|
+
setTimeout(abort, streamTimeout + 1000);
|
|
190
227
|
});
|
|
191
228
|
}
|
|
192
229
|
```
|
|
@@ -268,7 +305,7 @@ touch app/server.ts
|
|
|
268
305
|
// app/server.ts
|
|
269
306
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
270
307
|
|
|
271
|
-
export default
|
|
308
|
+
export default createHonoServer({/* options */});
|
|
272
309
|
```
|
|
273
310
|
|
|
274
311
|
#### Alternative
|
|
@@ -288,7 +325,7 @@ It is useful if you have many middleware and want to keep your server file clean
|
|
|
288
325
|
|
|
289
326
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
290
327
|
|
|
291
|
-
export default
|
|
328
|
+
export default createHonoServer({/* options */});
|
|
292
329
|
```
|
|
293
330
|
|
|
294
331
|
#### I don't like this default
|
|
@@ -489,12 +526,6 @@ export type HonoServerOptions<E extends Env = BlankEnv> = {
|
|
|
489
526
|
|
|
490
527
|
You can add additional Hono middleware with the `configure` function. If you do not provide a function, it will create a default Hono server.
|
|
491
528
|
|
|
492
|
-
The `configure` function can be async. So, make sure to `await createHonoServer()`.
|
|
493
|
-
|
|
494
|
-
> NB: If you import some shared code in your server file (or middleware), Vite will code split the file (your server code will be in a separate chunk, re-exported as default in `build/server/index.js`).
|
|
495
|
-
>
|
|
496
|
-
> In this situation, if you `await createHonoServer()` it will error with `Detected unsettled top-level await`. Just remove the `await` and it will work fine.
|
|
497
|
-
|
|
498
529
|
If you want to set up the React Router `AppLoadContext`, pass in a function to `getLoadContext`.
|
|
499
530
|
|
|
500
531
|
Modify the `AppLoadContext` interface used in your app.
|
|
@@ -520,7 +551,7 @@ declare module "react-router" {
|
|
|
520
551
|
}
|
|
521
552
|
}
|
|
522
553
|
|
|
523
|
-
export default
|
|
554
|
+
export default createHonoServer({
|
|
524
555
|
getLoadContext(_, { build, mode }) {
|
|
525
556
|
const isProductionMode = mode === "production";
|
|
526
557
|
return {
|
|
@@ -706,7 +737,7 @@ You can use it to add protection middleware, for example.
|
|
|
706
737
|
import { reactRouterRedirect } from "react-router-hono-server/http";
|
|
707
738
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
708
739
|
|
|
709
|
-
export default
|
|
740
|
+
export default createHonoServer({
|
|
710
741
|
beforeAll(app) {
|
|
711
742
|
app.use(async (c, next) => {
|
|
712
743
|
if (c.req.path.includes("/protected") && !c.req.header("Authorization")) {
|
|
@@ -728,7 +759,7 @@ import { createCookieSessionStorage } from "react-router";
|
|
|
728
759
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
729
760
|
import { session } from "remix-hono/session";
|
|
730
761
|
|
|
731
|
-
export default
|
|
762
|
+
export default createHonoServer({
|
|
732
763
|
configure: (server) => {
|
|
733
764
|
server.use(
|
|
734
765
|
session({
|
|
@@ -771,7 +802,7 @@ Then, use them with the `configure` function of `createHonoServer`.
|
|
|
771
802
|
import { createMiddleware } from "hono/factory";
|
|
772
803
|
import { createHonoServer } from "react-router-hono-server/node";
|
|
773
804
|
|
|
774
|
-
export default
|
|
805
|
+
export default createHonoServer({
|
|
775
806
|
configure: (server) => {
|
|
776
807
|
server.use(
|
|
777
808
|
createMiddleware(async (c, next) => {
|
|
@@ -809,7 +840,7 @@ import { createHonoServer } from "react-router-hono-server/node";
|
|
|
809
840
|
// Store connected clients
|
|
810
841
|
const clients = new Set<WSContext>();
|
|
811
842
|
|
|
812
|
-
export default
|
|
843
|
+
export default createHonoServer({
|
|
813
844
|
useWebSocket: true,
|
|
814
845
|
// 👆 Unlock this 👇 from @hono/node-ws
|
|
815
846
|
configure: (app, { upgradeWebSocket }) => {
|
|
@@ -855,7 +886,7 @@ import { createHonoServer } from "react-router-hono-server/bun";
|
|
|
855
886
|
// Store connected clients
|
|
856
887
|
const clients = new Set<WSContext>();
|
|
857
888
|
|
|
858
|
-
export default
|
|
889
|
+
export default createHonoServer({
|
|
859
890
|
useWebSocket: true,
|
|
860
891
|
// 👆 Unlock this 👇 from @hono/node-ws in dev, hono/bun in prod
|
|
861
892
|
configure(app, { upgradeWebSocket }) {
|
|
@@ -939,7 +970,7 @@ const app = new Hono();
|
|
|
939
970
|
// Mount the API app at /api
|
|
940
971
|
app.route(API_BASENAME, api);
|
|
941
972
|
|
|
942
|
-
export default
|
|
973
|
+
export default createHonoServer({
|
|
943
974
|
// Pass the root Hono app to the server.
|
|
944
975
|
// It will be used to mount the React Router app on the `basename` defined in react-router.config.ts
|
|
945
976
|
app,
|
|
@@ -1033,7 +1064,7 @@ We now use the Vite virtual import `virtual:react-router/server-build` to load t
|
|
|
1033
1064
|
> ```ts
|
|
1034
1065
|
> import { createHonoServer } from "react-router-hono-server/node";
|
|
1035
1066
|
>
|
|
1036
|
-
> export default
|
|
1067
|
+
> export default createHonoServer({/* other options */});
|
|
1037
1068
|
> ```
|
|
1038
1069
|
|
|
1039
1070
|
### Update your `vite.config.ts`
|
package/dist/adapters/bun.js
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
createGetLoadContext,
|
|
5
5
|
createWebSocket,
|
|
6
6
|
getBuildMode,
|
|
7
|
+
importBuild,
|
|
7
8
|
patchUpgradeListener
|
|
8
9
|
} from "../chunk-XPXQNPY3.js";
|
|
9
10
|
import {
|
|
@@ -11,13 +12,13 @@ import {
|
|
|
11
12
|
} from "../chunk-YI5FED4X.js";
|
|
12
13
|
|
|
13
14
|
// src/adapters/bun.ts
|
|
14
|
-
import * as build from "virtual:react-router/server-build";
|
|
15
15
|
import { Hono } from "hono";
|
|
16
16
|
import { serveStatic } from "hono/bun";
|
|
17
17
|
import { createMiddleware } from "hono/factory";
|
|
18
18
|
import { logger } from "hono/logger";
|
|
19
19
|
import { createRequestHandler } from "react-router";
|
|
20
20
|
async function createHonoServer(options) {
|
|
21
|
+
const build = await importBuild();
|
|
21
22
|
const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
|
|
22
23
|
const mergedOptions = {
|
|
23
24
|
...options,
|
package/dist/adapters/node.js
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
createGetLoadContext,
|
|
5
5
|
createWebSocket,
|
|
6
6
|
getBuildMode,
|
|
7
|
+
importBuild,
|
|
7
8
|
patchUpgradeListener
|
|
8
9
|
} from "../chunk-XPXQNPY3.js";
|
|
9
10
|
import {
|
|
@@ -11,7 +12,6 @@ import {
|
|
|
11
12
|
} from "../chunk-YI5FED4X.js";
|
|
12
13
|
|
|
13
14
|
// src/adapters/node.ts
|
|
14
|
-
import * as build from "virtual:react-router/server-build";
|
|
15
15
|
import { serve } from "@hono/node-server";
|
|
16
16
|
import { serveStatic } from "@hono/node-server/serve-static";
|
|
17
17
|
import { Hono } from "hono";
|
|
@@ -19,6 +19,8 @@ import { createMiddleware } from "hono/factory";
|
|
|
19
19
|
import { logger } from "hono/logger";
|
|
20
20
|
import { createRequestHandler } from "react-router";
|
|
21
21
|
async function createHonoServer(options) {
|
|
22
|
+
const startTime = Date.now();
|
|
23
|
+
const build = await importBuild();
|
|
22
24
|
const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
|
|
23
25
|
const mergedOptions = {
|
|
24
26
|
...options,
|
|
@@ -28,6 +30,7 @@ async function createHonoServer(options) {
|
|
|
28
30
|
if (basename !== "/") {
|
|
29
31
|
console.log(`\u{1F517} http://127.0.0.1:${info.port}${basename}`);
|
|
30
32
|
}
|
|
33
|
+
console.log(`\u{1F3CE}\uFE0F Server started in ${Date.now() - startTime}ms`);
|
|
31
34
|
}),
|
|
32
35
|
port: options?.port || Number(process.env.PORT) || 3e3,
|
|
33
36
|
defaultLogger: options?.defaultLogger ?? true,
|
package/dist/dev.js
CHANGED
|
@@ -98,21 +98,25 @@ Please add it to your plugins: import { cloudflareDevProxy } from "@react-router
|
|
|
98
98
|
chunk.facadeModuleId = REACT_ROUTER_VIRTUAL_MODULE_ID;
|
|
99
99
|
return "index.js";
|
|
100
100
|
},
|
|
101
|
-
// not invoked when target is webworker (single file output)
|
|
101
|
+
// not invoked when target is webworker (single file output)
|
|
102
102
|
chunkFileNames: (chunk) => {
|
|
103
103
|
if (chunk.name === "server-build") {
|
|
104
104
|
return reactRouterBuildFile;
|
|
105
105
|
}
|
|
106
106
|
return "assets/[name]-[hash].js";
|
|
107
107
|
},
|
|
108
|
+
manualChunks: runtime !== "cloudflare" ? (id) => {
|
|
109
|
+
if (id.includes(REACT_ROUTER_VIRTUAL_MODULE_ID)) {
|
|
110
|
+
return "server-build";
|
|
111
|
+
}
|
|
112
|
+
if (id.includes(pluginConfig.serverEntryPoint)) {
|
|
113
|
+
return "server";
|
|
114
|
+
}
|
|
115
|
+
return "chunk";
|
|
116
|
+
} : void 0,
|
|
108
117
|
// We are doing that because we build a single file that only exports the Hono server
|
|
109
118
|
// RR needs its exports for prerendering
|
|
110
|
-
footer:
|
|
111
|
-
if (!chunk.isEntry) {
|
|
112
|
-
return "";
|
|
113
|
-
}
|
|
114
|
-
return REACT_ROUTER_EXPORT;
|
|
115
|
-
}
|
|
119
|
+
footer: runtime === "cloudflare" ? REACT_ROUTER_EXPORT : void 0
|
|
116
120
|
}
|
|
117
121
|
}
|
|
118
122
|
}
|
package/package.json
CHANGED