Skip to content

Commit e86c3f0

Browse files
authored
Add 404.html file to enable routing for GH Pages (#266)
1 parent b565ffb commit e86c3f0

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

404.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Single Page Apps for GitHub Pages</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#qwe, becomes
14+
// https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
15+
// Note: this 404.html file must be at least 512 bytes for it to work
16+
// with Internet Explorer (it is currently > 512 bytes)
17+
18+
// If you're creating a Project Pages site and NOT using a custom domain,
19+
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
20+
// This way the code will only replace the route part of the path, and not
21+
// the real directory in which the app resides, for example:
22+
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
23+
// https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
24+
// Otherwise, leave pathSegmentsToKeep as 0.
25+
var pathSegmentsToKeep = 0;
26+
27+
var l = window.location;
28+
l.replace(
29+
l.protocol +
30+
"//" +
31+
l.hostname +
32+
(l.port ? ":" + l.port : "") +
33+
l.pathname
34+
.split("/")
35+
.slice(0, 1 + pathSegmentsToKeep)
36+
.join("/") +
37+
"/?/" +
38+
l.pathname
39+
.slice(1)
40+
.split("/")
41+
.slice(pathSegmentsToKeep)
42+
.join("/")
43+
.replace(/&/g, "~and~") +
44+
(l.search ? "&" + l.search.slice(1).replace(/&/g, "~and~") : "") +
45+
l.hash
46+
);
47+
</script>
48+
</head>
49+
<body></body>
50+
</html>

index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@
2626
crossorigin="anonymous"
2727
/>
2828
<title>VenusHacks 2024</title>
29+
<!-- Start Single Page Apps for GitHub Pages -->
30+
<script type="text/javascript">
31+
// Single Page Apps for GitHub Pages
32+
// MIT License
33+
// https://github.com/rafgraph/spa-github-pages
34+
// This script checks to see if a redirect is present in the query string,
35+
// converts it back into the correct url and adds it to the
36+
// browser's history using window.history.replaceState(...),
37+
// which won't cause the browser to attempt to load the new url.
38+
// When the single page app is loaded further down in this file,
39+
// the correct url will be waiting in the browser's history for
40+
// the single page app to route accordingly.
41+
(function (l) {
42+
if (l.search[1] === "/") {
43+
var decoded = l.search
44+
.slice(1)
45+
.split("&")
46+
.map(function (s) {
47+
return s.replace(/~and~/g, "&");
48+
})
49+
.join("?");
50+
window.history.replaceState(
51+
null,
52+
null,
53+
l.pathname.slice(0, -1) + decoded + l.hash
54+
);
55+
}
56+
})(window.location);
57+
</script>
58+
<!-- End Single Page Apps for GitHub Pages -->
2959
</head>
3060
<body>
3161
<div id="root"></div>

0 commit comments

Comments
 (0)