react-router-hono-server 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md 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
- > Install [remix-hono](https://github.com/sergiodxa/remix-hono) to add cool middlewares like [`session`](https://github.com/sergiodxa/remix-hono?tab=readme-ov-file#session-management)
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
 
@@ -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
@@ -105,7 +126,7 @@ That's all!
105
126
  ### Options
106
127
 
107
128
  ```ts
108
- export type HonoServerOptions = {
129
+ export type HonoServerOptions<E extends Env = BlankEnv> = {
109
130
  /**
110
131
  * Enable the default logger
111
132
  *
@@ -143,11 +164,11 @@ export type HonoServerOptions = {
143
164
  */
144
165
  assetsDir?: string;
145
166
  /**
146
- * Customize the Hono server, for example, adding middlewares
167
+ * Customize the Hono server, for example, adding middleware
147
168
  *
148
- * It is applied after the default middlewares and before the remix middleware
169
+ * It is applied after the default middleware and before the remix middleware
149
170
  */
150
- configure?: (server: Hono) => Promise<void> | void;
171
+ configure?: <E extends Env = BlankEnv>(server: Hono<E>) => Promise<void> | void;
151
172
  /**
152
173
  * Augment the Remix AppLoadContext
153
174
  *
@@ -173,6 +194,12 @@ export type HonoServerOptions = {
173
194
  * Defaults log the port
174
195
  */
175
196
  listeningListener?: (info: { port: number }) => void;
197
+ /**
198
+ * Hono constructor options
199
+ *
200
+ * {@link HonoOptions}
201
+ */
202
+ honoOptions?: HonoOptions<E>;
176
203
  };
177
204
  ```
178
205
 
@@ -224,24 +251,24 @@ export async function loader({ context }: LoaderFunctionArgs) {
224
251
  }
225
252
  ```
226
253
 
227
- ## Middlewares
254
+ ## Middleware
228
255
 
229
- Middlewares are functions that are called before Remix calls your loader/action.
256
+ Middleware are functions that are called before Remix calls your loader/action.
230
257
 
231
258
  Hono is the perfect tool for this, as it supports middleware out of the box.
232
259
 
233
260
  See the [Hono docs](https://hono.dev/docs/guides/middleware) for more information.
234
261
 
235
- You can imagine many use cases for middlewares, such as authentication, protecting routes, caching, logging, etc.
262
+ You can imagine many use cases for middleware, such as authentication, protecting routes, caching, logging, etc.
236
263
 
237
264
  See how [Shelf.nu](https://github.com/Shelf-nu/shelf.nu/blob/main/server/middleware.ts) uses them!
238
265
 
239
266
  > [!TIP]
240
- > This lib exports one middleware `cache` (`react-router-hono-server/middlewares`) that you can use to cache your responses.
267
+ > This lib exports one middleware `cache` (`react-router-hono-server/middleware`) that you can use to cache your responses.
241
268
 
242
- ### Using Remix Hono middlewares
269
+ ### Using Remix Hono middleware
243
270
 
244
- It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono) middlewares with this package.
271
+ It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono) middleware with this package.
245
272
 
246
273
  ```ts
247
274
  import { createCookieSessionStorage } from "@remix-run/node";
@@ -284,7 +311,7 @@ export const server = await createHonoServer({
284
311
 
285
312
  ### Creating custom Middleware
286
313
 
287
- You can create middlewares using the [`createMiddleware`](https://hono.dev/docs/helpers/factory#createmiddleware) or [`createFactory`](https://hono.dev/docs/helpers/factory#createfactory) functions from `hono/factory`.
314
+ 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
315
 
289
316
  Then, use them with the `configure` function of `createHonoServer`.
290
317
 
package/dist/dev.d.mts CHANGED
@@ -29,7 +29,7 @@ type HonoDevServerOptions = {
29
29
  *
30
30
  * Defaults include `appDirectory` content.
31
31
  */
32
- exclude?: string[];
32
+ exclude?: (string | RegExp)[];
33
33
  };
34
34
  /**
35
35
  * Create a dev server for the Hono server
package/dist/node.d.mts CHANGED
@@ -1,9 +1,11 @@
1
1
  import * as hono_types from 'hono/types';
2
+ import { BlankEnv } from 'hono/types';
2
3
  import { AppLoadContext } from '@remix-run/node';
3
- import { Hono, Context } from 'hono';
4
+ import { Env, Hono, Context } from 'hono';
5
+ import { HonoOptions } from 'hono/hono-base';
4
6
  import { RemixMiddlewareOptions } from 'remix-hono/handler';
5
7
 
6
- type HonoServerOptions = {
8
+ type HonoServerOptions<E extends Env = BlankEnv> = {
7
9
  /**
8
10
  * Enable the default logger
9
11
  *
@@ -41,11 +43,11 @@ type HonoServerOptions = {
41
43
  */
42
44
  assetsDir?: string;
43
45
  /**
44
- * Customize the Hono server, for example, adding middlewares
46
+ * Customize the Hono server, for example, adding middleware
45
47
  *
46
- * It is applied after the default middlewares and before the remix middleware
48
+ * It is applied after the default middleware and before the remix middleware
47
49
  */
48
- configure?: (server: Hono) => Promise<void> | void;
50
+ configure?: <E extends Env = BlankEnv>(server: Hono<E>) => Promise<void> | void;
49
51
  /**
50
52
  * Augment the Remix AppLoadContext
51
53
  *
@@ -70,12 +72,18 @@ type HonoServerOptions = {
70
72
  listeningListener?: (info: {
71
73
  port: number;
72
74
  }) => void;
75
+ /**
76
+ * Hono constructor options
77
+ *
78
+ * {@link HonoOptions}
79
+ */
80
+ honoOptions?: HonoOptions<E>;
73
81
  };
74
82
  /**
75
83
  * Create a Hono server
76
84
  *
77
85
  * @param config {@link HonoServerOptions} - The configuration options for the server
78
86
  */
79
- declare function createHonoServer(options?: HonoServerOptions): Promise<Hono<hono_types.BlankEnv, hono_types.BlankSchema, "/">>;
87
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E, hono_types.BlankSchema, "/">>;
80
88
 
81
89
  export { type HonoServerOptions, createHonoServer };
package/dist/node.mjs CHANGED
@@ -1,15 +1,11 @@
1
- import {
2
- a as i
3
- } from "./chunk-GCTZCGY2.mjs";
4
-
5
1
  // src/node.ts
6
- import d from "node:path";
7
- import v from "node:url";
2
+ import a from "node:path";
3
+ import f from "node:url";
8
4
  import { serve as g } from "@hono/node-server";
9
- import { serveStatic as u } from "@hono/node-server/serve-static";
10
- import { Hono as f } from "hono";
5
+ import { serveStatic as d } from "@hono/node-server/serve-static";
6
+ import { Hono as E } from "hono";
11
7
  import { logger as x } from "hono/logger";
12
- import { remix as L } from "remix-hono/handler";
8
+ import { remix as b } from "remix-hono/handler";
13
9
 
14
10
  // src/dev-build.ts
15
11
  var c = process.env.NODE_ENV === "production" ? void 0 : await import("vite").then(
@@ -22,8 +18,18 @@ async function p() {
22
18
  return c?.ssrLoadModule("virtual:remix/server-build");
23
19
  }
24
20
 
21
+ // src/middleware.ts
22
+ import { createMiddleware as v } from "hono/factory";
23
+ function i(o) {
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=${o}`);
28
+ });
29
+ }
30
+
25
31
  // src/node.ts
26
- var b = {
32
+ var L = {
27
33
  defaultLogger: !0,
28
34
  port: Number(process.env.PORT) || 3e3,
29
35
  buildDirectory: "build/server",
@@ -35,32 +41,32 @@ var b = {
35
41
  };
36
42
  async function M(o = {}) {
37
43
  let e = {
38
- ...b,
44
+ ...L,
39
45
  ...o,
40
46
  defaultLogger: o.defaultLogger ?? !0
41
- }, n = process.env.NODE_ENV === "test" ? "development" : process.env.NODE_ENV, t = n === "production", r = new f();
47
+ }, t = process.env.NODE_ENV === "test" ? "development" : process.env.NODE_ENV, n = t === "production", r = new E(e.honoOptions);
42
48
  return r.use(
43
49
  `/${e.assetsDir}/*`,
44
50
  i(60 * 60 * 24 * 365),
45
51
  // 1 year
46
- u({ root: "./build/client" })
47
- ), r.use("*", i(60 * 60), u({ root: t ? "./build/client" : "./public" })), e.defaultLogger && r.use("*", x()), e.configure && await e.configure(r), r.use(async (l, a) => {
48
- let s = t ? await import(
52
+ d({ root: "./build/client" })
53
+ ), r.use("*", i(60 * 60), d({ root: n ? "./build/client" : "./public" })), e.defaultLogger && r.use("*", x()), e.configure && await e.configure(r), r.use(async (u, l) => {
54
+ let s = n ? await import(
49
55
  /* @vite-ignore */
50
- v.pathToFileURL(
51
- d.resolve(
52
- d.join(process.cwd(), `./${e.buildDirectory}/${e.serverBuildFile}`)
56
+ f.pathToFileURL(
57
+ a.resolve(
58
+ a.join(process.cwd(), `./${e.buildDirectory}/${e.serverBuildFile}`)
53
59
  )
54
60
  ).toString()
55
61
  ) : await p();
56
- return L({
62
+ return b({
57
63
  build: s,
58
- mode: n,
64
+ mode: t,
59
65
  getLoadContext(m) {
60
66
  return e.getLoadContext ? e.getLoadContext(m, { build: s }) : {};
61
67
  }
62
- })(l, a);
63
- }), t && g(
68
+ })(u, l);
69
+ }), n && g(
64
70
  {
65
71
  ...r,
66
72
  port: e.port
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "0.0.3",
3
+ "version": "0.1.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": {
7
7
  "import": {
8
8
  "import": "./dist/node.mjs",
9
- "default": "./dist/node.mjs",
10
- "types": "./dist/node.d.mts"
9
+ "types": "./dist/node.d.mts",
10
+ "default": "./dist/node.mjs"
11
11
  }
12
12
  },
13
13
  "./dev": {
14
14
  "import": {
15
15
  "import": "./dist/dev.mjs",
16
- "default": "./dist/dev.mjs",
17
- "types": "./dist/dev.d.mts"
16
+ "types": "./dist/dev.d.mts",
17
+ "default": "./dist/dev.mjs"
18
18
  }
19
19
  },
20
20
  "./middlewares": {
21
21
  "import": {
22
22
  "import": "./dist/middlewares.mjs",
23
- "default": "./dist/middlewares.mjs",
24
- "types": "./dist/middlewares.d.mts"
23
+ "types": "./dist/middlewares.d.mts",
24
+ "default": "./dist/middlewares.mjs"
25
25
  }
26
26
  }
27
27
  },
@@ -1,13 +0,0 @@
1
- // src/middlewares.ts
2
- import { createMiddleware as t } from "hono/factory";
3
- function n(a) {
4
- return t(async (e, r) => {
5
- if (!e.req.path.match(/\.[a-zA-Z0-9]+$/) || e.req.path.endsWith(".data"))
6
- return r();
7
- await r(), e.res.ok && e.res.headers.set("cache-control", `public, max-age=${a}`);
8
- });
9
- }
10
-
11
- export {
12
- n as a
13
- };
@@ -1,10 +0,0 @@
1
- import * as hono from 'hono';
2
-
3
- /**
4
- * Cache middleware
5
- *
6
- * @param seconds - The number of seconds to cache
7
- */
8
- declare function cache(seconds: number): hono.MiddlewareHandler<any, string, {}>;
9
-
10
- export { cache };
@@ -1,6 +0,0 @@
1
- import {
2
- a
3
- } from "./chunk-GCTZCGY2.mjs";
4
- export {
5
- a as cache
6
- };