Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.8 KB
Raw Blame
import {hydrateRoot} from "react-dom/client";
import {createBrowserRouter, matchRoutes} from "react-router";
import {RouterProvider} from "react-router/dom";

import {router} from "./router";
import {App} from "./App";
import {queryClient} from "./utilites/queryClient";
import {dynamicActivateLocale, getClientLocale, getSupportedLocale,} from "./locales.ts";
import type {ThemeColors} from "./utilites/themeColors.ts";
import type {DehydratedState} from "@tanstack/react-query";

declare global {
    interface Window {
        __REHYDRATED_STATE__?: DehydratedState;
        __THEME_COLORS__?: ThemeColors;
        __SSR_STYLED__?: boolean;
    }
}

const dehydratedState = window.__REHYDRATED_STATE__;

async function initClientApp() {
    const rawLocale = getClientLocale();
    const locale = getSupportedLocale(rawLocale);
    await dynamicActivateLocale(locale);
    const themeColors = window.__THEME_COLORS__
        ?? (await import("./utilites/themeColors.ts")).generateThemeColors();

    // Resolve lazy-loaded routes before hydration
    const matches = matchRoutes(router, window.location)?.filter((m) => m.route.lazy);
    if (matches && matches.length > 0) {
        await Promise.all(
            matches.map(async (m) => {
                const routeModule = typeof m.route.lazy === "function" ? await m.route.lazy() : undefined;
                Object.assign(m.route, {...routeModule, lazy: undefined});
            })
        );
    }

    const browserRouter = createBrowserRouter(router);

    hydrateRoot(
        document.getElementById("app") as HTMLElement,
        <App queryClient={queryClient} locale={rawLocale} themeColors={themeColors} dehydratedState={dehydratedState} ssrStyled={window.__SSR_STYLED__}>
            <RouterProvider router={browserRouter}/>
        </App>
    );
}

initClientApp();