Skip to content

Commit f1b88fa

Browse files
authored
Restore SPA implementation (#980)
1 parent 3bb0896 commit f1b88fa

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

tests/dummy/app/index.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@
1313
<link integrity="" rel="stylesheet" href="{{rootURL}}assets/vendor.css">
1414
<link integrity="" rel="stylesheet" href="{{rootURL}}assets/dummy.css">
1515

16+
<!-- Start Single Page Apps for GitHub Pages -->
17+
<script type="text/javascript">
18+
// Single Page Apps for GitHub Pages
19+
// MIT License
20+
// https://github.com/rafgraph/spa-github-pages
21+
// This script checks to see if a redirect is present in the query string,
22+
// converts it back into the correct url and adds it to the
23+
// browser's history using window.history.replaceState(...),
24+
// which won't cause the browser to attempt to load the new url.
25+
// When the single page app is loaded further down in this file,
26+
// the correct url will be waiting in the browser's history for
27+
// the single page app to route accordingly.
28+
(function(l) {
29+
if (l.search[1] === '/' ) {
30+
var decoded = l.search.slice(1).split('&').map(function(s) {
31+
return s.replace(/~and~/g, '&')
32+
}).join('?');
33+
window.history.replaceState(null, null,
34+
l.pathname.slice(0, -1) + decoded + l.hash
35+
);
36+
}
37+
}(window.location))
38+
</script>
39+
<!-- End Single Page Apps for GitHub Pages -->
40+
1641
{{content-for "head-footer"}}
1742
</head>
1843
<body>

tests/dummy/public/404.html

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@
55
<title>Single Page Apps for GitHub Pages</title>
66
<script type="text/javascript">
77
// Single Page Apps for GitHub Pages
8-
// https://github.com/rafrex/spa-github-pages
9-
// Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
10-
// ----------------------------------------------------------------------
8+
// MIT License
9+
// https://github.com/rafgraph/spa-github-pages
1110
// This script takes the current url and converts the path and query
1211
// string into just a query string, and then redirects the browser
1312
// to the new url with only a query string and hash fragment,
14-
// e.g. http://www.foo.tld/one/two?a=b&c=d#qwe, becomes
15-
// http://www.foo.tld/?p=/one/two&q=a=b~and~c=d#qwe
13+
// e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
14+
// https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
1615
// Note: this 404.html file must be at least 512 bytes for it to work
1716
// with Internet Explorer (it is currently > 512 bytes)
17+
1818
// If you're creating a Project Pages site and NOT using a custom domain,
19-
// then set segmentCount to 1 (enterprise users may need to set it to > 1).
19+
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
2020
// This way the code will only replace the route part of the path, and not
2121
// the real directory in which the app resides, for example:
2222
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
23-
// https://username.github.io/repo-name/?p=/one/two&q=a=b~and~c=d#qwe
24-
// Otherwise, leave segmentCount as 0.
25-
var segmentCount = 1;
23+
// https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
24+
// Otherwise, leave pathSegmentsToKeep as 0.
25+
var pathSegmentsToKeep = 0;
26+
2627
var l = window.location;
2728
l.replace(
2829
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
29-
l.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=/' +
30-
l.pathname.slice(1).split('/').slice(segmentCount).join('/').replace(/&/g, '~and~') +
31-
(l.search ? '&q=' + l.search.slice(1).replace(/&/g, '~and~') : '') +
30+
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
31+
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
32+
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
3233
l.hash
3334
);
35+
3436
</script>
3537
</head>
3638
<body>

0 commit comments

Comments
 (0)