Skip to content

Commit d14e5c0

Browse files
haojianclaude
andcommitted
Fix SPA routing on GitHub Pages for /people and other routes
Add 404.html redirect and index.html URL restoration script so direct navigation to client-side routes works on GitHub Pages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d10bbaf commit d14e5c0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
</head>
1919

2020
<body>
21+
<!-- GitHub Pages SPA redirect: restores URL from 404.html query string -->
22+
<script>
23+
(function(l) {
24+
if (l.search[1] === '/') {
25+
var decoded = l.search.slice(1).split('&').map(function(s) {
26+
return s.replace(/~and~/g, '&')
27+
}).join('?');
28+
window.history.replaceState(null, null, l.pathname.slice(0, -1) + decoded + l.hash);
29+
}
30+
}(window.location))
31+
</script>
2132
<div id="root"></div>
2233
<script type="module" src="/src/main.tsx"></script>
2334
</body>

public/404.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>AI Smith Lab</title>
6+
<script>
7+
// Single Page App redirect for GitHub Pages
8+
// Converts the path into a query string so index.html can restore it
9+
var pathSegmentsToKeep = 0;
10+
var l = window.location;
11+
l.replace(
12+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
13+
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
14+
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
15+
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
16+
l.hash
17+
);
18+
</script>
19+
</head>
20+
<body>
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)