Repository: Rezact/Rezact
Pull request #92 (2024-01-16 01:23:07)

Fix root 404 issue and preserve query params


Patch:
diff --git a/package.json b/package.json
index 2dbe81a..0f9ddfc 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@rezact/rezact",
-  "version": "1.0.15-beta.39",
+  "version": "1.0.15-beta.40",
   "type": "module",
   "description": "Intuitive Reactivity, Simplified State. Embrace a modern UI framework that encourages direct data mutations, offers module-level reactivity, and simplifies component design. With Rezact, you get the power of reactivity without the boilerplate. Dive into a seamless development experience tailored for today's web.",
   "main": "index.cjs",
diff --git a/src/lib/rezact/router.ts b/src/lib/rezact/router.ts
index 0bed9bb..48f699f 100644
--- a/src/lib/rezact/router.ts
+++ b/src/lib/rezact/router.ts
@@ -12,7 +12,7 @@ function copyNextRoute(nextRoute, router) {
   const pathObj = typeof nextRoute === "object";
   const newURLObj = (pathObj
     ? new URL(nextRoute)
-    : new URL(nextRoute, window.location.origin)) as unknown as routeIF;
+    : new URL(nextRoute, window.location.href)) as unknown as routeIF;
 
   ($currentRoute as any).set(nextRoute.route || "/");
   ($currentPath as any).set(newURLObj.pathname);
@@ -189,7 +189,7 @@ export class TrieRouter {
   routeChanged(path = null, replace = false) {
     this.replaceState = replace;
     if (path instanceof PopStateEvent) this.popState = true;
-    const url = path || window.location.href;
+    const url = path || window.location.pathname;
 
     let pathObj = this.getNextRoute(url);
     this.runBeforeHooks(pathObj);
@@ -291,7 +291,12 @@ export class TrieRouter {
           params[`$${paramName}`] = new Signal(part);
           params[`$${paramName}`].subscribe((val) => {
             const newURL = this.getNextRoute(location.pathname, paramName, val);
-            val !== part && history.pushState({}, "", newURL.builtPath);
+
+            history.pushState(
+              {},
+              "",
+              newURL.builtPath + newURL.search + newURL.hash
+            );
           });
         }
 
@@ -307,7 +312,12 @@ export class TrieRouter {
           params[`$${paramName}`] = new Signal(rest);
           params[`$${paramName}`].subscribe((val) => {
             const newURL = this.getNextRoute(location.pathname, paramName, val);
-            val !== rest && history.pushState({}, "", newURL.builtPath);
+
+            history.pushState(
+              {},
+              "",
+              newURL.builtPath + newURL.search + newURL.hash
+            );
           });
         }
         currentNode = currentNode.wildcardHandler;
@@ -324,6 +334,8 @@ export class TrieRouter {
 
     const newURLObj = new URL(path, window.location.href) as unknown as routeIF;
 
+    newURLObj.hash = window.location.hash;
+    newURLObj.search = window.location.search;
     newURLObj.route = route;
     newURLObj.builtPath = builtPath;
     newURLObj.params = params;


Test patch:
diff --git a/src/router.test.js b/src/router.test.js
index 6b3cca6..4c0ed56 100644
--- a/src/router.test.js
+++ b/src/router.test.js
@@ -39,7 +39,8 @@ describe("Router Tests Suite", () => {
   it("First Render Home Page", async () => {
     document.body.innerHTML = `<div id="app"></div>`;
     const { router: _router } = await import("./components/appRouter");
-    _router.routeRequest("/");
+    // _router.routeRequest("/");
+    _router.routeChanged();
     router = _router;
 
     const allHeaders = await screen.findAllByRole("heading", {
@@ -861,7 +862,6 @@ describe("Router Tests Suite", () => {
     expect(router.currentRoute.pathname).toBe("/data-fetching");
     router.afterHooks = [];
     expect(beforeEachGuardTest.count).toBe(3);
-    console.log(beforeEachGuardTest);
   });
 });