-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.production.ts
41 lines (33 loc) · 1.01 KB
/
server.production.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
import { serveFile } from "@std/http/file-server";
import { join } from "@std/path/join";
import { createRequestHandler } from "react-router";
const handleRequest = createRequestHandler(
// @ts-expect-error - build output
await import("./build/server/index.js"),
"production",
);
Deno.serve(async (request) => {
const pathname = new URL(request.url).pathname;
try {
const filePath = join("./build/client", pathname);
const fileInfo = await Deno.stat(filePath);
if (fileInfo.isDirectory) {
throw new Deno.errors.NotFound();
}
const response = await serveFile(request, filePath, { fileInfo });
if (pathname.startsWith("/assets/")) {
response.headers.set(
"cache-control",
"public, max-age=31536000, immutable",
);
} else {
response.headers.set("cache-control", "public, max-age=600");
}
return response;
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
}
}
return handleRequest(request);
});