-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathvite-dev-custom-entry-test.ts
103 lines (92 loc) · 3.14 KB
/
vite-dev-custom-entry-test.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { expect } from "@playwright/test";
import type { Files } from "./helpers/vite.js";
import { test, viteConfig } from "./helpers/vite.js";
const js = String.raw;
const files: Files = async ({ port }) => ({
"vite.config.ts": await viteConfig.basic({ port }),
"app/entry.server.tsx": js`
import { PassThrough } from "node:stream";
import type { EntryContext } from "react-router";
import { createReadableStreamFromReadable } from "@react-router/node";
import { ServerRouter } from "react-router";
import { renderToPipeableStream } from "react-dom/server";
export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
return new Promise((resolve, reject) => {
let shellRendered = false;
const { pipe, abort } = renderToPipeableStream(
<ServerRouter context={remixContext} url={request.url} />,
{
onShellReady() {
shellRendered = true;
const body = new PassThrough();
const stream = createReadableStreamFromReadable(body);
responseHeaders.set("Content-Type", "text/html");
// Used to test that the request object is an instance of the global Request constructor
responseHeaders.set("x-test-request-instanceof-request", String(request instanceof Request));
resolve(
new Response(stream, {
headers: responseHeaders,
status: responseStatusCode,
})
);
pipe(body);
},
onShellError(error: unknown) {
reject(error);
},
onError(error: unknown) {
responseStatusCode = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
}
);
setTimeout(abort, 5000);
});
}
`,
"app/root.tsx": js`
import { Links, Meta, Outlet, Scripts } from "react-router";
export default function Root() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<div id="content">
<h1>Root</h1>
<Outlet />
</div>
<Scripts />
</body>
</html>
);
}
`,
"app/routes/_index.tsx": js`
export default function IndexRoute() {
return <div>IndexRoute</div>
}
`,
});
test.describe("Vite custom entry dev", () => {
// Ensure libraries/consumers can perform an instanceof check on the request
test("request instanceof Request", async ({ request, dev }) => {
let { port } = await dev(files);
let res = await request.get(`http://localhost:${port}/`);
expect(res.headers()).toMatchObject({
"x-test-request-instanceof-request": "true",
});
});
});