react-router-hono-server 0.0.2 → 0.0.4
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 +32 -11
- package/dist/dev.d.mts +1 -1
- package/dist/dev.mjs +27 -1
- package/dist/node.d.mts +2 -2
- package/dist/node.mjs +79 -1
- package/package.json +12 -7
- package/dist/chunk-4CKI2DYM.mjs +0 -1
- package/dist/middlewares.d.mts +0 -10
- package/dist/middlewares.mjs +0 -1
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@ with the rest of your Remix app, you may import app modules as needed. It also
|
|
|
15
15
|
supports Vite HMR via the `react-router-hono-server/dev` plugin (which is required
|
|
16
16
|
for this to function).
|
|
17
17
|
|
|
18
|
+
It relies on [remix-hono](https://github.com/sergiodxa/remix-hono) and presets a default Hono server config that you can [customize](#options)
|
|
19
|
+
|
|
18
20
|
> [!IMPORTANT]
|
|
19
21
|
> Only works with Remix in **ESM mode**
|
|
20
22
|
>
|
|
@@ -23,7 +25,7 @@ for this to function).
|
|
|
23
25
|
> Only works for **node**
|
|
24
26
|
|
|
25
27
|
> [!TIP]
|
|
26
|
-
>
|
|
28
|
+
> 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)
|
|
27
29
|
|
|
28
30
|
## Installation
|
|
29
31
|
|
|
@@ -31,7 +33,7 @@ Install the following npm package. NOTE: This is not a dev dependency, as it
|
|
|
31
33
|
creates the Hono server used in production.
|
|
32
34
|
|
|
33
35
|
```bash
|
|
34
|
-
npm install
|
|
36
|
+
npm install react-router-hono-server
|
|
35
37
|
```
|
|
36
38
|
|
|
37
39
|
## Configuration
|
|
@@ -47,6 +49,25 @@ import { createHonoServer } from "react-router-hono-server/node";
|
|
|
47
49
|
export const server = await createHonoServer();
|
|
48
50
|
```
|
|
49
51
|
|
|
52
|
+
#### Alternative
|
|
53
|
+
You can create your server in a separate file and export it from your _entry.server.tsx_.
|
|
54
|
+
|
|
55
|
+
It is useful if you have many middleware and want to keep your _entry.server.tsx_ clean.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// app/server.ts
|
|
59
|
+
|
|
60
|
+
import { createHonoServer } from "react-router-hono-server/node";
|
|
61
|
+
|
|
62
|
+
export const server = await createHonoServer();
|
|
63
|
+
```
|
|
64
|
+
```ts
|
|
65
|
+
// app/entry.server.tsx
|
|
66
|
+
|
|
67
|
+
export * from "./server";
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
|
|
50
71
|
### Add the Vite plugin
|
|
51
72
|
```ts
|
|
52
73
|
// vite.config.ts
|
|
@@ -143,9 +164,9 @@ export type HonoServerOptions = {
|
|
|
143
164
|
*/
|
|
144
165
|
assetsDir?: string;
|
|
145
166
|
/**
|
|
146
|
-
* Customize the Hono server, for example, adding
|
|
167
|
+
* Customize the Hono server, for example, adding middleware
|
|
147
168
|
*
|
|
148
|
-
* It is applied after the default
|
|
169
|
+
* It is applied after the default middleware and before the remix middleware
|
|
149
170
|
*/
|
|
150
171
|
configure?: (server: Hono) => Promise<void> | void;
|
|
151
172
|
/**
|
|
@@ -224,24 +245,24 @@ export async function loader({ context }: LoaderFunctionArgs) {
|
|
|
224
245
|
}
|
|
225
246
|
```
|
|
226
247
|
|
|
227
|
-
##
|
|
248
|
+
## Middleware
|
|
228
249
|
|
|
229
|
-
|
|
250
|
+
Middleware are functions that are called before Remix calls your loader/action.
|
|
230
251
|
|
|
231
252
|
Hono is the perfect tool for this, as it supports middleware out of the box.
|
|
232
253
|
|
|
233
254
|
See the [Hono docs](https://hono.dev/docs/guides/middleware) for more information.
|
|
234
255
|
|
|
235
|
-
You can imagine many use cases for
|
|
256
|
+
You can imagine many use cases for middleware, such as authentication, protecting routes, caching, logging, etc.
|
|
236
257
|
|
|
237
258
|
See how [Shelf.nu](https://github.com/Shelf-nu/shelf.nu/blob/main/server/middleware.ts) uses them!
|
|
238
259
|
|
|
239
260
|
> [!TIP]
|
|
240
|
-
> This lib exports one middleware `cache` (`react-router-hono-server/
|
|
261
|
+
> This lib exports one middleware `cache` (`react-router-hono-server/middleware`) that you can use to cache your responses.
|
|
241
262
|
|
|
242
|
-
### Using Remix Hono
|
|
263
|
+
### Using Remix Hono middleware
|
|
243
264
|
|
|
244
|
-
It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono)
|
|
265
|
+
It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono) middleware with this package.
|
|
245
266
|
|
|
246
267
|
```ts
|
|
247
268
|
import { createCookieSessionStorage } from "@remix-run/node";
|
|
@@ -284,7 +305,7 @@ export const server = await createHonoServer({
|
|
|
284
305
|
|
|
285
306
|
### Creating custom Middleware
|
|
286
307
|
|
|
287
|
-
You can create
|
|
308
|
+
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`.
|
|
288
309
|
|
|
289
310
|
Then, use them with the `configure` function of `createHonoServer`.
|
|
290
311
|
|
package/dist/dev.d.mts
CHANGED
package/dist/dev.mjs
CHANGED
|
@@ -1 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
// src/dev.ts
|
|
2
|
+
import t from "@hono/vite-dev-server";
|
|
3
|
+
var o = {
|
|
4
|
+
entry: "app/entry.server.tsx",
|
|
5
|
+
exportName: "server",
|
|
6
|
+
appDirectory: "app",
|
|
7
|
+
exclude: []
|
|
8
|
+
};
|
|
9
|
+
function p(r) {
|
|
10
|
+
let e = { ...o, ...r };
|
|
11
|
+
return t({
|
|
12
|
+
injectClientScript: !1,
|
|
13
|
+
entry: e.entry,
|
|
14
|
+
// The file path of your server.
|
|
15
|
+
export: e.exportName,
|
|
16
|
+
exclude: [
|
|
17
|
+
`/${e.appDirectory}/**/*`,
|
|
18
|
+
`/${e.appDirectory}/**/.*/**`,
|
|
19
|
+
/^\/@.+$/,
|
|
20
|
+
/^\/node_modules\/.*/,
|
|
21
|
+
...e.exclude
|
|
22
|
+
]
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
p as devServer
|
|
27
|
+
};
|
package/dist/node.d.mts
CHANGED
|
@@ -41,9 +41,9 @@ type HonoServerOptions = {
|
|
|
41
41
|
*/
|
|
42
42
|
assetsDir?: string;
|
|
43
43
|
/**
|
|
44
|
-
* Customize the Hono server, for example, adding
|
|
44
|
+
* Customize the Hono server, for example, adding middleware
|
|
45
45
|
*
|
|
46
|
-
* It is applied after the default
|
|
46
|
+
* It is applied after the default middleware and before the remix middleware
|
|
47
47
|
*/
|
|
48
48
|
configure?: (server: Hono) => Promise<void> | void;
|
|
49
49
|
/**
|
package/dist/node.mjs
CHANGED
|
@@ -1 +1,79 @@
|
|
|
1
|
-
|
|
1
|
+
// src/node.ts
|
|
2
|
+
import d from "node:path";
|
|
3
|
+
import f from "node:url";
|
|
4
|
+
import { serve as g } from "@hono/node-server";
|
|
5
|
+
import { serveStatic as a } from "@hono/node-server/serve-static";
|
|
6
|
+
import { Hono as x } from "hono";
|
|
7
|
+
import { logger as L } from "hono/logger";
|
|
8
|
+
import { remix as b } from "remix-hono/handler";
|
|
9
|
+
|
|
10
|
+
// src/dev-build.ts
|
|
11
|
+
var m = process.env.NODE_ENV === "production" ? void 0 : await import("vite").then(
|
|
12
|
+
(r) => r.createServer({
|
|
13
|
+
server: { middlewareMode: !0 },
|
|
14
|
+
appType: "custom"
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
async function p() {
|
|
18
|
+
return m?.ssrLoadModule("virtual:remix/server-build");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/middleware.ts
|
|
22
|
+
import { createMiddleware as v } from "hono/factory";
|
|
23
|
+
function n(r) {
|
|
24
|
+
return v(async (e, t) => {
|
|
25
|
+
if (!e.req.path.match(/\.[a-zA-Z0-9]+$/) || e.req.path.endsWith(".data"))
|
|
26
|
+
return t();
|
|
27
|
+
await t(), e.res.ok && e.res.headers.set("cache-control", `public, max-age=${r}`);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/node.ts
|
|
32
|
+
var y = {
|
|
33
|
+
defaultLogger: !0,
|
|
34
|
+
port: Number(process.env.PORT) || 3e3,
|
|
35
|
+
buildDirectory: "build/server",
|
|
36
|
+
serverBuildFile: "index.js",
|
|
37
|
+
assetsDir: "assets",
|
|
38
|
+
listeningListener: (r) => {
|
|
39
|
+
console.log(`\u{1F680} Server started on port ${r.port}`);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
async function P(r = {}) {
|
|
43
|
+
let e = {
|
|
44
|
+
...y,
|
|
45
|
+
...r,
|
|
46
|
+
defaultLogger: r.defaultLogger ?? !0
|
|
47
|
+
}, t = process.env.NODE_ENV === "test" ? "development" : process.env.NODE_ENV, i = t === "production", o = new x();
|
|
48
|
+
return o.use(
|
|
49
|
+
`/${e.assetsDir}/*`,
|
|
50
|
+
n(60 * 60 * 24 * 365),
|
|
51
|
+
// 1 year
|
|
52
|
+
a({ root: "./build/client" })
|
|
53
|
+
), o.use("*", n(60 * 60), a({ root: i ? "./build/client" : "./public" })), e.defaultLogger && o.use("*", L()), e.configure && await e.configure(o), o.use(async (u, l) => {
|
|
54
|
+
let s = i ? await import(
|
|
55
|
+
/* @vite-ignore */
|
|
56
|
+
f.pathToFileURL(
|
|
57
|
+
d.resolve(
|
|
58
|
+
d.join(process.cwd(), `./${e.buildDirectory}/${e.serverBuildFile}`)
|
|
59
|
+
)
|
|
60
|
+
).toString()
|
|
61
|
+
) : await p();
|
|
62
|
+
return b({
|
|
63
|
+
build: s,
|
|
64
|
+
mode: t,
|
|
65
|
+
getLoadContext(c) {
|
|
66
|
+
return e.getLoadContext ? e.getLoadContext(c, { build: s }) : {};
|
|
67
|
+
}
|
|
68
|
+
})(u, l);
|
|
69
|
+
}), i && g(
|
|
70
|
+
{
|
|
71
|
+
...o,
|
|
72
|
+
port: e.port
|
|
73
|
+
},
|
|
74
|
+
e.listeningListener
|
|
75
|
+
), o;
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
P as createHonoServer
|
|
79
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-router-hono-server",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
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,8 +28,8 @@
|
|
|
28
28
|
"scripts": {
|
|
29
29
|
"test": "vitest run",
|
|
30
30
|
"test:cov": "vitest run --coverage",
|
|
31
|
-
"build": "tsup src/node.ts src/dev.ts src/middlewares.ts --dts --format esm --outDir dist --
|
|
32
|
-
"remix:dev": "npm run dev -w
|
|
31
|
+
"build": "tsup src/node.ts src/dev.ts src/middlewares.ts --dts --format esm --outDir dist --clean --minifyIdentifiers --minifySyntax --external '../build/server/index.js'",
|
|
32
|
+
"remix:dev": "npm run dev -w remix-stable",
|
|
33
33
|
"build:dev": "tsup src/node.ts src/dev.ts src/middlewares.ts --dts --format esm --outDir dist --external '../build/server/index.js'",
|
|
34
34
|
"build:dev:watch": "npm run build:dev -- --watch",
|
|
35
35
|
"dev": "npm-run-all -s build:dev -p remix:dev build:dev:watch",
|
|
@@ -49,18 +49,17 @@
|
|
|
49
49
|
"url": "https://github.com/rphlmr/react-router-hono-server/issues"
|
|
50
50
|
},
|
|
51
51
|
"files": ["dist"],
|
|
52
|
-
"workspaces": [".", "
|
|
52
|
+
"workspaces": [".", "examples/*"],
|
|
53
53
|
"homepage": "https://github.com/rphlmr/react-router-hono-server#readme",
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@hono/node-server": "^1.12.0",
|
|
56
56
|
"@hono/vite-dev-server": "^0.13.1",
|
|
57
|
-
"@remix-run/node": "^2.11.0",
|
|
58
57
|
"hono": "^4.5.3",
|
|
59
|
-
"remix-hono": "^0.0.16"
|
|
60
|
-
"vite": "^5.3.5"
|
|
58
|
+
"remix-hono": "^0.0.16"
|
|
61
59
|
},
|
|
62
60
|
"devDependencies": {
|
|
63
61
|
"@biomejs/biome": "^1.8.3",
|
|
62
|
+
"@remix-run/node": "^2.11.1",
|
|
64
63
|
"@types/node": "^20.12.7",
|
|
65
64
|
"@vitest/coverage-v8": "^1.5.2",
|
|
66
65
|
"happy-dom": "^14.7.1",
|
|
@@ -70,6 +69,12 @@
|
|
|
70
69
|
"typescript": "^5.4.5",
|
|
71
70
|
"vitest": "^1.5.2"
|
|
72
71
|
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@remix-run/node": "^2.0.0",
|
|
74
|
+
"remix-hono": "^0.0.16",
|
|
75
|
+
"hono": "^4.5.3",
|
|
76
|
+
"vite": "^5.0.0"
|
|
77
|
+
},
|
|
73
78
|
"optionalDependencies": {
|
|
74
79
|
"@rollup/rollup-darwin-x64-gnu": "4.18.1",
|
|
75
80
|
"@rollup/rollup-darwin-x64-musl": "4.18.1",
|
package/dist/chunk-4CKI2DYM.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{createMiddleware as t}from"hono/factory";function n(a){return t(async(e,r)=>{if(!e.req.path.match(/\.[a-zA-Z0-9]+$/)||e.req.path.endsWith(".data"))return r();await r(),e.res.ok&&e.res.headers.set("cache-control",`public, max-age=${a}`)})}export{n as a};
|
package/dist/middlewares.d.mts
DELETED
package/dist/middlewares.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a}from"./chunk-4CKI2DYM.mjs";export{a as cache};
|