Skip to content

Commit 8c1916c

Browse files
committed
Add Server Component example 🚀
1 parent 00fbc6e commit 8c1916c

3 files changed

Lines changed: 34 additions & 43 deletions

File tree

‎app/entry.ssr.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export async function generateHTML(
4141
},
4242
);
4343

44-
if (isbot(userAgent)) {
45-
await htmlStream.allReady;
46-
}
44+
// if (isbot(userAgent)) {
45+
// await htmlStream.allReady;
46+
// }
4747

4848
return htmlStream;
4949
},

‎app/pages/splash.tsx‎

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { Link } from "react-router";
2-
import { Suspense, use } from "react";
2+
import { Suspense } from "react";
33

44
import iconsHref from "~/icons.svg";
55
import { getStats } from "~/modules/stats";
66
import type { Route } from "./+types/splash";
77

8-
export let loader = async () => {
9-
const stats = getStats();
10-
return { stats };
11-
};
12-
138
// TODO: target="_blank" for discord?
149

1510
export const meta: Route.MetaFunction = ({ matches }) => {
@@ -110,7 +105,7 @@ const adventures: Adventure[] = [
110105
},
111106
];
112107

113-
export default function Home({ loaderData }: Route.ComponentProps) {
108+
export function ServerComponent() {
114109
return (
115110
<main className="flex min-h-full w-full flex-col items-center justify-center dark:bg-gray-900">
116111
<section className="from-23% via-82% flex w-full flex-col items-center gap-y-12 bg-gradient-to-b from-[#CCD2DE] via-[#D9DDE6] to-white to-100% py-[96px] dark:from-[#595F6C] dark:via-[#202228] dark:via-65% dark:to-gray-900 md:py-[160px]">
@@ -212,28 +207,8 @@ export default function Home({ loaderData }: Route.ComponentProps) {
212207
</div>
213208
</section>
214209
<section className="grid w-full place-content-center p-12">
215-
<Suspense fallback={null}>
216-
<Await resolve={loaderData.stats}>
217-
{(stats) => (
218-
<dl className="grid grid-cols-1 gap-x-6 gap-y-16 md:grid-cols-2">
219-
{stats.map(({ svgId, count, label }) => (
220-
<div key={svgId} className="flex w-[308px] gap-2">
221-
<svg className="h-8 w-8 text-gray-600" aria-hidden="true">
222-
<use href={`${iconsHref}#${svgId}`} />
223-
</svg>
224-
<div className="flex flex-col">
225-
<dd className="text-2xl font-semibold text-gray-700 dark:text-gray-200">
226-
{count?.toLocaleString("en-US")}
227-
</dd>
228-
<dt className="text-gray-500 dark:text-gray-400">
229-
{label}
230-
</dt>
231-
</div>
232-
</div>
233-
))}
234-
</dl>
235-
)}
236-
</Await>
210+
<Suspense fallback={<div>loading...</div>}>
211+
<Stats />
237212
</Suspense>
238213
</section>
239214
<section className="grid h-[205px] w-full place-content-center place-items-center gap-y-6 bg-gray-50 p-12 dark:bg-black">
@@ -252,13 +227,24 @@ export default function Home({ loaderData }: Route.ComponentProps) {
252227
);
253228
}
254229

255-
function Await<T>({
256-
children,
257-
resolve,
258-
}: {
259-
children: (r: T) => React.ReactNode;
260-
resolve: Promise<T>;
261-
}) {
262-
const result = use(resolve);
263-
return <>{children(result)}</>;
230+
async function Stats() {
231+
const stats = await getStats();
232+
233+
return (
234+
<dl className="grid grid-cols-1 gap-x-6 gap-y-16 md:grid-cols-2">
235+
{stats.map(({ svgId, count, label }) => (
236+
<div key={svgId} className="flex w-[308px] gap-2">
237+
<svg className="h-8 w-8 text-gray-600" aria-hidden="true">
238+
<use href={`${iconsHref}#${svgId}`} />
239+
</svg>
240+
<div className="flex flex-col">
241+
<dd className="text-2xl font-semibold text-gray-700 dark:text-gray-200">
242+
{count?.toLocaleString("en-US")}
243+
</dd>
244+
<dt className="text-gray-500 dark:text-gray-400">{label}</dt>
245+
</div>
246+
</div>
247+
))}
248+
</dl>
249+
);
264250
}

‎server.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import compression from "compression";
1+
// import compression from "compression";
22
import express from "express";
33
import morgan from "morgan";
44
import { rateLimit } from "express-rate-limit";
@@ -20,9 +20,14 @@ const limiter = rateLimit({
2020

2121
app.use(limiter);
2222

23-
app.use(compression());
23+
// could use fastly's compression
24+
// jk already done
25+
// app.use(compression());
2426
app.disable("x-powered-by");
2527

28+
// TODO: use remix fetch server and remove development
29+
// fetchServer.route('ANY', '*', build.fetch)
30+
2631
if (DEVELOPMENT) {
2732
console.log("Starting development server");
2833
const vite = await import("vite");

0 commit comments

Comments
 (0)