Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 647bae4

Browse files
committed
Add cache-control header for static file requests
1 parent f13c677 commit 647bae4

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

server/mod.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getContentType } from "../lib/mime.ts";
88
import util from "../lib/util.ts";
99
import { errorHtml } from "./error.ts";
1010
import { DependencyGraph } from "./graph.ts";
11-
import { getDeploymentId, initModuleLoaders, loadImportMap, loadJSXConfig } from "./helpers.ts";
11+
import { getDeploymentId, initModuleLoaders, loadImportMap, loadJSXConfig, regFullVersion } from "./helpers.ts";
1212
import { type HTMLRewriterHandlers, loadAndFixIndexHtml } from "./html.ts";
1313
import renderer, { type SSR } from "./renderer.ts";
1414
import { content, type CookieOptions, json, setCookieHeader } from "./response.ts";
@@ -53,7 +53,7 @@ export const serve = (options: ServerOptions = {}) => {
5353
});
5454
const handler = async (req: Request, connInfo: ConnInfo): Promise<Response> => {
5555
const url = new URL(req.url);
56-
const { host, pathname } = url;
56+
const { host, pathname, searchParams } = url;
5757

5858
if (pathname === "/-/hmr") {
5959
const { socket, response } = Deno.upgradeWebSocket(req, {});
@@ -150,6 +150,9 @@ export const serve = (options: ServerOptions = {}) => {
150150
}
151151
headers.append("ETag", etag);
152152
}
153+
if (searchParams.get("v") || regFullVersion.test(pathname)) {
154+
headers.append("Cache-Control", "public, max-age=31536000, immutable");
155+
}
153156
const file = await Deno.open(filePath, { read: true });
154157
return new Response(readableStreamFromReader(file), { headers });
155158
}
@@ -176,6 +179,7 @@ export const serve = (options: ServerOptions = {}) => {
176179
}
177180
}
178181

182+
// create context object
179183
const customHTMLRewriter = new Map<string, HTMLRewriterHandlers>();
180184
const ctx = {
181185
connInfo,

0 commit comments

Comments
 (0)