Skip to content

Commit 2903b9b

Browse files
deniz.dundardeniz.dundar
authored andcommitted
Fix 404 issue: Add GitHub Pages SPA redirect for Angular routing
1 parent f5674ba commit 2903b9b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

95 Bytes
Binary file not shown.

public/404.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Ahmet Deniz Dündar - Frontend Developer</title>
7+
<script type="text/javascript">
8+
// GitHub Pages hack for single page applications
9+
// Redirect from 404 page to index.html with the route encoded in the query string
10+
var pathSegmentsToKeep = 0;
11+
var l = window.location;
12+
l.replace(
13+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
14+
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
15+
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
16+
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
17+
l.hash
18+
);
19+
20+
</script>
21+
</head>
22+
23+
<body>
24+
<!-- GitHub Pages 404 redirect for Single Page Applications -->
25+
</body>
26+
27+
</html>

src/index.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,32 @@
218218

219219
</style>
220220

221+
<!-- GitHub Pages Redirect Script for SPA -->
222+
<script type="text/javascript">
223+
// Decode URL parameters from 404.html redirect
224+
(function () {
225+
var redirect = sessionStorage.redirect;
226+
delete sessionStorage.redirect;
227+
if (redirect && redirect != location.href) {
228+
history.replaceState(null, null, redirect);
229+
}
230+
})();
231+
232+
// Check for redirect parameter in URL
233+
(function () {
234+
var l = window.location;
235+
if (l.search && l.search[1] === '/') {
236+
var decoded = decodeURIComponent(l.search.slice(1)).split('&').map(function (s) {
237+
return s.replace(/~and~/g, '&')
238+
}).join('?');
239+
window.history.replaceState(null, null,
240+
l.pathname.slice(0, -1) + decoded + l.hash
241+
);
242+
}
243+
})();
244+
245+
</script>
246+
221247
</head>
222248

223249
<body>

0 commit comments

Comments
 (0)