Skip to content

Commit

Permalink
improve redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Aug 16, 2024
1 parent b3679f0 commit f015290
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
11 changes: 7 additions & 4 deletions frontend/public/404.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<!doctype html>
<html>
<body>
<script type="text/javascript">
// redirect to root and pass page to router
console.debug("Redirecting from:", window.location);
const { origin, pathname, search, hash } = window.location;
window.sessionStorage.redirect = pathname + search + hash;
window.location = "/";
console.debug("Redirecting from:", location);
console.debug("With state:", history.state);
const { origin, pathname, search, hash } = location;
sessionStorage.redirectPath = pathname + search + hash;
sessionStorage.redirectState = JSON.stringify(history.state);
location = "/";
</script>
</body>
</html>
15 changes: 10 additions & 5 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useMatches,
useRouteLoaderData,
} from "react-router-dom";
import { isEmpty } from "lodash";
import Footer from "@/components/Footer";
import Header from "@/components/Header";
import TableOfContents from "@/components/TableOfContents";
Expand Down Expand Up @@ -77,11 +78,15 @@ const routes = [
element: <Home />,
loader: async () => {
/** handle 404 redirect */
const url = window.sessionStorage.redirect as string;
if (url) {
console.debug("Redirecting to:", url);
window.sessionStorage.removeItem("redirect");
return redirect(url);
const path = window.sessionStorage.redirectPath || "";
const state = JSON.parse(window.sessionStorage.redirectState || "{}");
if (!isEmpty(state)) window.history.replaceState(state, "");
if (path) {
console.debug("Redirecting to:", path);
console.debug("With state:", state);
window.sessionStorage.removeItem("redirectPath");
window.sessionStorage.removeItem("redirectState");
return redirect(path);
} else return null;
},
},
Expand Down

0 comments on commit f015290

Please sign in to comment.