Skip to content

Commit 5d9a3a9

Browse files
committed
fix: add url() helper for correct base path on GitHub Pages
Astro 4.0.2 does not auto-prefix href/src with base path. Add url() helper in config.ts and use it across all components, layouts, and pages so paths work correctly under /ac-site-template/.
1 parent 98cc914 commit 5d9a3a9

13 files changed

Lines changed: 57 additions & 38 deletions

src/components/BaseHead.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import "../styles/global.css";
55
import "@fontsource-variable/nunito";
66
import "lxgw-wenkai-webfont/style.css";
7+
import { url } from "../config";
78
89
export interface Props {
910
title: string;
@@ -24,7 +25,7 @@ const {
2425
<meta charset="utf-8" />
2526
<meta name="viewport" content="width=device-width,initial-scale=1" />
2627
<!-- TODO change color according to theme -->
27-
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
28+
<link rel="icon" type="image/svg+xml" href={url('favicon.svg')} />
2829
<meta name="generator" content={Astro.generator} />
2930

3031
<!-- Primary Meta Tags -->

src/components/Header.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
import { SITE_TITLE } from "../config";
2+
import { SITE_TITLE, url } from "../config";
33
---
44
<div
55
class="sticky lg:hidden top-0 z-30 flex h-16 w-full justify-center bg-opacity-90 backdrop-blur transition-all duration-100 bg-base-100 text-base-content shadow-ac rounded-b-ac-lg"
@@ -14,7 +14,7 @@ import { SITE_TITLE } from "../config";
1414
</div>
1515
<div class="navbar-center">
1616
<!-- Header on mobile -->
17-
<a class="btn btn-ghost normal-case text-xl" href="/">{SITE_TITLE}</a>
17+
<a class="btn btn-ghost normal-case text-xl" href={url('')}>{SITE_TITLE}</a>
1818
</div>
1919
<div class="navbar-end"></div>
2020
</div>

src/components/SideBar.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import SideBarFooter from "./SideBarFooter.astro";
33
import { Image } from "astro:assets";
44
import SideBarMenu from "./SideBarMenu.astro";
5+
import { url } from "../config";
56
const { sideBarActiveItemID } = Astro.props;
67
---
78

@@ -11,7 +12,7 @@ const { sideBarActiveItemID } = Astro.props;
1112
class="px-2 pt-2 h-auto min-h-full w-[19rem] bg-ac-cream-dark text-ac-text flex flex-col"
1213
>
1314
<div class="w-fit mx-auto mt-5 mb-6">
14-
<a href="/">
15+
<a href={url('')}>
1516
<div
1617
class="avatar transition ease-in-out hover:scale-[102%] block m-auto"
1718
>
@@ -21,7 +22,7 @@ const { sideBarActiveItemID } = Astro.props;
2122
format="webp"
2223
width={300}
2324
height={300}
24-
src="/profile.jpg"
25+
src={url('profile.webp')}
2526
alt="Profile image"
2627
/>
2728
</div>

src/components/SideBarFooter.astro

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
import { url } from "../config";
3+
---
4+
15
<div
26
class="block sticky pointer-events-none bottom-10 bg-ac-cream-dark justify-center h-12 [mask-image:linear-gradient(transparent,#000000)]"
37
>
@@ -11,7 +15,7 @@
1115
aria-label="Toggle theme"
1216
title="Toggle theme"
1317
>
14-
<img id="theme-icon" src="/leaf_light.png" alt="" class="w-5 h-5 rounded-ac-sm" />
18+
<img id="theme-icon" src={url('leaf_light.png')} alt="" class="w-5 h-5 rounded-ac-sm" />
1519
<span id="theme-label">Dark Mode</span>
1620
</button>
1721
</div>
@@ -26,7 +30,7 @@
2630
aria-label="GitHub"
2731
title="GitHub"
2832
>
29-
<img src="/news.png" alt="GitHub" class="w-5 h-5 rounded-ac-sm" />
33+
<img src={url('news.png')} alt="GitHub" class="w-5 h-5 rounded-ac-sm" />
3034
<span>GitHub</span>
3135
</a>
3236

@@ -37,7 +41,7 @@
3741
aria-label="LinkedIn"
3842
title="LinkedIn"
3943
>
40-
<img src="/brewster.png" alt="LinkedIn" class="w-5 h-5 rounded-ac-sm" />
44+
<img src={url('brewster.png')} alt="LinkedIn" class="w-5 h-5 rounded-ac-sm" />
4145
<span>LinkedIn</span>
4246
</a>
4347

@@ -48,7 +52,7 @@
4852
aria-label="WhatsApp"
4953
title="WhatsApp"
5054
>
51-
<img src="/whatsapp.png" alt="WhatsApp" class="w-5 h-5 rounded-ac-sm" />
55+
<img src={url('whatsapp.png')} alt="WhatsApp" class="w-5 h-5 rounded-ac-sm" />
5256
<span>WhatsApp</span>
5357
</a>
5458

@@ -59,7 +63,7 @@
5963
aria-label="Instagram"
6064
title="Instagram"
6165
>
62-
<img src="/instagram.png" alt="Instagram" class="w-5 h-5 rounded-ac-sm" />
66+
<img src={url('instagram.png')} alt="Instagram" class="w-5 h-5 rounded-ac-sm" />
6367
<span>Instagram</span>
6468
</a>
6569
</div>

src/components/SideBarMenu.astro

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
import { url } from "../config";
23
const { sideBarActiveItemID } = Astro.props;
34
const activeClass = "bg-ac-primary/15 text-ac-primary-dark rounded-ac-md";
45
---
@@ -8,59 +9,59 @@ const activeClass = "bg-ac-primary/15 text-ac-primary-dark rounded-ac-md";
89
<a
910
class="py-3 text-base rounded-ac-md transition-colors hover:bg-ac-sand"
1011
id="home"
11-
href="/"
12+
href={url('')}
1213
>
13-
<img src="/nookmiles.png" alt="" class="w-6 h-6 rounded-ac-sm" />
14+
<img src={url('nookmiles.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
1415
Home
1516
</a>
1617
</li>
1718
<li>
1819
<a
1920
class="py-3 text-base rounded-ac-md transition-colors hover:bg-ac-sand"
2021
id="projects"
21-
href="/projects"
22+
href={url('projects')}
2223
>
23-
<img src="/brush.png" alt="" class="w-6 h-6 rounded-ac-sm" />
24+
<img src={url('brush.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
2425
Projects
2526
</a>
2627
</li>
2728
<li>
2829
<a
2930
class="py-3 text-base rounded-ac-md transition-colors hover:bg-ac-sand"
3031
id="cv"
31-
href="/cv"
32+
href={url('cv')}
3233
>
33-
<img src="/nookwallet.png" alt="" class="w-6 h-6 rounded-ac-sm" />
34+
<img src={url('nookwallet.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
3435
CV
3536
</a>
3637
</li>
3738
<li>
3839
<a
3940
class="py-3 text-base rounded-ac-md transition-colors hover:bg-ac-sand"
4041
id="blog"
41-
href="/blog/"
42+
href={url('blog/')}
4243
>
43-
<img src="/photos.png" alt="" class="w-6 h-6 rounded-ac-sm" />
44+
<img src={url('photos.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
4445
Blog
4546
</a>
4647
</li>
4748
<li>
4849
<a
4950
class="py-3 text-base rounded-ac-md transition-colors hover:bg-ac-sand"
5051
id="personal"
51-
href="/personal"
52+
href={url('personal')}
5253
>
53-
<img src="/nookcritters.png" alt="" class="w-6 h-6 rounded-ac-sm" />
54+
<img src={url('nookcritters.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
5455
Personal
5556
</a>
5657
</li>
5758
<li>
5859
<a
5960
class="py-3 text-base rounded-ac-md transition-colors hover:bg-ac-sand"
6061
id="notes"
61-
href="/notes"
62+
href={url('notes')}
6263
>
63-
<img src="/drawing.png" alt="" class="w-6 h-6 rounded-ac-sm" />
64+
<img src={url('drawing.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
6465
Notes
6566
</a>
6667
</li>
@@ -72,7 +73,7 @@ const activeClass = "bg-ac-primary/15 text-ac-primary-dark rounded-ac-md";
7273
target="_blank"
7374
referrerpolicy="no-referrer-when-downgrade"
7475
>
75-
<img src="/messages.png" alt="" class="w-6 h-6 rounded-ac-sm" />
76+
<img src={url('messages.png')} alt="" class="w-6 h-6 rounded-ac-sm" />
7677
Contact
7778
</a>
7879
</li>

src/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ export const SITE_DESCRIPTION =
88
"Your Name's personal website.";
99
export const GENERATE_SLUG_FROM_TITLE = true;
1010
export const TRANSITION_API = true;
11+
12+
// Base path helper for GitHub Pages project sites
13+
// In dev: "/" — in prod with BASE_URL: "/ac-site-template/"
14+
const BASE_PATH = import.meta.env.BASE_URL;
15+
export const url = (path: string) => {
16+
const clean = path.replace(/^\//, "");
17+
return clean ? BASE_PATH + clean : BASE_PATH;
18+
};
19+
export { BASE_PATH };

src/layouts/BaseLayout.astro

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Footer from "../components/Footer.astro";
55
import SideBar from "../components/SideBar.astro";
66
import { ViewTransitions } from "astro:transitions";
77
8-
import { SITE_TITLE, SITE_DESCRIPTION, TRANSITION_API } from "../config";
8+
import { SITE_TITLE, SITE_DESCRIPTION, TRANSITION_API, url, BASE_PATH } from "../config";
99
1010
const {
1111
image,
@@ -28,6 +28,7 @@ const {
2828
ogType={ogType}
2929
/>
3030
{TRANSITION_API && <ViewTransitions />}
31+
<Fragment set:html={`<style is:inline>:root{--divider-img:url("${url('divider_line.png')}")}</style>`} />
3132
<script is:inline>
3233
(function(){
3334
var t = localStorage.getItem('theme');
@@ -41,7 +42,7 @@ const {
4142
<input id="my-drawer" type="checkbox" class="drawer-toggle" />
4243
<div class="drawer-content bg-base-100 rounded-tl-ac-xl relative min-h-screen flex flex-col">
4344
<div class="w-full overflow-hidden pointer-events-none select-none">
44-
<img src="/header_banner_light.png" data-light-src="/header_banner_light.png" data-dark-src="/header_banner_dark.png" alt="" class="w-full theme-banner" />
45+
<img src={url('header_banner_light.png')} data-light-src={url('header_banner_light.png')} data-dark-src={url('header_banner_dark.png')} alt="" class="w-full theme-banner" />
4546
</div>
4647
<Header title={SITE_TITLE} />
4748
<div class="md:flex md:justify-center flex-1">
@@ -52,7 +53,7 @@ const {
5253
<Footer />
5354

5455
<div class="w-full overflow-hidden pointer-events-none select-none flex justify-end mt-auto">
55-
<img src="/banner_light.png" data-light-src="/banner_light.png" data-dark-src="/banner_dark.png" alt="" class="w-full theme-banner" />
56+
<img src={url('banner_light.png')} data-light-src={url('banner_light.png')} data-dark-src={url('banner_dark.png')} alt="" class="w-full theme-banner" />
5657
</div>
5758
</div>
5859
{
@@ -62,6 +63,7 @@ const {
6263
}
6364
</div>
6465
<script is:inline>
66+
var __BASE__ = `${BASE_PATH}`;
6567
function applyTheme(theme) {
6668
document.documentElement.setAttribute('data-theme', theme);
6769
localStorage.setItem('theme', theme);
@@ -70,7 +72,7 @@ const {
7072
});
7173
var icon = document.getElementById('theme-icon');
7274
var label = document.getElementById('theme-label');
73-
if (icon) icon.src = theme === 'night' ? '/leaf_light.png' : '/leaf_dark.png';
75+
if (icon) icon.src = theme === 'night' ? __BASE__ + 'leaf_light.png' : __BASE__ + 'leaf_dark.png';
7476
if (label) label.textContent = theme === 'night' ? 'Light Mode' : 'Dark Mode';
7577
}
7678
window.toggleTheme = function() {

src/layouts/PostLayout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const {
1818
dayjs.extend(localizedFormat);
1919
const displayDate = dayjs(pubDate).format("ll");
2020
import { Image } from "astro:assets";
21-
import { AUTHOR_INITIAL } from "../config";
21+
import { AUTHOR_INITIAL, url } from "../config";
2222
---
2323

2424
<BaseLayout

src/pages/404.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
---
22
import BaseLayout from "../layouts/BaseLayout.astro";
3+
import { url } from "../config";
34
---
45

56
<BaseLayout title="404: Not Found" includeSidebar={false}>
67
<div class="text-center">
78
<h1 class="text-8xl font-bold mb-4">☁️</h1>
89
<h1 class="text-6xl font-bold mb-2">404</h1>
910
<h3 class="text-2xl">Oops, page not found ...</h3>
10-
<a class="btn-ac mt-9" href="/">Home</a>
11+
<a class="btn-ac mt-9" href={url('')}>Home</a>
1112
</div>
1213
</BaseLayout>

src/pages/index.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
import BaseLayout from "../layouts/BaseLayout.astro";
3-
import { SITE_TITLE } from "../config";
3+
import { SITE_TITLE, url } from "../config";
44
---
55

66
<BaseLayout sideBarActiveItemID="home">
77
<div class="pb-12 mt-5">
8-
<img src="/favicon.svg" alt="" class="w-24 h-24 mb-2" />
8+
<img src={url('favicon.svg')} alt="" class="w-24 h-24 mb-2" />
99
<div class="text-xl py-1">Hey there</div>
1010
<div class="text-5xl font-bold">I'm {SITE_TITLE}</div>
1111
<div class="text-xl py-6">

0 commit comments

Comments
 (0)