Skip to content

Commit f99009d

Browse files
authored
[Docs Site] Support paths with and without trailing slash in local dev (cloudflare#19596)
* [Docs Site] Add trailingSlash: always to Astro config * Support paths with and without trailing slash in local dev
1 parent 0e72bf5 commit f99009d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/components/overrides/Sidebar.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Default from "@astrojs/starlight/components/Sidebar.astro";
55
import { Icon as AstroIcon } from "astro-icon/components";
66
import { lookupProductTitle } from "~/util/sidebar";
77
8-
const [product, module] = Astro.url.pathname.split("/").slice(1, -1);
8+
const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
99
---
1010

1111
<a

src/util/sidebar.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const sidebars = new Map<string, Group>();
1616

1717
export async function getSidebar(context: AstroGlobal<Props>) {
1818
const pathname = context.url.pathname;
19-
const segments = pathname.split("/").slice(1, -1);
19+
const segments = pathname.split("/").filter(Boolean);
2020

2121
const product = segments.at(0);
2222

@@ -96,9 +96,14 @@ function setSidebarCurrentEntry(
9696
pathname: string,
9797
): boolean {
9898
for (const entry of sidebar) {
99-
if (entry.type === "link" && entry.href === pathname) {
100-
entry.isCurrent = true;
101-
return true;
99+
if (entry.type === "link") {
100+
const href = entry.href;
101+
102+
// Compare with and without trailing slash
103+
if (href === pathname || href.slice(0, -1) === href) {
104+
entry.isCurrent = true;
105+
return true;
106+
}
102107
}
103108

104109
if (

0 commit comments

Comments
 (0)