@zenithbuild/router 0.7.0 → 0.7.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenithbuild/router",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "File-based SPA router for Zenith framework with deterministic, compile-time route resolution",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/template-core.js CHANGED
@@ -6,6 +6,9 @@ void __zenithHydrate;
6
6
  void __zenithOnMount;
7
7
 
8
8
  const __ZENITH_MANIFEST__ = ${manifest};
9
+ const __ZENITH_BASE_PATH__ = normalizeBasePath(
10
+ typeof __ZENITH_MANIFEST__.base_path === "string" ? __ZENITH_MANIFEST__.base_path : "/"
11
+ );
9
12
  const __ZENITH_ROUTE_EVENT_KEY = "__zenith_route_event_listeners";
10
13
  const __ZENITH_ROUTE_EVENT_NAMES = [
11
14
  "guard:start",
@@ -43,6 +46,48 @@ function splitPath(path) {
43
46
  return path.split('/').filter(Boolean);
44
47
  }
45
48
 
49
+ function normalizeBasePath(basePath) {
50
+ const raw = typeof basePath === "string" ? basePath.trim() : "";
51
+ if (!raw || raw === "/") return "/";
52
+ if (!raw.startsWith("/")) return "/";
53
+ const normalized = raw.replace(/\\/{2,}/g, "/").replace(/\\/+$/g, "");
54
+ return normalized || "/";
55
+ }
56
+
57
+ function normalizePublicPath(pathname) {
58
+ const raw = typeof pathname === "string" && pathname ? pathname : "/";
59
+ const withLeadingSlash = raw.startsWith("/") ? raw : "/" + raw;
60
+ const normalized = withLeadingSlash.replace(/\\/{2,}/g, "/");
61
+ if (normalized.length > 1) {
62
+ return normalized.replace(/\\/+$/g, "");
63
+ }
64
+ return normalized;
65
+ }
66
+
67
+ function prependBasePath(pathname) {
68
+ const normalizedPath = normalizePublicPath(pathname);
69
+ if (__ZENITH_BASE_PATH__ === "/") return normalizedPath;
70
+ if (normalizedPath === "/") return __ZENITH_BASE_PATH__;
71
+ if (normalizedPath === __ZENITH_BASE_PATH__ || normalizedPath.startsWith(__ZENITH_BASE_PATH__ + "/")) {
72
+ return normalizedPath;
73
+ }
74
+ return __ZENITH_BASE_PATH__ + normalizedPath;
75
+ }
76
+
77
+ function stripBasePath(pathname) {
78
+ const normalizedPath = normalizePublicPath(pathname);
79
+ if (__ZENITH_BASE_PATH__ === "/") return normalizedPath;
80
+ if (normalizedPath === __ZENITH_BASE_PATH__) return "/";
81
+ if (normalizedPath.startsWith(__ZENITH_BASE_PATH__ + "/")) {
82
+ return normalizedPath.slice(__ZENITH_BASE_PATH__.length) || "/";
83
+ }
84
+ return null;
85
+ }
86
+
87
+ function routeCheckPath() {
88
+ return prependBasePath("/__zenith/route-check");
89
+ }
90
+
46
91
  function copyParams(params) {
47
92
  if (!params || typeof params !== "object") {
48
93
  return {};
@@ -109,11 +154,16 @@ function compareRouteSpecificity(a, b) {
109
154
  }
110
155
 
111
156
  function resolveRoute(pathname) {
112
- if (__ZENITH_MANIFEST__.chunks[pathname]) {
113
- return { route: pathname, params: {} };
157
+ const canonicalPathname = stripBasePath(pathname);
158
+ if (canonicalPathname === null) {
159
+ return null;
160
+ }
161
+
162
+ if (__ZENITH_MANIFEST__.chunks[canonicalPathname]) {
163
+ return { route: canonicalPathname, params: {} };
114
164
  }
115
165
 
116
- const pathnameSegments = splitPath(pathname);
166
+ const pathnameSegments = splitPath(canonicalPathname);
117
167
  const routes = Object.keys(__ZENITH_MANIFEST__.chunks).sort(compareRouteSpecificity);
118
168
  for (let i = 0; i < routes.length; i++) {
119
169
  const route = routes[i];
@@ -63,7 +63,7 @@ async function requestRouteCheck(context, resolved, targetUrl, signal) {
63
63
  });
64
64
 
65
65
  try {
66
- const response = await fetch("/__zenith/route-check?path=" + encodeURIComponent(toNavigationPath(targetUrl)), {
66
+ const response = await fetch(routeCheckPath() + "?path=" + encodeURIComponent(toNavigationPath(targetUrl)), {
67
67
  headers: { "x-zenith-route-check": "1" },
68
68
  credentials: "include",
69
69
  signal
@@ -363,7 +363,7 @@ function isInternalLink(anchor) {
363
363
  return false;
364
364
  }
365
365
  const url = new URL(anchor.href, window.location.href);
366
- return url.origin === window.location.origin;
366
+ return url.origin === window.location.origin && stripBasePath(url.pathname) !== null;
367
367
  }
368
368
 
369
369
  async function mountInitialRoute() {