Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit e3cf41d

Browse files
committed
improve backend redirection
1 parent 4291b51 commit e3cf41d

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useHistory, useLocation } from "react-router-dom";
2+
3+
export default function BackendRedirect() {
4+
const location = useLocation();
5+
const history = useHistory();
6+
const urlSearchParams = new URLSearchParams(location.search);
7+
const redirected = urlSearchParams.get("redirected");
8+
9+
// this param will be visible for the user, even though it is only needed if the fe is not accessed via nginx, but it prevents endless loops
10+
if (redirected === "") {
11+
history.push("/error404");
12+
return null;
13+
}
14+
urlSearchParams.set("redirected", "");
15+
history.push(location.pathname + "?" + urlSearchParams.toString());
16+
window.location.reload(); // leave the spa, to access the apis.
17+
return null;
18+
}

src/components/Router/Router.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Registration from "../pages/User/Registration";
77
import FileSystem, { filesBaseUrl } from "../pages/filesytem/Filesystem";
88
import Profile from "../pages/User/Profile";
99
import Error400 from "../pages/errors/Error400";
10+
import BackendRedirect from "./BackendRedirect";
1011

1112
export default function Router(): ReactElement {
1213
return (
@@ -24,13 +25,7 @@ export default function Router(): ReactElement {
2425
<Route path={"/profile"} component={Profile} />
2526
<Route path={"/profile"} component={Profile} />
2627
<Route path={"/error"} component={Error400} />
27-
<Route
28-
path={["/data", "/api"]}
29-
component={() => {
30-
window.location.reload(); // leave the spa, to access the apis. This could cause a endless loop if the nginx is not present
31-
return null;
32-
}}
33-
/>
28+
<Route path={["/data", "/api"]} component={BackendRedirect} />
3429
<Route path={"*"} component={Error404} />
3530
</Switch>
3631
);

0 commit comments

Comments
 (0)