Skip to content

Commit 44d45f5

Browse files
committed
fix(web): work around TanStack Router hash history index route bug
TanStack Router with hash history doesn't match the index route for root path "/" - it falls through to _not-found. This is a known issue: TanStack/router#5528 Workaround: - Add redirect script in index.html to ensure "/" redirects to "/#/" - Detect root path in _not-found.tsx and render HomePage instead of 404 - Simplify index.tsx lazy import pattern for consistency
1 parent 9fd23af commit 44d45f5

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

apps/web/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
<meta property="twitter:title" content="BibGraph - Explore Academic Research" />
2121
<meta property="twitter:description" content="Explore academic research data from OpenAlex, visualize citation networks, and discover research relationships across works, authors, institutions, and more." />
2222

23+
<script>
24+
// Redirect to hash-based home route if no hash is present
25+
// This ensures users landing on "/" get properly routed to "/#/"
26+
(function() {
27+
if (!window.location.hash || window.location.hash === '#') {
28+
window.location.replace(window.location.pathname + '#/');
29+
}
30+
})();
31+
</script>
2332
<script>
2433
// Prevent flash of wrong theme by setting color scheme before React loads
2534
(function() {

apps/web/src/routes/_not-found.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { Alert, Button,Container, Group, Stack, Text, Title } from "@mantine/core";
22
import { IconHome, IconSearch } from "@tabler/icons-react";
3-
import { createFileRoute } from "@tanstack/react-router";
3+
import { createFileRoute, useLocation } from "@tanstack/react-router";
4+
import { lazy, Suspense } from "react";
45

56
import { ICON_SIZE } from "@/config/style-constants";
67

8+
// Lazy load HomePage for when we need to render it as fallback for root path
9+
// This works around a TanStack Router bug where the index route isn't matched
10+
const HomePage = lazy(() => import("./index.lazy"));
11+
712
const NotFoundRoute = () => {
13+
const location = useLocation();
14+
15+
// Workaround for TanStack Router bug: index route not matching root path
16+
// See: https://github.com/TanStack/router/issues/5528
17+
if (location.pathname === "/") {
18+
console.log("[NOT FOUND ROUTE] Root path detected, rendering HomePage as workaround");
19+
return (
20+
<Suspense fallback={<div style={{ padding: "40px", textAlign: "center" }}>Loading...</div>}>
21+
<HomePage />
22+
</Suspense>
23+
);
24+
}
25+
26+
console.log("[NOT FOUND ROUTE] Rendering 404 component");
827
return (
928
<Container size="md" py="xl">
1029
<Stack gap="md" align="center">

apps/web/src/routes/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import { lazy } from "react";
33

44
import { LazyRoute } from "@/components/routing/LazyRoute";
55

6-
const IndexRoute = lazy(() =>
7-
import("./index.lazy").then((m) => ({ default: m.default })),
8-
);
6+
const HomePage = lazy(() => import("./index.lazy"));
97

108
export const Route = createFileRoute("/")({
119
component: () => (
1210
<LazyRoute>
13-
<IndexRoute />
11+
<HomePage />
1412
</LazyRoute>
1513
),
1614
});

0 commit comments

Comments
 (0)