-
Notifications
You must be signed in to change notification settings - Fork 378
fix(server): serve static assets with standard MIME types #2713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8163ecf
7589212
e61581f
bdad882
f1c3b17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,28 +17,45 @@ import { ASSET_PREFIX_URL_DIR } from "../utils/asset-prefix.js"; | |
|
|
||
| /** Content-type lookup for static assets. Shared with prod-server.ts. */ | ||
| export const CONTENT_TYPES: Record<string, string> = { | ||
| ".js": "application/javascript", | ||
| ".mjs": "application/javascript", | ||
| ".css": "text/css", | ||
| ".avif": "image/avif", | ||
| ".bmp": "image/bmp", | ||
| ".csv": "text/csv; charset=utf-8", | ||
| ".eot": "application/vnd.ms-fontobject", | ||
| ".gif": "image/gif", | ||
| ".heic": "image/heic", | ||
| ".js": "application/javascript; charset=utf-8", | ||
| ".mjs": "application/javascript; charset=utf-8", | ||
| ".css": "text/css; charset=utf-8", | ||
| ".html": "text/html; charset=utf-8", | ||
| ".json": "application/json", | ||
| ".txt": "text/plain; charset=utf-8", | ||
| ".png": "image/png", | ||
| ".jpg": "image/jpeg", | ||
| ".ico": "image/x-icon", | ||
| ".jpeg": "image/jpeg", | ||
| ".gif": "image/gif", | ||
| ".jpg": "image/jpeg", | ||
| ".json": "application/json; charset=utf-8", | ||
| ".map": "application/json; charset=utf-8", | ||
| ".mp3": "audio/mpeg", | ||
| ".mp4": "video/mp4", | ||
| ".ogg": "audio/ogg", | ||
| ".ogv": "video/ogg", | ||
| ".pdf": "application/pdf", | ||
| ".png": "image/png", | ||
| ".rsc": "text/x-component", | ||
| ".svg": "image/svg+xml", | ||
| ".ico": "image/x-icon", | ||
| ".woff": "font/woff", | ||
| ".woff2": "font/woff2", | ||
| ".ttf": "font/ttf", | ||
| ".eot": "application/vnd.ms-fontobject", | ||
| ".txt": "text/plain; charset=utf-8", | ||
| ".wasm": "application/wasm", | ||
| ".webm": "video/webm", | ||
| ".webmanifest": "application/manifest+json", | ||
| ".webp": "image/webp", | ||
| ".avif": "image/avif", | ||
| ".map": "application/json", | ||
| ".rsc": "text/x-component", | ||
| ".woff": "font/woff", | ||
| ".woff2": "font/woff2", | ||
| ".xml": "application/xml", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor Next.js-parity nit (non-blocking): Next.js serve-static delegates to |
||
| }; | ||
|
|
||
| /** Resolve a path's MIME type with case-insensitive extension matching. */ | ||
| export function contentTypeForPath(filePath: string): string { | ||
| return CONTENT_TYPES[path.extname(filePath).toLowerCase()] ?? "application/octet-stream"; | ||
| } | ||
|
|
||
| /** | ||
| * Files below this size are buffered in memory at startup for zero-syscall | ||
| * serving via res.end(buffer). Above this, files stream via createReadStream. | ||
|
|
@@ -118,7 +135,7 @@ export class StaticFileCache { | |
| if (relativePath.startsWith(".vite/") || relativePath === ".vite") continue; | ||
|
|
||
| const ext = path.extname(relativePath); | ||
| const contentType = CONTENT_TYPES[ext] ?? "application/octet-stream"; | ||
| const contentType = contentTypeForPath(relativePath); | ||
| // Files under Vite's `assetsDir` are content-hashed. The default | ||
| // layout writes to `<ASSET_PREFIX_URL_DIR>/` (Next.js's canonical | ||
| // convention); when `assetPrefix` is a path prefix the layout | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parity nit (non-blocking):
mime-dbmarks bothimage/svg+xmlandapplication/xmlwithcharset: UTF-8, so strictsend/mime-types parity would emitimage/svg+xml; charset=utf-8andapplication/xml; charset=utf-8. As written they're bare, which is inconsistent with.csv/.txt/.htmlin the same map. Either add the charset or note the deliberate divergence. (isSafeImageContentTypesplits on;, so adding it wouldn't affect the SVG security gate.)