react-router-hono-server 4.0.0 → 4.0.1

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
@@ -766,6 +766,27 @@ export default {
766
766
 
767
767
  React Router also accepts `basename`, `appDirectory`, and `buildDirectory` in the same configuration file.
768
768
 
769
+ #### React Router `basename` and Vite `base`
770
+
771
+ React Router's [`basename`](https://reactrouter.com/api/framework-conventions/react-router.config.ts#basename) and Vite's [`base`](https://vite.dev/config/shared-options.html#base) configure independent URL spaces:
772
+
773
+ - `basename` mounts documents, route data requests, and the React Router handler.
774
+ - `base` controls Vite development URLs and URLs emitted for generated JavaScript, CSS, and other bundled assets.
775
+
776
+ For an application deployed entirely beneath `/v2`, configure both upstream tools:
777
+
778
+ ```ts
779
+ // react-router.config.ts
780
+ export default { basename: "/v2" };
781
+
782
+ // vite.config.ts
783
+ export default defineConfig({ base: "/v2/", plugins: [reactRouterHonoServer(), reactRouter()] });
784
+ ```
785
+
786
+ The prefixes may intentionally differ. For example, `basename: "/v2/app"` with `base: "/v2/"` keeps application documents beneath `/v2/app` while generated assets remain beneath `/v2/assets`. Likewise, `basename: "/"` with `base: "/v2/"` keeps documents at the origin root and moves only Vite-owned URLs.
787
+
788
+ The Node, Bun, and Deno adapters serve generated assets locally when `base` is an absolute pathname. Cloudflare Workers passes the original asset URL to its asset binding, and AWS production expects an external asset service. Full-URL bases remain externally owned. Relative bases (`""` and `"./"`) are preserved as emitted by Vite and do not create a fixed server-side mount. Public-directory files always remain available from the origin root rather than beneath `base`.
789
+
769
790
  #### Deployment behavior
770
791
 
771
792
  | Configuration | Node, Bun, Deno, and Cloudflare Workers | AWS Lambda |
@@ -1,6 +1,7 @@
1
1
  import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild, t as attachWebSocketToVite } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
3
  import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
4
+ import { i as viteGeneratedAssetsRoute, n as composeViteAssetRewriteRequestPath, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
4
5
  import { Hono } from "hono";
5
6
  import { createMiddleware } from "hono/factory";
6
7
  import { logger } from "hono/logger";
@@ -19,6 +20,7 @@ async function createHonoServer(options) {
19
20
  const mode = getBuildMode();
20
21
  const PRODUCTION = mode === "production";
21
22
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
23
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
22
24
  if (PRODUCTION && mergedOptions.onGracefulShutdown) shutdownCallback = mergedOptions.onGracefulShutdown;
23
25
  const app = new Hono(mergedOptions.app);
24
26
  const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
@@ -35,9 +37,11 @@ async function createHonoServer(options) {
35
37
  */
36
38
  if (!isReactRouterBuildRequest()) {
37
39
  const { serveStatic } = await import("hono/bun");
38
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({
40
+ const rewriteClientAssetsPath = composeViteAssetRewriteRequestPath(vitePublicPath, mergedOptions.serveStaticOptions?.clientAssets?.rewriteRequestPath);
41
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveStatic({
39
42
  root: clientBuildPath,
40
- ...mergedOptions.serveStaticOptions?.clientAssets
43
+ ...mergedOptions.serveStaticOptions?.clientAssets,
44
+ rewriteRequestPath: rewriteClientAssetsPath
41
45
  }));
42
46
  /**
43
47
  * Serve public files
@@ -1,5 +1,6 @@
1
1
  import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
+ import { i as viteGeneratedAssetsRoute, r as stripVitePathnamePrefix, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
3
4
  import { Hono } from "hono";
4
5
  import { createMiddleware } from "hono/factory";
5
6
  import { logger } from "hono/logger";
@@ -13,6 +14,7 @@ async function createHonoServer(options) {
13
14
  };
14
15
  const mode = getBuildMode();
15
16
  const PRODUCTION = mode === "production";
17
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
16
18
  const app = new Hono(mergedOptions.app);
17
19
  const { upgradeWebSocket } = await createWebSocket({
18
20
  app,
@@ -25,7 +27,7 @@ async function createHonoServer(options) {
25
27
  /**
26
28
  * Serve assets files from build/client/assets
27
29
  */
28
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveCloudflareAssets());
30
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveCloudflareAssets(vitePublicPath));
29
31
  /**
30
32
  * Serve public files
31
33
  */
@@ -71,7 +73,7 @@ let warned = false;
71
73
  *
72
74
  * https://github.com/sergiodxa/remix-hono/blob/main/src/cloudflare.ts
73
75
  */
74
- function serveCloudflareAssets() {
76
+ function serveCloudflareAssets(vitePublicPath) {
75
77
  return createMiddleware(async (c, next) => {
76
78
  const binding = c.env?.ASSETS;
77
79
  if (!binding) {
@@ -81,7 +83,9 @@ function serveCloudflareAssets() {
81
83
  }
82
84
  let response;
83
85
  try {
84
- response = await binding.fetch(c.req.url, c.req.raw.clone());
86
+ const url = new URL(c.req.url);
87
+ if (vitePublicPath) url.pathname = stripVitePathnamePrefix(url.pathname, vitePublicPath);
88
+ response = await binding.fetch(url.toString(), c.req.raw.clone());
85
89
  } catch {
86
90
  return next();
87
91
  }
@@ -1,6 +1,7 @@
1
1
  import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild, t as attachWebSocketToVite } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
3
  import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
4
+ import { i as viteGeneratedAssetsRoute, n as composeViteAssetRewriteRequestPath, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
4
5
  import { Hono } from "hono";
5
6
  import { createMiddleware } from "hono/factory";
6
7
  import { logger } from "hono/logger";
@@ -19,6 +20,7 @@ async function createHonoServer(options) {
19
20
  const mode = getBuildMode();
20
21
  const PRODUCTION = mode === "production";
21
22
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
23
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
22
24
  const app = new Hono(mergedOptions.app);
23
25
  const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
24
26
  app,
@@ -35,9 +37,11 @@ async function createHonoServer(options) {
35
37
  */
36
38
  if (!isReactRouterBuildRequest()) {
37
39
  const { serveStatic } = await import("hono/deno");
38
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({
40
+ const rewriteClientAssetsPath = composeViteAssetRewriteRequestPath(vitePublicPath, mergedOptions.serveStaticOptions?.clientAssets?.rewriteRequestPath);
41
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveStatic({
39
42
  root: clientBuildPath,
40
- ...mergedOptions.serveStaticOptions?.clientAssets
43
+ ...mergedOptions.serveStaticOptions?.clientAssets,
44
+ rewriteRequestPath: rewriteClientAssetsPath
41
45
  }));
42
46
  /**
43
47
  * Serve public files
@@ -1,6 +1,7 @@
1
1
  import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild, t as attachWebSocketToVite } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
3
  import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
4
+ import { i as viteGeneratedAssetsRoute, n as composeViteAssetRewriteRequestPath, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
4
5
  import { serve } from "@hono/node-server";
5
6
  import { serveStatic } from "@hono/node-server/serve-static";
6
7
  import { Hono } from "hono";
@@ -27,6 +28,7 @@ async function createHonoServer(options) {
27
28
  const mode = getBuildMode();
28
29
  const PRODUCTION = mode === "production";
29
30
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
31
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
30
32
  const app = new Hono(mergedOptions.app);
31
33
  const { upgradeWebSocket, injectWebSocket, nodeWebSocket } = await createWebSocket({
32
34
  app,
@@ -40,9 +42,11 @@ async function createHonoServer(options) {
40
42
  /**
41
43
  * Serve assets files from build/client/assets
42
44
  */
43
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({
45
+ const rewriteClientAssetsPath = composeViteAssetRewriteRequestPath(vitePublicPath, mergedOptions.serveStaticOptions?.clientAssets?.rewriteRequestPath);
46
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveStatic({
44
47
  root: clientBuildPath,
45
- ...mergedOptions.serveStaticOptions?.clientAssets
48
+ ...mergedOptions.serveStaticOptions?.clientAssets,
49
+ rewriteRequestPath: rewriteClientAssetsPath
46
50
  }));
47
51
  /**
48
52
  * Serve public files
package/dist/dev.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { t as isReactRouterBuildRequest } from "./react-router-build-request-_jlIVza5.js";
2
+ import { a as vitePathnamePrefix, t as classifyVitePublicPath } from "./vite-public-path-Aqd7hZ0d.js";
2
3
  import honoDevServer from "@hono/vite-dev-server";
3
4
  import bunAdapter from "@hono/vite-dev-server/bun";
4
5
  import nodeAdapter from "@hono/vite-dev-server/node";
@@ -41,7 +42,8 @@ function reactRouterHonoServer(options = {}) {
41
42
  "import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY": JSON.stringify(pluginConfig.buildDirectory),
42
43
  "import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR": JSON.stringify(pluginConfig.assetsDir),
43
44
  "import.meta.env.REACT_ROUTER_HONO_SERVER_RUNTIME": JSON.stringify(runtime),
44
- "import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME": JSON.stringify(pluginConfig.basename)
45
+ "import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME": JSON.stringify(pluginConfig.basename),
46
+ "import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE": JSON.stringify(pluginConfig.viteBase)
45
47
  },
46
48
  ssr: {
47
49
  target: runtime === "cloudflare" ? "webworker" : void 0,
@@ -102,19 +104,27 @@ function reactRouterHonoServer(options = {}) {
102
104
  });
103
105
  return { env: Deno.env.toObject() };
104
106
  };
107
+ const vitePrefix = vitePathnamePrefix(classifyVitePublicPath(pluginConfig.viteBase));
108
+ const appDirectory = pluginConfig.appDirectory.replace(/^[/\\]+|[/\\]+$/g, "").replaceAll(/[/\\]+/g, "/");
109
+ const sourceDirectory = appDirectory.split("/")[0];
110
+ const appPath = `${vitePrefix}/${appDirectory}`;
111
+ const sourcePath = `${vitePrefix}/${sourceDirectory}`;
112
+ const escapedAppPath = escapeRegExp(appPath);
113
+ const escapedSourcePath = escapeRegExp(sourcePath);
114
+ const escapedVitePrefix = escapeRegExp(vitePrefix);
105
115
  devServerPlugin = honoDevServer({
106
116
  adapter,
107
117
  injectClientScript: false,
108
118
  entry: pluginConfig.serverEntryPoint,
109
119
  export: options.dev?.export || "default",
110
120
  exclude: [
111
- new RegExp(`^(?=\\/${pluginConfig.appDirectory.replace(/^[/\\]+|[/\\]+$/g, "").replaceAll(/[/\\]+/g, "/")}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`),
112
- new RegExp(`^(?=\\/${pluginConfig.appDirectory.replace(/^[/\\]+|[/\\]+$/g, "").replaceAll(/[/\\]+/g, "/").split("/")[0]}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`),
121
+ new RegExp(`^(?=${escapedAppPath}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`),
122
+ new RegExp(`^(?=${escapedSourcePath}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`),
113
123
  /\?import(\?.*)?$/,
114
- /^\/@.+$/,
115
- /^\/node_modules\/.*/,
116
- `^(?=\\/${pluginConfig.appDirectory.replace(/^[/\\]+|[/\\]+$/g, "").replace(/[/\\]+/g, "/")}/**/.*/**)`,
117
- `^(?=\\/${pluginConfig.appDirectory.replace(/^[/\\]+|[/\\]+$/g, "").replace(/[/\\]+/g, "/").split("/")[0]}/**/.*/**)`,
124
+ new RegExp(`^${escapedVitePrefix}\\/@.+$`),
125
+ new RegExp(`^${escapedVitePrefix}\\/node_modules\\/.*`),
126
+ `^(?=${appPath}/**/.*/**)`,
127
+ `^(?=${sourcePath}/**/.*/**)`,
118
128
  ...pluginConfig.dev?.exclude || []
119
129
  ]
120
130
  });
@@ -171,6 +181,7 @@ function resolvePluginConfig(config, options) {
171
181
  const serverEntryPoint = options.serverEntryPoint || findDefaultServerEntry(appDirectory);
172
182
  const serverBuildFile = reactRouterConfig.serverBuildFile;
173
183
  const basename = reactRouterConfig.basename;
184
+ const viteBase = config.base ?? "/";
174
185
  return {
175
186
  rootDirectory,
176
187
  buildDirectory,
@@ -180,10 +191,14 @@ function resolvePluginConfig(config, options) {
180
191
  serverEntryPoint,
181
192
  dev: options.dev,
182
193
  serverBuildFile,
183
- basename
194
+ basename,
195
+ viteBase
184
196
  };
185
197
  }
186
198
  let warned = false;
199
+ function escapeRegExp(value) {
200
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
201
+ }
187
202
  function findDefaultServerEntry(appDirectory) {
188
203
  const fileWay = `${appDirectory}/server.ts`;
189
204
  const folderWay = `${appDirectory}/server/index.ts`;
@@ -0,0 +1,59 @@
1
+ //#region src/vite-public-path.ts
2
+ function classifyVitePublicPath(raw) {
3
+ if (raw === "" || raw === "./") return {
4
+ kind: "relative",
5
+ raw,
6
+ pathname: "/"
7
+ };
8
+ if (raw.startsWith("/")) {
9
+ const pathname = normalizeAbsolutePathname(raw);
10
+ return pathname === "/" ? {
11
+ kind: "root",
12
+ raw,
13
+ pathname
14
+ } : {
15
+ kind: "absolute",
16
+ raw,
17
+ pathname
18
+ };
19
+ }
20
+ try {
21
+ return {
22
+ kind: "url",
23
+ raw,
24
+ pathname: normalizeAbsolutePathname(new URL(raw).pathname)
25
+ };
26
+ } catch {
27
+ return {
28
+ kind: "relative",
29
+ raw,
30
+ pathname: "/"
31
+ };
32
+ }
33
+ }
34
+ function vitePathnamePrefix(publicPath) {
35
+ return publicPath.pathname === "/" ? "" : publicPath.pathname.slice(0, -1);
36
+ }
37
+ function viteGeneratedAssetsRoute(publicPath, assetsDir) {
38
+ return `${publicPath.kind === "absolute" ? vitePathnamePrefix(publicPath) : ""}/${assetsDir.replace(/^\/+|\/+$/g, "")}/*`;
39
+ }
40
+ function stripVitePathnamePrefix(pathname, publicPath) {
41
+ if (publicPath.kind !== "absolute" || publicPath.pathname === "/") return pathname;
42
+ const prefix = vitePathnamePrefix(publicPath);
43
+ if (pathname === prefix) return "/";
44
+ if (pathname.startsWith(`${prefix}/`)) return pathname.slice(prefix.length);
45
+ return pathname;
46
+ }
47
+ function composeViteAssetRewriteRequestPath(publicPath, userRewrite) {
48
+ return (requestPath, context) => {
49
+ const logicalPath = stripVitePathnamePrefix(requestPath, publicPath);
50
+ if (!userRewrite) return logicalPath;
51
+ return context === void 0 ? userRewrite(logicalPath) : userRewrite(logicalPath, context);
52
+ };
53
+ }
54
+ function normalizeAbsolutePathname(pathname) {
55
+ const withLeadingSlash = pathname.startsWith("/") ? pathname : `/${pathname}`;
56
+ return withLeadingSlash === "/" ? "/" : `${withLeadingSlash.replace(/\/+$/, "")}/`;
57
+ }
58
+ //#endregion
59
+ export { vitePathnamePrefix as a, viteGeneratedAssetsRoute as i, composeViteAssetRewriteRequestPath as n, stripVitePathnamePrefix as r, classifyVitePublicPath as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-hono-server",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "The Vite plugin you need to create a Hono server for your React Router app in less than 10 seconds.",
5
5
  "homepage": "https://github.com/rphlmr/react-router-hono-server#readme",
6
6
  "bugs": {