import { lazy } from "react"; import { createBrowserRouter, Navigate, type RouteObject } from "react-router-dom"; import App from "@/App"; import { ChunkLoadErrorFallback } from "@/components/ErrorBoundary"; import MainLayout from "@/layouts/MainLayout"; import RootLayout from "@/layouts/RootLayout"; import { LandingRoute, RequireAuthRoute, RequireGuestRoute } from "./guards"; import { ROUTES } from "./routes"; // Wrap lazy imports to auto-reload on chunk load failure (e.g., after redeployment). function lazyWithReload(factory: () => Promise<{ default: T }>) { return lazy(() => factory().catch((error) => { const isChunkError = error?.message?.includes("Failed to fetch dynamically imported module") || error?.message?.includes("Importing a module script failed"); const reloadKey = "chunk-reload"; if (isChunkError && !sessionStorage.getItem(reloadKey)) { sessionStorage.setItem(reloadKey, "1"); window.location.reload(); } throw error; }), ); } const AdminSignIn = lazyWithReload(() => import("@/pages/AdminSignIn")); const Archived = lazyWithReload(() => import("@/pages/Archived")); const AuthCallback = lazyWithReload(() => import("@/pages/AuthCallback")); const Explore = lazyWithReload(() => import("@/pages/Explore")); const Home = lazyWithReload(() => import("@/pages/Home")); const Inboxes = lazyWithReload(() => import("@/pages/Inboxes")); const MemoDetail = lazyWithReload(() => import("@/pages/MemoDetail")); const NotFound = lazyWithReload(() => import("@/pages/NotFound")); const PermissionDenied = lazyWithReload(() => import("@/pages/PermissionDenied")); const Attachments = lazyWithReload(() => import("@/pages/Attachments")); const Setting = lazyWithReload(() => import("@/pages/Setting")); const Shortcuts = lazyWithReload(() => import("@/pages/Shortcuts")); const SignIn = lazyWithReload(() => import("@/pages/SignIn")); const SignUp = lazyWithReload(() => import("@/pages/SignUp")); const UserProfile = lazyWithReload(() => import("@/pages/UserProfile")); // Backward compatibility alias. export const Routes = ROUTES; export { ROUTES }; /** * Static route configuration. Exported so tests can assert on the tree shape * (e.g. that `/auth/callback` stays outside the guest-only guard subtree) and * so integration tests can drive a `createMemoryRouter` over the same tree. */ export const routeConfig: RouteObject[] = [ { path: "/", element: , errorElement: , children: [ { path: Routes.AUTH, children: [ // The OAuth callback must run regardless of the current session — an // authenticated tab elsewhere must not block it from consuming its // one-time OAuth state. Keep it outside the guest-only subtree. { path: "callback", element: }, { element: , children: [ { path: "", element: }, { path: "admin", element: }, { path: "signup", element: }, ], }, ], }, // Backward compatibility: the old `/home` URL now lives at `/`. { path: "home", element: }, { element: , children: [ { element: , children: [ { element: , children: [{ index: true, element: }], }, { path: Routes.EXPLORE, element: }, { path: "u/:username", element: }, { element: , children: [ { path: Routes.ARCHIVED, element: }, { path: Routes.SHORTCUTS, element: }, ], }, ], }, { path: "memos/:uid", element: }, { path: "memos/shares/:token", element: }, { element: , children: [ { path: Routes.ATTACHMENTS, element: }, { path: Routes.INBOX, element: }, { path: Routes.SETTING, element: }, ], }, { path: "403", element: }, { path: "404", element: }, { path: "*", element: }, ], }, ], }, ]; const router = createBrowserRouter(routeConfig); export default router;