diff --git a/src/build_cache.ts b/src/build_cache.ts index 3bf5bc9c..de707508 100644 --- a/src/build_cache.ts +++ b/src/build_cache.ts @@ -10,6 +10,7 @@ export interface FileSnapshot { name: string; filePath: string; hash: string | null; + contentType: string; } export interface BuildSnapshot { @@ -22,6 +23,7 @@ export interface BuildSnapshot { export interface StaticFile { hash: string | null; size: number; + contentType: string; readable: ReadableStream | Uint8Array; close(): void; } @@ -63,6 +65,7 @@ export class ProdBuildCache implements BuildCache { return { hash: info.hash, + contentType: info.contentType, size: stat.size, readable: file.readable, close: () => file.close(), diff --git a/src/dev/dev_build_cache.ts b/src/dev/dev_build_cache.ts index 7436b17f..17581b66 100644 --- a/src/dev/dev_build_cache.ts +++ b/src/dev/dev_build_cache.ts @@ -14,9 +14,11 @@ import type { ResolvedBuildConfig } from "./builder.ts"; import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts"; import type { Command } from "../commands.ts"; import type { ServerIslandRegistry } from "../context.ts"; +import { contentType as getStdContentType } from "@std/media-types/content-type"; export interface MemoryFile { hash: string | null; + contentType: string; content: Uint8Array; } @@ -81,6 +83,7 @@ export class MemoryBuildCache implements DevBuildCache { hash: processed.hash, readable: processed.content, size: processed.content.byteLength, + contentType: processed.contentType, close: () => {}, }; } @@ -97,6 +100,7 @@ export class MemoryBuildCache implements DevBuildCache { hash: null, size: stat.size, readable: file.readable, + contentType: getContentType(unprocessed), close: () => file.close(), }; } catch { @@ -167,7 +171,11 @@ export class MemoryBuildCache implements DevBuildCache { content: Uint8Array, hash: string | null, ): Promise { - this.#processedFiles.set(pathname, { content, hash }); + this.#processedFiles.set(pathname, { + content, + hash, + contentType: getContentType(pathname), + }); } async flush(): Promise { @@ -311,6 +319,7 @@ export class DiskBuildCache implements DevBuildCache { name, hash, filePath: path.relative(root, filePath), + contentType: getContentType(filePath), }); } @@ -332,6 +341,7 @@ export class DiskBuildCache implements DevBuildCache { name, hash, filePath: path.relative(root, filePath), + contentType: getContentType(filePath), }); } @@ -459,3 +469,8 @@ async function hashContent( ); return encodeHex(hashBuf); } + +export function getContentType(filePath: string): string { + const ext = path.extname(filePath); + return getStdContentType(ext) ?? "text/plain"; +} diff --git a/src/middlewares/static_files.ts b/src/middlewares/static_files.ts index 8fe90990..57899ff7 100644 --- a/src/middlewares/static_files.ts +++ b/src/middlewares/static_files.ts @@ -1,5 +1,3 @@ -import * as path from "@std/path"; -import { contentType as getContentType } from "@std/media-types/content-type"; import type { Middleware } from "./mod.ts"; import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx"; import { BUILD_ID } from "../runtime/build_id.ts"; @@ -64,12 +62,9 @@ export function staticFiles(): Middleware { }); } - const ext = path.extname(pathname); const etag = file.hash; - - const contentType = getContentType(ext); const headers = new Headers({ - "Content-Type": contentType ?? "text/plain", + "Content-Type": file.contentType, vary: "If-None-Match", }); diff --git a/src/middlewares/static_files_test.ts b/src/middlewares/static_files_test.ts index 9c6368b4..a85ba3b2 100644 --- a/src/middlewares/static_files_test.ts +++ b/src/middlewares/static_files_test.ts @@ -6,6 +6,7 @@ import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx"; import { BUILD_ID } from "../runtime/build_id.ts"; import type { Command } from "../commands.ts"; import type { ServerIslandRegistry } from "../context.ts"; +import { getContentType } from "../dev/dev_build_cache.ts"; class MockBuildCache implements BuildCache { root = ""; @@ -23,6 +24,7 @@ class MockBuildCache implements BuildCache { hash: info.hash, size: text.byteLength, readable: text, + contentType: getContentType(normalized), close: () => {}, }); }