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 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
 
@@ -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 hono remix-hono react-router-hono-server
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 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/dev.mjs CHANGED
@@ -1 +1,27 @@
1
- import t from"@hono/vite-dev-server";var o={entry:"app/entry.server.tsx",exportName:"server",appDirectory:"app",exclude:[]};function p(r){let e={...o,...r};return t({injectClientScript:!1,entry:e.entry,export:e.exportName,exclude:[`/${e.appDirectory}/**`,/^\/@.+$/,/^\/node_modules\/.*/,...e.exclude]})}export{p as devServer};
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 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 +1,79 @@
1
- import{a as i}from"./chunk-4CKI2DYM.mjs";import d from"node:path";import v from"node:url";import{serve as g}from"@hono/node-server";import{serveStatic as u}from"@hono/node-server/serve-static";import{Hono as f}from"hono";import{logger as x}from"hono/logger";import{remix as L}from"remix-hono/handler";var c=process.env.NODE_ENV==="production"?void 0:await import("vite").then(o=>o.createServer({server:{middlewareMode:!0},appType:"custom"}));async function p(){return c?.ssrLoadModule("virtual:remix/server-build")}var b={defaultLogger:!0,port:Number(process.env.PORT)||3e3,buildDirectory:"build/server",serverBuildFile:"index.js",assetsDir:"assets",listeningListener:o=>{console.log(`\u{1F680} Server started on port ${o.port}`)}};async function M(o={}){let e={...b,...o,defaultLogger:o.defaultLogger??!0},n=process.env.NODE_ENV==="test"?"development":process.env.NODE_ENV,t=n==="production",r=new f;return r.use(`/${e.assetsDir}/*`,i(60*60*24*365),u({root:"./build/client"})),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)=>{let s=t?await import(v.pathToFileURL(d.resolve(d.join(process.cwd(),`./${e.buildDirectory}/${e.serverBuildFile}`))).toString()):await p();return L({build:s,mode:n,getLoadContext(m){return e.getLoadContext?e.getLoadContext(m,{build:s}):{}}})(l,a)}),t&&g({...r,port:e.port},e.listeningListener),r}export{M as createHonoServer};
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.2",
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 --minify --clean --external '../build/server/index.js'",
32
- "remix:dev": "npm run dev -w example",
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": [".", "example"],
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",
@@ -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};
@@ -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 +0,0 @@
1
- import{a}from"./chunk-4CKI2DYM.mjs";export{a as cache};