Skip to content

Commit 1f40a76

Browse files
committed
fix: update app Link component to support base url
1 parent da7a101 commit 1f40a76

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

renderer/Link.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,37 @@ import { usePageContext } from "./usePageContext";
33

44
export { Link };
55

6-
function Link(props: {
6+
function Link({
7+
href,
8+
className,
9+
children,
10+
}: {
711
href?: string;
812
className?: string;
913
children: React.ReactNode;
1014
}) {
15+
let base = import.meta.env.BASE_URL;
16+
let processedHref = href;
17+
if (base) {
18+
if (!href?.startsWith("/")) {
19+
throw new Error("Link href should start with /");
20+
}
21+
if (base.endsWith("/")) {
22+
base = base.slice(0, -1);
23+
}
24+
processedHref = `${base}${href}`;
25+
}
26+
1127
const pageContext = usePageContext();
12-
const className = [
13-
props.className,
14-
pageContext.urlPathname === props.href && "is-active",
28+
const completeClassName = [
29+
className,
30+
pageContext.urlPathname === href && "is-active",
1531
]
1632
.filter(Boolean)
1733
.join(" ");
18-
return <a {...props} className={className} />;
34+
return (
35+
<a href={processedHref} className={completeClassName}>
36+
{children}
37+
</a>
38+
);
1939
}

0 commit comments

Comments
 (0)