Skip to content

Commit 50fc7b3

Browse files
committed
Fix SPA routing for GitHub Pages
- Add basename to BrowserRouter using import.meta.env.BASE_URL - Add 404.html for GitHub Pages SPA redirect - Add redirect script to index.html
1 parent e5ecf69 commit 50fc7b3

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

unified-app/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>ML Animations - Interactive Machine Learning Visualizations</title>
88
<meta name="description" content="Interactive visualizations and animations for machine learning concepts including transformers, attention mechanisms, neural networks, and more." />
9+
<!-- Start Single Page Apps for GitHub Pages -->
10+
<script type="text/javascript">
11+
// Single Page Apps for GitHub Pages
12+
// MIT License
13+
// https://github.com/rafgraph/spa-github-pages
14+
(function(l) {
15+
if (l.search[1] === '/' ) {
16+
var decoded = l.search.slice(1).split('&').map(function(s) {
17+
return s.replace(/~and~/g, '&')
18+
}).join('?');
19+
window.history.replaceState(null, null,
20+
l.pathname.slice(0, -1) + decoded + l.hash
21+
);
22+
}
23+
}(window.location))
24+
</script>
25+
<!-- End Single Page Apps for GitHub Pages -->
926
</head>
1027
<body class="antialiased">
1128
<div id="root"></div>

unified-app/public/404.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>ML Animations</title>
6+
<script type="text/javascript">
7+
// Single Page Apps for GitHub Pages
8+
// MIT License
9+
// https://github.com/rafgraph/spa-github-pages
10+
// This script takes the current url and converts the path and query
11+
// string into just a query string, and then redirects the browser
12+
// to the new url with only a query string and hash fragment,
13+
// e.g. https://www.foo.tld/one/two?a=b&c=d#qber becomes
14+
// https://www.foo.tld/?/one/two&a=b~and~c=d#qber
15+
// Note: this 404.html file must be at least 512 bytes for it to work
16+
// with Internet Explorer (see this stackoverflow thread
17+
// http://stackoverflow.com/questions/7028563/ie-and-custom-404-pages)
18+
19+
var pathSegmentsToKeep = 1;
20+
21+
var l = window.location;
22+
l.replace(
23+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
24+
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
25+
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
26+
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
27+
l.hash
28+
);
29+
30+
</script>
31+
</head>
32+
<body>
33+
</body>
34+
</html>

unified-app/src/main.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import App from './App'
55
import { ThemeProvider } from './context/ThemeContext'
66
import './index.css'
77

8+
// Use base path for GitHub Pages deployment
9+
const basename = import.meta.env.BASE_URL
10+
811
ReactDOM.createRoot(document.getElementById('root')).render(
912
<React.StrictMode>
10-
<BrowserRouter>
13+
<BrowserRouter basename={basename}>
1114
<ThemeProvider>
1215
<App />
1316
</ThemeProvider>

0 commit comments

Comments
 (0)