Skip to content

Commit c985f9a

Browse files
author
Joel Travieso
committed
feat(404): redirect 404s to home keeping the path for react routing
1 parent 7f8df9d commit c985f9a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

404.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>404 Redirect Page</title>
6+
<script type="text/javascript">
7+
// Source:
8+
// Single Page Apps for GitHub Pages
9+
// https://github.com/rafrex/spa-github-pages
10+
// Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
11+
// ----------------------------------------------------------------------
12+
// This script takes the current url and converts the path and query
13+
// string into just a query string, and then redirects the browser
14+
// to the new url with only a query string and hash fragment,
15+
// e.g. http://www.foo.tld/one/two?a=b&c=d#qwe, becomes
16+
// http://www.foo.tld/?p=/one/two&q=a=b~and~c=d#qwe
17+
// Note: this 404.html file must be at least 512 bytes for it to work
18+
// with Internet Explorer (it is currently > 512 bytes)
19+
// If you're creating a Project Pages site and NOT using a custom domain,
20+
// then set segmentCount to 1 (enterprise users may need to set it to > 1).
21+
// This way the code will only replace the route part of the path, and not
22+
// the real directory in which the app resides, for example:
23+
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
24+
// https://username.github.io/repo-name/?p=/one/two&q=a=b~and~c=d#qwe
25+
// Otherwise, leave segmentCount as 0.
26+
var segmentCount = 0;
27+
var l = window.location;
28+
l.replace(
29+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
30+
l.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=/' +
31+
l.pathname.slice(1).split('/').slice(segmentCount).join('/').replace(/&/g, '~and~') +
32+
(l.search ? '&q=' + l.search.slice(1).replace(/&/g, '~and~') : '') +
33+
l.hash
34+
);
35+
</script>
36+
</head>
37+
<body>
38+
</body>
39+
</html>

public/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@
88
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
99
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
1010
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
11+
<!-- Start Single Page Apps for GitHub Pages -->
12+
<script type="text/javascript">
13+
// Single Page Apps for GitHub Pages
14+
// https://github.com/rafrex/spa-github-pages
15+
// Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
16+
// ----------------------------------------------------------------------
17+
// This script checks to see if a redirect is present in the query string
18+
// and converts it back into the correct url and adds it to the
19+
// browser's history using window.history.replaceState(...),
20+
// which won't cause the browser to attempt to load the new url.
21+
// When the single page app is loaded further down in this file,
22+
// the correct url will be waiting in the browser's history for
23+
// the single page app to route accordingly.
24+
(function(l) {
25+
if (l.search) {
26+
var q = {};
27+
l.search.slice(1).split('&').forEach(function(v) {
28+
var a = v.split('=');
29+
q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&');
30+
});
31+
if (q.p !== undefined) {
32+
window.history.replaceState(null, null,
33+
l.pathname.slice(0, -1) + (q.p || '') +
34+
(q.q ? ('?' + q.q) : '') +
35+
l.hash
36+
);
37+
}
38+
}
39+
}(window.location))
40+
</script>
41+
<!-- End Single Page Apps for GitHub Pages -->
1142
</head>
1243
<body>
1344
<noscript>

0 commit comments

Comments
 (0)