-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.tsx
More file actions
51 lines (45 loc) · 1.5 KB
/
server.tsx
File metadata and controls
51 lines (45 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { eventHandler } from "vinxi/http";
import { renderToPipeableStream } from "react-dom/server";
import App from "./app";
import { Suspense } from "react";
import { Route, Router } from "wouter";
import { lazyRoute, renderAsset } from "@vinxi/react";
import { getManifest } from "vinxi/manifest";
import fileRoutes from "vinxi/routes";
export default eventHandler(async (event) => {
const clientManifest = getManifest("client");
const ssrManifest = getManifest("ssr");
const routes = fileRoutes.map((route) => ({
path: route.path,
component: lazyRoute(route.$component, clientManifest, ssrManifest),
}));
const assets = await clientManifest.inputs[clientManifest.handler].assets();
const base =
import.meta.env.BASE_URL === "/" ? undefined : import.meta.env.BASE_URL;
const stream = renderToPipeableStream(
<Router ssrPath={event.path} base={base}>
{/* @ts-expect-error */}
<App assets={<Suspense>{assets.map(renderAsset)}</Suspense>}>
<Suspense>
{routes.map((route) => (
<Route
path={route.path}
key={route.path}
component={route.component}
/>
))}
</Suspense>
</App>
</Router>,
{
bootstrapModules: [
clientManifest.inputs[clientManifest.handler].output.path,
],
bootstrapScriptContent: `
window.manifest = ${JSON.stringify(clientManifest.json())};
window.base = ${JSON.stringify(base)}
`,
}
);
return stream;
});