From f3e5b47124e12b09cebe7e581d34472f78504a9f Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Thu, 6 Jul 2023 14:16:02 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20replace=20path=20on=20internal?= =?UTF-8?q?=20navigation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don’t replace the path when we navigate internally. This means we keep the hash part of the URL even if it is not part of the route and we can remove the workaround for that. This also means that we keep the original URL if the parsed route would result in a different URL. This change is visible through a slight adjustments in the tests. Signed-off-by: Thomas Scholtes --- src/lib/router.ts | 20 +++----------------- src/views/projects/router.ts | 2 +- tests/e2e/hashRouter.spec.ts | 2 +- tests/e2e/historyRouter.spec.ts | 2 +- 4 files changed, 6 insertions(+), 20 deletions(-) diff --git a/src/lib/router.ts b/src/lib/router.ts index 8f2259dce..ea9c20b36 100644 --- a/src/lib/router.ts +++ b/src/lib/router.ts @@ -1,13 +1,12 @@ import type { BaseUrl } from "@httpd-client"; import type { LoadedRoute, Route } from "@app/lib/router/definitions"; -import { get, writable } from "svelte/store"; +import { writable } from "svelte/store"; import * as mutexExecutor from "@app/lib/mutexExecutor"; import * as utils from "@app/lib/utils"; import { config } from "@app/lib/config"; import { - createProjectRoute, projectRouteToPath, resolveProjectRoute, } from "@app/views/projects/router"; @@ -61,22 +60,9 @@ export async function loadFromLocation(): Promise { const relativeUrl = pathname + window.location.search + (hash || ""); const url = new URL(relativeUrl, window.origin); - let route = pathToRoute(url); + const route = pathToRoute(url); if (route) { - const activeRoute = get(activeRouteStore); - if ( - activeRoute.resource === "projects" && - route.resource === "projects" && - route.params.hash - ) { - if (route.params.hash.match(/^L\d+$/)) { - route = createProjectRoute(activeRoute, {}); - } else { - route = createProjectRoute(activeRoute, { hash: route.params.hash }); - } - } - await replace(route); } else { await replace({ resource: "notFound", params: { url: relativeUrl } }); @@ -99,7 +85,7 @@ async function navigate( if (action === "push") { window.history.pushState(newRoute, DOCUMENT_TITLE, path); } else if (action === "replace") { - window.history.replaceState(newRoute, DOCUMENT_TITLE, path); + window.history.replaceState(newRoute, DOCUMENT_TITLE); } currentUrl = new URL(window.location.href); diff --git a/src/views/projects/router.ts b/src/views/projects/router.ts index 16cb93f44..50e166317 100644 --- a/src/views/projects/router.ts +++ b/src/views/projects/router.ts @@ -463,7 +463,7 @@ function sanitizeQueryString(queryString: string): string { return queryString.startsWith("?") ? queryString.substring(1) : queryString; } -export function createProjectRoute( +function createProjectRoute( activeRoute: ProjectRoute, projectRouteParams: Partial, ): ProjectRoute { diff --git a/tests/e2e/hashRouter.spec.ts b/tests/e2e/hashRouter.spec.ts index 0cbf412cc..fb95689e0 100644 --- a/tests/e2e/hashRouter.spec.ts +++ b/tests/e2e/hashRouter.spec.ts @@ -31,7 +31,7 @@ test("navigation between seed and project pages", async ({ page }) => { await project.click(); await expect(page).toHaveURL(`/#${sourceBrowsingUrl}`); - await expectBackAndForwardNavigationWorks("/#/seeds/127.0.0.1", page); + await expectBackAndForwardNavigationWorks("/#/seeds/radicle.local", page); await expectUrlPersistsReload(page); await page.locator('role=link[name="radicle.local"]').click(); diff --git a/tests/e2e/historyRouter.spec.ts b/tests/e2e/historyRouter.spec.ts index 7cf47a87f..0035e60c3 100644 --- a/tests/e2e/historyRouter.spec.ts +++ b/tests/e2e/historyRouter.spec.ts @@ -31,7 +31,7 @@ test("navigation between seed and project pages", async ({ page }) => { await project.click(); await expect(page).toHaveURL(sourceBrowsingUrl); - await expectBackAndForwardNavigationWorks("/seeds/127.0.0.1", page); + await expectBackAndForwardNavigationWorks("/seeds/radicle.local", page); await expectUrlPersistsReload(page); await page.locator('role=link[name="radicle.local"]').click();