Skip to content

Commit 22040e3

Browse files
committed
Add support for url paths
1 parent 3e7dd4c commit 22040e3

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

404.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Single Page Apps for GitHub Pages</title>
6+
<script type="text/javascript">
7+
// Single Page Apps for GitHub Pages
8+
// MIT License
9+
// https://github.com/rafgraph/spa-github-pages
10+
// This script takes the current url and converts the path and query
11+
// string into just a query string, and then redirects the browser
12+
// to the new url with only a query string and hash fragment,
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
15+
// Note: this 404.html file must be at least 512 bytes for it to work
16+
// with Internet Explorer (it is currently > 512 bytes)
17+
18+
// If you're creating a Project Pages site and NOT using a custom domain,
19+
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
20+
// This way the code will only replace the route part of the path, and not
21+
// the real directory in which the app resides, for example:
22+
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
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+
27+
var l = window.location;
28+
l.replace(
29+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
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~') : '') +
33+
l.hash
34+
);
35+
</script>
36+
</head>
37+
<body>
38+
</body>
39+
</html>

index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,27 @@
2323
<body style="margin: 0">
2424
<div id="root"></div>
2525
<script type="module" src="/src/main.tsx"></script>
26+
<script type="text/javascript">
27+
// Single Page Apps for GitHub Pages
28+
// MIT License
29+
// https://github.com/rafgraph/spa-github-pages
30+
// This script checks to see if a redirect is present in the query string,
31+
// converts it back into the correct url and adds it to the
32+
// browser's history using window.history.replaceState(...),
33+
// which won't cause the browser to attempt to load the new url.
34+
// When the single page app is loaded further down in this file,
35+
// the correct url will be waiting in the browser's history for
36+
// the single page app to route accordingly.
37+
(function(l) {
38+
if (l.search[1] === '/' ) {
39+
var decoded = l.search.slice(1).split('&').map(function(s) {
40+
return s.replace(/~and~/g, '&')
41+
}).join('?');
42+
window.history.replaceState(null, null,
43+
l.pathname.slice(0, -1) + decoded + l.hash
44+
);
45+
}
46+
}(window.location))
47+
</script>
2648
</body>
2749
</html>

vite.config.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { defineConfig, loadEnv } from 'vite'
22
import react from '@vitejs/plugin-react'
3+
import { resolve } from 'path'
4+
import { copyFileSync } from 'fs'
35

46
export default defineConfig(({ command, mode }) => {
57
const env = loadEnv(mode, process.cwd(), '')
68
return {
79
base: env.VITE_BASE_URL,
8-
plugins: [react()],
10+
plugins: [
11+
react(),
12+
{
13+
name: 'copy-404',
14+
closeBundle() {
15+
copyFileSync(resolve('404.html'), resolve('dist/404.html'));
16+
},
17+
},
18+
],
919
}
1020
})

0 commit comments

Comments
 (0)