-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathredirect.js
34 lines (32 loc) · 1.13 KB
/
redirect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Single Page Apps for GitHub Pages
// https://github.com/rafrex/spa-github-pages
// Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
let redirect404 = () => {
var segmentCount = 0;
var location = window.location;
location.replace(
location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') +
location.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=/' +
location.pathname.slice(1).split('/').slice(segmentCount).join('/').replace(/&/g, '~and~') +
(location.search ? '&q=' + location.search.slice(1).replace(/&/g, '~and~') : '') +
location.hash
);
}
let recieveRedirect = () => {
(function(location) {
if (location.search) {
var q = {};
location.search.slice(1).split('&').forEach(function(v) {
var a = v.split('=');
q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&');
});
if (q.p !== undefined) {
window.history.replaceState(null, null,
location.pathname.slice(0, -1) + (q.p || '') +
(q.q ? ('?' + q.q) : '') +
location.hash
);
}
}
}(window.location));
}