Skip to content

Commit 3cf67c1

Browse files
authored
Update 404.html
1 parent d6a5cd2 commit 3cf67c1

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

public/404.html

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@
44
<head>
55
<meta charset="utf-8">
66
<title>Single Page Apps for GitHub Pages</title>
7-
<script type="text/javascript">
8-
// Single Page Apps for GitHub Pages
9-
// MIT License
10-
// https://github.com/rafgraph/spa-github-pages
11-
// This script checks to see if a redirect is present in the query string,
12-
// converts it back into the correct url and adds it to the
13-
// browser's history using window.history.replaceState(...),
14-
// which won't cause the browser to attempt to load the new url.
15-
// When the single page app is loaded further down in this file,
16-
// the correct url will be waiting in the browser's history for
17-
// the single page app to route accordingly.
18-
(function(l) {
19-
if (l.search[1] === '/' ) {
20-
var decoded = l.search.slice(1).split('&').map(function(s) {
21-
return s.replace(/~and~/g, '&')
22-
}).join('?');
23-
window.history.replaceState(null, null,
24-
l.pathname.slice(0, -1) + decoded + l.hash
25-
);
26-
}
27-
}(window.location))
28-
</script>
7+
<script type="text/javascript">
8+
// Single Page Apps for GitHub Pages
9+
// MIT License
10+
// https://github.com/rafgraph/spa-github-pages
11+
(function(l) {
12+
if (l.search[1] === '/') {
13+
var decoded = l.search.slice(1).split('&').map(function(s) {
14+
return s.replace(/~and~/g, '&');
15+
}).join('?');
16+
17+
// Construct the new URL using the current origin
18+
var newUrl = l.origin + l.pathname.slice(0, -1) + decoded + l.hash;
19+
20+
try {
21+
var newUrlObj = new URL(newUrl);
22+
// Only allow update if the new URL matches the current origin
23+
if (newUrlObj.origin === window.location.origin) {
24+
window.history.replaceState(null, null, newUrlObj.href);
25+
}
26+
} catch (e) {
27+
console.warn('Invalid redirect URL', e);
28+
}
29+
}
30+
}(window.location));
31+
</script>
32+
2933
</head>
3034
<body>
3135
<script type="text/javascript">

0 commit comments

Comments
 (0)