Skip to content

Commit d458ed5

Browse files
committed
feat: implement GitHub Pages 404 redirect handling in ApplyQueryParams component
1 parent 1b4d4eb commit d458ed5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

public/404.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Redirecting</title>
6+
<script type="text/javascript">
7+
// Single Page Apps for GitHub Pages
8+
// Redirect to index.html with path preserved
9+
var pathSegmentsToKeep = 0;
10+
var l = window.location;
11+
l.replace(
12+
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
13+
'/index.html?p=/' + l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
14+
(l.search ? '&q=' + l.search.slice(1).replace(/&/g, '~and~') : '') +
15+
l.hash
16+
);
17+
</script>
18+
</head>
19+
<body>
20+
</body>
21+
</html>

src/processes/ApplyQueryParams.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import { FunctionComponent, PropsWithChildren, useEffect } from 'react';
2-
import { useSearchParams } from 'react-router-dom';
2+
import { useSearchParams, useNavigate } from 'react-router-dom';
33
import { projectsStore } from 'src/shared/stores';
44
import { observer } from 'mobx-react-lite';
55

66
const ApplyQueryParams: FunctionComponent<PropsWithChildren> = ({ children }) => {
77
const [searchParams, setSearchParams] = useSearchParams();
8+
const navigate = useNavigate();
89

910
useEffect(() => {
11+
// Handle GitHub Pages 404 redirect for SPA routing
12+
const pathParam = searchParams.get('p');
13+
if (pathParam !== null) {
14+
// Decode the saved path and navigate to it
15+
const decodedPath = pathParam.replace(/~and~/g, '&');
16+
searchParams.delete('p');
17+
setSearchParams(searchParams);
18+
navigate(decodedPath);
19+
return;
20+
}
21+
1022
const projectId = searchParams.get('project_id');
1123
if (projectId !== null) {
1224
searchParams.delete('project_id');

0 commit comments

Comments
 (0)