Skip to content

Commit d6a5cd2

Browse files
authored
Update index.html
1 parent a127a50 commit d6a5cd2

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

index.html

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,28 @@
2020
// Single Page Apps for GitHub Pages
2121
// MIT License
2222
// https://github.com/rafgraph/spa-github-pages
23-
// This script checks to see if a redirect is present in the query string,
24-
// converts it back into the correct url and adds it to the
25-
// browser's history using window.history.replaceState(...),
26-
// which won't cause the browser to attempt to load the new url.
27-
// When the single page app is loaded further down in this file,
28-
// the correct url will be waiting in the browser's history for
29-
// the single page app to route accordingly.
3023
(function(l) {
31-
if (l.search[1] === '/' ) {
24+
if (l.search[1] === '/') {
3225
var decoded = l.search.slice(1).split('&').map(function(s) {
33-
return s.replace(/~and~/g, '&')
26+
return s.replace(/~and~/g, '&');
3427
}).join('?');
35-
window.history.replaceState(null, null,
36-
l.pathname.slice(0, -1) + decoded + l.hash
37-
);
28+
29+
// Construct the new URL using the current origin
30+
var newUrl = l.origin + l.pathname.slice(0, -1) + decoded + l.hash;
31+
32+
try {
33+
var newUrlObj = new URL(newUrl);
34+
// Only allow update if the new URL matches the current origin
35+
if (newUrlObj.origin === window.location.origin) {
36+
window.history.replaceState(null, null, newUrlObj.href);
37+
}
38+
} catch (e) {
39+
console.warn('Invalid redirect URL', e);
40+
}
3841
}
39-
}(window.location))
42+
}(window.location));
4043
</script>
44+
4145
<!-- End Single Page Apps for GitHub Pages -->
4246
</head>
4347

0 commit comments

Comments
 (0)