Skip to content

Commit

Permalink
Add Vite integration and refactor portal entry points for improved se…
Browse files Browse the repository at this point in the history
…rver-side rendering
  • Loading branch information
jthoward64 committed Jan 10, 2025
1 parent b5f8c9c commit 590e289
Show file tree
Hide file tree
Showing 12 changed files with 2,782 additions and 274 deletions.
2 changes: 1 addition & 1 deletion packages/portal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/render.tsx"></script>
<script type="module" src="/src/entry-client.tsx"></script>
</body>
</html>
18 changes: 13 additions & 5 deletions packages/portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
},
"type": "module",
"exports": {
"./*": {
"import": "./dist/*"
"./server/*": {
"import": "./dist/server/*"
},
"./client/*": {
"import": "./dist/client/*"
},
"./src/*": {
"import": "./src/*"
}
},
"imports": {
Expand Down Expand Up @@ -53,7 +59,8 @@
"@sentry/react": "^8.38.0",
"@sentry/vite-plugin": "^2.22.6",
"@tanstack/react-form": "^0.41.0",
"@tanstack/react-router": "^1.95.1",
"@tanstack/react-router": "^1.95.3",
"@tanstack/start": "^1.95.3",
"@uidotdev/usehooks": "^2.4.1",
"@ukdanceblue/common": "workspace:^",
"@urql/core": "^5.1.0",
Expand All @@ -76,6 +83,7 @@
"react-markdown": "^9.0.1",
"react-to-print": "^3.0.2",
"reflect-metadata": "^0.2.2",
"superjson": "^2.2.2",
"thumbhash": "^0.1.1",
"ts-results-es": "^4.2.0",
"type-graphql": "^2.0.0-rc.2",
Expand All @@ -90,8 +98,8 @@
},
"devDependencies": {
"@refinedev/cli": "^2.16.42",
"@tanstack/router-devtools": "^1.95.1",
"@tanstack/router-plugin": "^1.95.1",
"@tanstack/router-devtools": "^1.95.3",
"@tanstack/router-plugin": "^1.95.3",
"@types/lodash.isequal": "^4.5.8",
"@types/luxon": "^3.4.2",
"@types/pluralize": "^0.0.33",
Expand Down
25 changes: 25 additions & 0 deletions packages/portal/src/entry-client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "normalize.css";
import "./root.css";

import { init, tanstackRouterBrowserTracingIntegration } from "@sentry/react";
import { StartClient } from "@tanstack/start";
import { hydrateRoot } from "react-dom/client";

import { createRouter } from "./router.js";

const router = createRouter();

init({
dsn: "https://f149f5546299b507f5e7b9b4aeafc2f4@o4507762130681856.ingest.us.sentry.io/4508071881932800",
integrations: [tanstackRouterBrowserTracingIntegration(router)],
tracesSampleRate: 0.1,
tracePropagationTargets: [
"localhost",
/^https:\/\/(app|dev)\.danceblue\.org\/(api|graphql)/,
],
enabled: ["https://app.danceblue.org", "https://dev.danceblue.org"].includes(
window.location.origin
),
});

hydrateRoot(document, <StartClient router={router} />);
19 changes: 19 additions & 0 deletions packages/portal/src/entry-server.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getRouterManifest } from "@tanstack/start/router-manifest";
import {
createRequestHandler,
defaultStreamHandler,
} from "@tanstack/start/server";

import { createRouter } from "./router";

export async function render(opts: {
url: string;
head: string;
req: Request;
}) {
return createRequestHandler({
createRouter,
request: opts.req,
getRouterManifest,
})(defaultStreamHandler);
}
166 changes: 15 additions & 151 deletions packages/portal/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,166 +1,30 @@
import "normalize.css";
import "./root.css";

import { DevtoolsPanel, DevtoolsProvider } from "@refinedev/devtools";
import { browserTracingIntegration, init } from "@sentry/react";
import { createRouter, RouterProvider } from "@tanstack/react-router";
import { App, Result, Spin } from "antd";
import { DevtoolsProvider } from "@refinedev/devtools";
import { App as AntApp } from "antd";
import type { useAppProps } from "antd/es/app/context.js";
import { StrictMode, useEffect, useState } from "react";
import { StrictMode } from "react";
import { Provider as UrqlProvider } from "urql";

import { AntdThemeProvider } from "#config/ant.js";
import { API_BASE_URL, urqlClient } from "#config/api.js";
import { urqlClient } from "#config/api.js";
import { MarathonConfigProvider } from "#config/marathon.js";
import { NivoThemeProvider } from "#config/nivo.js";
import { LoadingRibbon } from "#elements/components/design/RibbonSpinner.js";

import { routeTree } from "./routeTree.gen.js";

// @ts-expect-error Avoid an annoying log message from a library
window.process = { env: {} };

init({
dsn: "https://f149f5546299b507f5e7b9b4aeafc2f4@o4507762130681856.ingest.us.sentry.io/4508071881932800",
integrations: [browserTracingIntegration()],
tracesSampleRate: 0.1,
tracePropagationTargets: [
"localhost",
/^https:\/\/(app|dev)\.danceblue\.org\/(api|graphql)/,
],
enabled: ["https://app.danceblue.org", "https://dev.danceblue.org"].includes(
window.location.origin
),
});

const router = createRouter({
routeTree,
defaultPendingComponent: () => (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "100%",
width: "100%",
}}
>
<Spin
size="large"
tip="Connecting to server..."
fullscreen
indicator={<LoadingRibbon size={96} />}
>
<div
style={{
padding: 64,
borderRadius: 4,
}}
/>
</Spin>
</div>
),
defaultNotFoundComponent: () => (
<Result
status="404"
title="404"
subTitle="Sorry, the page you visited does not exist."
/>
),
defaultErrorComponent: ({ error }) => (
<Result title={error.name} subTitle={error.message} status="error" />
),

context: {
urqlClient,
antApp: {} as useAppProps,
},
defaultPreload: false,
Wrap: Context,
});

declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
// interface StaticDataRouteOption {}
}

function Context({ children }: { children: React.ReactNode }) {
return (
<AntdThemeProvider>
<NivoThemeProvider>
<UrqlProvider value={urqlClient}>
<DevtoolsProvider>
<MarathonConfigProvider>
<AntApp style={{ height: "100%" }}>{children}</AntApp>
</MarathonConfigProvider>
</DevtoolsProvider>
</UrqlProvider>
</NivoThemeProvider>
</AntdThemeProvider>
);
}

function RouterComponent() {
const [isServerReachable, setIsServerReachable] = useState<
boolean | undefined
>(undefined);

useEffect(() => {
if (isServerReachable !== undefined) {
return;
}

const timeout = setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (isServerReachable === undefined) {
setIsServerReachable(false);
}
}, 5000);
fetch(new URL("/api/healthcheck", API_BASE_URL))
.then(async (res) => {
clearTimeout(timeout);
if (res.ok) {
const text = await res.text();
setIsServerReachable(text === "OK");
} else {
setIsServerReachable(false);
}
})
.catch(() => {
clearTimeout(timeout);
setIsServerReachable(false);
});
}, [isServerReachable]);

const antApp = App.useApp();

return isServerReachable === false ? (
<Result
status="error"
title="Server is not reachable"
subTitle={
<span>
Contact tech committee or see our{" "}
<a href="https://status.danceblue.org/">status page</a> for more
information.
</span>
}
/>
) : (
<>
<RouterProvider router={router} context={{ antApp }} />
{import.meta.env.MODE === "development" && <DevtoolsPanel />}
</>
);
}

export function Main() {
export function MainContext({ children }: { children: React.ReactNode }) {
return (
<StrictMode>
<RouterComponent />
<AntdThemeProvider>
<NivoThemeProvider>
<UrqlProvider value={urqlClient}>
<DevtoolsProvider>
<MarathonConfigProvider>
<AntApp style={{ height: "100%" }}>{children}</AntApp>
</MarathonConfigProvider>
</DevtoolsProvider>
</UrqlProvider>
</NivoThemeProvider>
</AntdThemeProvider>
</StrictMode>
);
}
9 changes: 0 additions & 9 deletions packages/portal/src/render.tsx

This file was deleted.

Loading

0 comments on commit 590e289

Please sign in to comment.