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;
filePath: string;
hash: string | null;
contentType: string;
}
export interface BuildSnapshot<State> {
@@ -22,6 +23,7 @@ export interface BuildSnapshot<State> {
export interface StaticFile {
hash: string | null;
size: number;
contentType: string;
readable: ReadableStream<Uint8Array> | Uint8Array;
close(): void;
}
@@ -63,6 +65,7 @@ export class ProdBuildCache<State> implements BuildCache<State> {
return {
hash: info.hash,
contentType: info.contentType,
size: stat.size,
readable: file.readable,
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 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<State> implements DevBuildCache<State> {
hash: processed.hash,
readable: processed.content,
size: processed.content.byteLength,
contentType: processed.contentType,
close: () => {},
};
}
@@ -97,6 +100,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
hash: null,
size: stat.size,
readable: file.readable,
contentType: getContentType(unprocessed),
close: () => file.close(),
};
} catch {
@@ -167,7 +171,11 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
content: Uint8Array,
hash: string | null,
): Promise<void> {
this.#processedFiles.set(pathname, { content, hash });
this.#processedFiles.set(pathname, {
content,
hash,
contentType: getContentType(pathname),
});
}
async flush(): Promise<void> {
@@ -311,6 +319,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
name,
hash,
filePath: path.relative(root, filePath),
contentType: getContentType(filePath),
});
}
@@ -332,6 +341,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
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";
}
+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 { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
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 contentType = getContentType(ext);
const headers = new Headers({
"Content-Type": contentType ?? "text/plain",
"Content-Type": file.contentType,
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 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: () => {},
});
}