Skip to content

Commit 932d812

Browse files
committed
Add gh-pages SPA fix
1 parent df25189 commit 932d812

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@
88
name="description"
99
content="The latest industry news, interviews, technologies, and resources."
1010
/>
11+
<script type="text/javascript">
12+
// Single Page Apps for GitHub Pages
13+
// MIT License
14+
// https://github.com/rafgraph/spa-github-pages
15+
// This script checks to see if a redirect is present in the query string,
16+
// converts it back into the correct url and adds it to the
17+
// browser's history using window.history.replaceState(...),
18+
// which won't cause the browser to attempt to load the new url.
19+
// When the single page app is loaded further down in this file,
20+
// the correct url will be waiting in the browser's history for
21+
// the single page app to route accordingly.
22+
(function (l) {
23+
if (l.search[1] === "/") {
24+
var decoded = l.search
25+
.slice(1)
26+
.split("&")
27+
.map(function (s) {
28+
return s.replace(/~and~/g, "&");
29+
})
30+
.join("?");
31+
window.history.replaceState(
32+
null,
33+
null,
34+
l.pathname.slice(0, -1) + decoded + l.hash,
35+
);
36+
}
37+
})(window.location);
38+
</script>
1139
<title>Blog</title>
1240
</head>
1341
<body>

public/404.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 = 1;
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
45+
? "&" + l.search.slice(1).replace(/&/g, "~and~")
46+
: "") +
47+
l.hash,
48+
);
49+
</script>
50+
</head>
51+
<body></body>
52+
</html>

0 commit comments

Comments
 (0)