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 lib/composite/header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Button } from "../../components/button";
import { GiHamburgerMenu } from "react-icons/gi";
import { useState } from "react";
import gwMerge from "../../gw-merge";
import Link from "../../components/navigation/link";

const BASE_URL = import.meta.env.BASE_URL;

Expand All @@ -28,14 +27,14 @@ function Title({ title = "", subtitle = "" }) {
function Logo() {
return (
<div className="gw-relative gw-w-[70px] sm:gw-w-[82px] gw-shrink-0 sm:gw-top-3">
<Link href={BASE_URL + "/"}>
<a href={BASE_URL + "/"} rel="external">
<img
className="gw-w-[50px] sm:gw-w-[60px] gw-h-auto"
src={usaceLogo}
alt="U.S. Army Corps of Engineers"
aria-label="Visit the homepage of this site"
/>
</Link>
</a>
<div className="gw-absolute gw-left-[55px] sm:gw-left-[65px] gw-bottom-[-10px] sm:gw-bottom-[-9px] gw-text-sm gw-text-gray-400">
®
</div>
Expand Down
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
if (url.includes(`${BASE_URL}#`)) 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");
});
});
5 changes: 1 addition & 4 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export default defineConfig(({ mode }) => {
};
} else {
console.log("Building preview app", mode);
const base =
mode === "production"
? "https://usace.github.io/groundwork/"
: "http://localhost:5173/";
const base = mode === "production" ? "/groundwork/" : "/";
return {
plugins: [react()],
base: base,
Expand Down
Loading