weifuwu 0.9.4 → 0.9.5

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/dist/index.js CHANGED
@@ -422,8 +422,13 @@ var Router = class _Router {
422
422
  const remainingSegments = segments.slice(remainingIdx);
423
423
  const delegate = (req2, ctx2) => sub.handle(req2, ctx2, remainingSegments, query);
424
424
  const allMws = this.globalMws.length + match.pathMws.length === 0 ? [] : [...this.globalMws, ...match.pathMws];
425
+ const levelMount = "/" + segments.slice(0, remainingIdx).join("/");
425
426
  try {
426
- return await this.runChain(allMws, delegate, req, { ...ctx, params: { ...ctx.params, ...match.params } });
427
+ return await this.runChain(allMws, delegate, req, {
428
+ ...ctx,
429
+ params: { ...ctx.params, ...match.params },
430
+ mountPath: (ctx.mountPath || "") + levelMount
431
+ });
427
432
  } catch (e) {
428
433
  return this.errorHandler ? this.errorHandler(e, req, ctx) : new Response("Internal Server Error", { status: 500 });
429
434
  }
@@ -908,6 +913,7 @@ async function getOrBuildClientBundle(entryPath, layoutPaths, pagesDir, router)
908
913
  }
909
914
  function makeSsrHandler(entryPath, layoutPaths, loadPath, pagesDir, router) {
910
915
  return async (req, ctx) => {
916
+ const base = ctx.mountPath || "";
911
917
  const pageMod = pageModules.get(entryPath);
912
918
  if (!pageMod) return new Response("", { status: 500 });
913
919
  const Component = pageMod.default;
@@ -936,7 +942,7 @@ function makeSsrHandler(entryPath, layoutPaths, loadPath, pagesDir, router) {
936
942
  scripts.push(`<script>window.__WEIFUWU_PROPS=${JSON.stringify(allProps)}</script>`);
937
943
  const bundle = await getOrBuildClientBundle(entryPath, layoutPaths, pagesDir, router);
938
944
  if (bundle) {
939
- scripts.push(`<script type="module" src="${bundle.url}"></script>`);
945
+ scripts.push(`<script type="module" src="${base}${bundle.url}"></script>`);
940
946
  }
941
947
  let html = `<!DOCTYPE html>
942
948
  ${body}
@@ -944,13 +950,13 @@ ${scripts.join("\n")}`;
944
950
  if (tailwindCssUrl && html.includes("</head>")) {
945
951
  html = html.replace(
946
952
  "</head>",
947
- `<link rel="stylesheet" href="${tailwindCssUrl}" />
953
+ `<link rel="stylesheet" href="${base}${tailwindCssUrl}" />
948
954
  </head>`
949
955
  );
950
956
  }
951
957
  if (isDev) {
952
958
  html += `
953
- <script>(function(){var ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'/__weifuwu/livereload');ws.onmessage=function(e){if(e.data==='reload')location.reload()};ws.onclose=function(){setTimeout(function(){location.reload()},500)}})()</script>`;
959
+ <script>(function(){var ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'${base}/__weifuwu/livereload');ws.onmessage=function(e){if(e.data==='reload')location.reload()};ws.onclose=function(){setTimeout(function(){location.reload()},500)}})()</script>`;
954
960
  }
955
961
  return new Response(html, {
956
962
  headers: { "content-type": "text/html; charset=utf-8" }
@@ -1049,6 +1055,7 @@ async function tsx(options) {
1049
1055
  }
1050
1056
  }
1051
1057
  const handler = async (req, ctx) => {
1058
+ const base = ctx.mountPath || "";
1052
1059
  const nfMod = pageModules.get(nfPath);
1053
1060
  if (!nfMod) return new Response("Not Found", { status: 404 });
1054
1061
  const NfComponent = nfMod.default;
@@ -1068,13 +1075,13 @@ ${body}`;
1068
1075
  if (tailwindCssUrl && html.includes("</head>")) {
1069
1076
  html = html.replace(
1070
1077
  "</head>",
1071
- `<link rel="stylesheet" href="${tailwindCssUrl}" />
1078
+ `<link rel="stylesheet" href="${base}${tailwindCssUrl}" />
1072
1079
  </head>`
1073
1080
  );
1074
1081
  }
1075
1082
  if (isDev) {
1076
1083
  html += `
1077
- <script>(function(){var ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'/__weifuwu/livereload');ws.onmessage=function(e){if(e.data==='reload')location.reload()};ws.onclose=function(){setTimeout(function(){location.reload()},500)}})()</script>`;
1084
+ <script>(function(){var ws=new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'${base}/__weifuwu/livereload');ws.onmessage=function(e){if(e.data==='reload')location.reload()};ws.onclose=function(){setTimeout(function(){location.reload()},500)}})()</script>`;
1078
1085
  }
1079
1086
  return new Response(html, {
1080
1087
  status: 404,
package/dist/types.d.ts CHANGED
@@ -3,6 +3,7 @@ export interface Context {
3
3
  query: Record<string, string>;
4
4
  user?: unknown;
5
5
  parsed?: Record<string, unknown>;
6
+ mountPath?: string;
6
7
  }
7
8
  export type Handler = (req: Request, ctx: Context) => Response | Promise<Response>;
8
9
  export type Middleware = (req: Request, ctx: Context, next: Handler) => Response | Promise<Response>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "weifuwu",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "Web-standard HTTP framework for Node.js — (req, ctx) => Response",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",