Skip to content

Commit 6c6ff1d

Browse files
committed
fix: Improve 404 page redirection logic and update SPA route handling
1 parent 9c731c2 commit 6c6ff1d

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

demo/public/404.html

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
<head>
44
<meta charset="utf-8" />
55
<script>
6-
// Redirect to index.html and keep the original path
7-
sessionStorage.redirect = location.pathname + location.search;
8-
location.href = location.origin + location.pathname.replace(/\/[^\/]*$/, '/') + 'index.html';
6+
const pathname = location.pathname;
7+
const hasExtension = /\.[^\/]+$/.test(pathname);
8+
9+
// Only redirect SPA routes (no extension)
10+
if (!hasExtension) {
11+
const redirectUrl = encodeURIComponent(pathname + location.search);
12+
13+
// Redirect exactly once into SPA entry
14+
location.replace("/react-hook-form-ai/index.html?redirect=" + redirectUrl);
15+
}
916
</script>
17+
<title>404</title>
1018
</head>
11-
<body></body>
19+
<body>
20+
<h1>404</h1>
21+
</body>
1222
</html>

demo/src/main.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { BrowserRouter } from "react-router-dom";
44
import App from "./App";
55
import "./index.css";
66

7-
// Restore intended SPA route after GitHub Pages redirect fallback
8-
if (sessionStorage.redirect) {
9-
const redirectUrl = sessionStorage.redirect as string;
10-
delete sessionStorage.redirect;
11-
history.replaceState(null, "", redirectUrl);
7+
// Read redirect query param
8+
const params = new URLSearchParams(location.search);
9+
const redirect = params.get("redirect");
10+
11+
if (redirect) {
12+
history.replaceState(null, "", redirect);
1213
}
1314

1415
ReactDOM.render(

0 commit comments

Comments
 (0)