react-router-hono-server 0.0.3 → 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 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
@@ -143,9 +164,9 @@ 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
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
- ## Middlewares
248
+ ## Middleware
228
249
 
229
- Middlewares are functions that are called before Remix calls your loader/action.
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 middlewares, such as authentication, protecting routes, caching, logging, etc.
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/middlewares`) that you can use to cache your responses.
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 middlewares
263
+ ### Using Remix Hono middleware
243
264
 
244
- It is easy to use [remix-hono](https://github.com/sergiodxa/remix-hono) middlewares with this package.
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 middlewares using the [`createMiddleware`](https://hono.dev/docs/helpers/factory#createmiddleware) or [`createFactory`](https://hono.dev/docs/helpers/factory#createfactory) functions from `hono/factory`.
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
@@ -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
@@ -41,9 +41,9 @@ type HonoServerOptions = {
41
41
  */
42
42
  assetsDir?: string;
43
43
  /**
44
- * Customize the Hono server, for example, adding middlewares
44
+ * Customize the Hono server, for example, adding middleware
45
45
  *
46
- * It is applied after the default middlewares and before the remix middleware
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,73 +1,79 @@
1
- import {
2
- a as i
3
- } from "./chunk-GCTZCGY2.mjs";
4
-
5
1
  // src/node.ts
6
2
  import d from "node:path";
7
- import v from "node:url";
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";
11
- import { logger as x } from "hono/logger";
12
- import { remix as L } from "remix-hono/handler";
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";
13
9
 
14
10
  // src/dev-build.ts
15
- var c = process.env.NODE_ENV === "production" ? void 0 : await import("vite").then(
16
- (o) => o.createServer({
11
+ var m = process.env.NODE_ENV === "production" ? void 0 : await import("vite").then(
12
+ (r) => r.createServer({
17
13
  server: { middlewareMode: !0 },
18
14
  appType: "custom"
19
15
  })
20
16
  );
21
17
  async function p() {
22
- return c?.ssrLoadModule("virtual:remix/server-build");
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
+ });
23
29
  }
24
30
 
25
31
  // src/node.ts
26
- var b = {
32
+ var y = {
27
33
  defaultLogger: !0,
28
34
  port: Number(process.env.PORT) || 3e3,
29
35
  buildDirectory: "build/server",
30
36
  serverBuildFile: "index.js",
31
37
  assetsDir: "assets",
32
- listeningListener: (o) => {
33
- console.log(`\u{1F680} Server started on port ${o.port}`);
38
+ listeningListener: (r) => {
39
+ console.log(`\u{1F680} Server started on port ${r.port}`);
34
40
  }
35
41
  };
36
- async function M(o = {}) {
42
+ async function P(r = {}) {
37
43
  let e = {
38
- ...b,
39
- ...o,
40
- defaultLogger: o.defaultLogger ?? !0
41
- }, n = process.env.NODE_ENV === "test" ? "development" : process.env.NODE_ENV, t = n === "production", r = new f();
42
- return r.use(
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(
43
49
  `/${e.assetsDir}/*`,
44
- i(60 * 60 * 24 * 365),
50
+ n(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
+ 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(
49
55
  /* @vite-ignore */
50
- v.pathToFileURL(
56
+ f.pathToFileURL(
51
57
  d.resolve(
52
58
  d.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,
59
- getLoadContext(m) {
60
- return e.getLoadContext ? e.getLoadContext(m, { build: s }) : {};
64
+ mode: t,
65
+ getLoadContext(c) {
66
+ return e.getLoadContext ? e.getLoadContext(c, { build: s }) : {};
61
67
  }
62
- })(l, a);
63
- }), t && g(
68
+ })(u, l);
69
+ }), i && g(
64
70
  {
65
- ...r,
71
+ ...o,
66
72
  port: e.port
67
73
  },
68
74
  e.listeningListener
69
- ), r;
75
+ ), o;
70
76
  }
71
77
  export {
72
- M as createHonoServer
78
+ P as createHonoServer
73
79
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "0.0.3",
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": {
@@ -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
- };