Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getNavHelper } from "internal-nav-helper";
import { useConnect } from "redux-bundler-hook";
import links from "./nav-links";
import { FaGithub } from "react-icons/fa";
import { normalizeUrlForHash } from "./app-url";

const version = import.meta.env.PKG_VERSION;
const BASE_URL = import.meta.env.BASE_URL;
Expand Down Expand Up @@ -38,9 +39,7 @@ function App() {
return (
<div
onClick={getNavHelper((url) => {
// Remove BASE_URL# before it is added again by the navhelper so it can be included in the hrefs for copy url purposes
url = url.replace(`${BASE_URL}#`, "");
doUpdateHash(url);
doUpdateHash(normalizeUrlForHash(url, BASE_URL));
})}
>
<SiteWrapper
Expand Down
29 changes: 29 additions & 0 deletions src/app-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Normalizes URLs captured from click events into the hash route shape expected
// by redux-bundler. This avoids re-appending BASE_URL or "#" and producing
// duplicated routes when docs are hosted under a subpath.
export function normalizeUrlForHash(url, baseUrl) {
if (!url) return "/";

const hashIndex = url.indexOf("#");
if (hashIndex >= 0) {
// Internal hash links are already pointing at the target route, so keep
// only the path after "#".
return url.slice(hashIndex + 1) || "/";
}

let basePath = "/";
try {
basePath = new URL(baseUrl, "http://localhost").pathname;
} catch {
basePath = "/";
}

const normalizedBasePath = `/${basePath.replace(/^\/+|\/+$/g, "")}/`;
if (normalizedBasePath !== "//" && url.startsWith(normalizedBasePath)) {
// Same-origin links without a hash still need the deploy subpath removed
// before they are handed to doUpdateHash.
return url.slice(normalizedBasePath.length - 1) || "/";
}

return url;
}
31 changes: 31 additions & 0 deletions src/app-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from "vitest";
import { normalizeUrlForHash } from "./app-url";

describe("normalizeUrlForHash", () => {
it("extracts the route from a same-origin hash URL under a base path", () => {
expect(
normalizeUrlForHash(
"/groundwork/#/docs/navigation/sidebar",
"https://usace.github.io/groundwork/",
),
).toBe("/docs/navigation/sidebar");
});

it("extracts the route from a root hash URL", () => {
expect(
normalizeUrlForHash(
"/#/docs/navigation/sidebar",
"http://localhost:5173/",
),
).toBe("/docs/navigation/sidebar");
});

it("strips the base path from non-hash same-origin URLs", () => {
expect(
normalizeUrlForHash(
"/groundwork/docs",
"https://usace.github.io/groundwork/",
),
).toBe("/docs");
});
});