feat: optimize static asset content type handling (#3149)

No need to detect them at runtime when we can do this during build. This
saves us from having to load `@std/media-types` during production which
is quite a heavy library due to the amount of file types out there.
This commit is contained in:
Marvin Hagemeister
2025-07-29 20:30:13 +02:00
committed by GitHub
parent 1218038918
commit c30e46ec30
4 changed files with 22 additions and 7 deletions
+3
View File
@@ -10,6 +10,7 @@ export interface FileSnapshot {
name: string; name: string;
filePath: string; filePath: string;
hash: string | null; hash: string | null;
contentType: string;
} }
export interface BuildSnapshot<State> { export interface BuildSnapshot<State> {
@@ -22,6 +23,7 @@ export interface BuildSnapshot<State> {
export interface StaticFile { export interface StaticFile {
hash: string | null; hash: string | null;
size: number; size: number;
contentType: string;
readable: ReadableStream<Uint8Array> | Uint8Array; readable: ReadableStream<Uint8Array> | Uint8Array;
close(): void; close(): void;
} }
@@ -63,6 +65,7 @@ export class ProdBuildCache<State> implements BuildCache<State> {
return { return {
hash: info.hash, hash: info.hash,
contentType: info.contentType,
size: stat.size, size: stat.size,
readable: file.readable, readable: file.readable,
close: () => file.close(), close: () => file.close(),
+16 -1
View File
@@ -14,9 +14,11 @@ import type { ResolvedBuildConfig } from "./builder.ts";
import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts"; import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts";
import type { Command } from "../commands.ts"; import type { Command } from "../commands.ts";
import type { ServerIslandRegistry } from "../context.ts"; import type { ServerIslandRegistry } from "../context.ts";
import { contentType as getStdContentType } from "@std/media-types/content-type";
export interface MemoryFile { export interface MemoryFile {
hash: string | null; hash: string | null;
contentType: string;
content: Uint8Array; content: Uint8Array;
} }
@@ -81,6 +83,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
hash: processed.hash, hash: processed.hash,
readable: processed.content, readable: processed.content,
size: processed.content.byteLength, size: processed.content.byteLength,
contentType: processed.contentType,
close: () => {}, close: () => {},
}; };
} }
@@ -97,6 +100,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
hash: null, hash: null,
size: stat.size, size: stat.size,
readable: file.readable, readable: file.readable,
contentType: getContentType(unprocessed),
close: () => file.close(), close: () => file.close(),
}; };
} catch { } catch {
@@ -167,7 +171,11 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
content: Uint8Array, content: Uint8Array,
hash: string | null, hash: string | null,
): Promise<void> { ): Promise<void> {
this.#processedFiles.set(pathname, { content, hash }); this.#processedFiles.set(pathname, {
content,
hash,
contentType: getContentType(pathname),
});
} }
async flush(): Promise<void> { async flush(): Promise<void> {
@@ -311,6 +319,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
name, name,
hash, hash,
filePath: path.relative(root, filePath), filePath: path.relative(root, filePath),
contentType: getContentType(filePath),
}); });
} }
@@ -332,6 +341,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
name, name,
hash, hash,
filePath: path.relative(root, filePath), filePath: path.relative(root, filePath),
contentType: getContentType(filePath),
}); });
} }
@@ -459,3 +469,8 @@ async function hashContent(
); );
return encodeHex(hashBuf); return encodeHex(hashBuf);
} }
export function getContentType(filePath: string): string {
const ext = path.extname(filePath);
return getStdContentType(ext) ?? "text/plain";
}
+1 -6
View File
@@ -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 type { Middleware } from "./mod.ts";
import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx"; import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
import { BUILD_ID } from "../runtime/build_id.ts"; import { BUILD_ID } from "../runtime/build_id.ts";
@@ -64,12 +62,9 @@ export function staticFiles<T>(): Middleware<T> {
}); });
} }
const ext = path.extname(pathname);
const etag = file.hash; const etag = file.hash;
const contentType = getContentType(ext);
const headers = new Headers({ const headers = new Headers({
"Content-Type": contentType ?? "text/plain", "Content-Type": file.contentType,
vary: "If-None-Match", vary: "If-None-Match",
}); });
+2
View File
@@ -6,6 +6,7 @@ import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
import { BUILD_ID } from "../runtime/build_id.ts"; import { BUILD_ID } from "../runtime/build_id.ts";
import type { Command } from "../commands.ts"; import type { Command } from "../commands.ts";
import type { ServerIslandRegistry } from "../context.ts"; import type { ServerIslandRegistry } from "../context.ts";
import { getContentType } from "../dev/dev_build_cache.ts";
class MockBuildCache implements BuildCache { class MockBuildCache implements BuildCache {
root = ""; root = "";
@@ -23,6 +24,7 @@ class MockBuildCache implements BuildCache {
hash: info.hash, hash: info.hash,
size: text.byteLength, size: text.byteLength,
readable: text, readable: text,
contentType: getContentType(normalized),
close: () => {}, close: () => {},
}); });
} }