fix(mime): specify charset parameter per MIME type instead of mechanical detection#4912
Open
renatograsso10 wants to merge 1 commit intohonojs:mainfrom
Open
fix(mime): specify charset parameter per MIME type instead of mechanical detection#4912renatograsso10 wants to merge 1 commit intohonojs:mainfrom
renatograsso10 wants to merge 1 commit intohonojs:mainfrom
Conversation
…cal detection Per honojs#4510: - Inline `; charset=utf-8` in `_baseMimes` for all text/* entries plus `image/svg+xml`, `application/xhtml+xml`, and `application/xml`, which are XML-based formats that benefit from an explicit charset. - Drop the `if (mimeType.startsWith("text"))` block in `getMimeType`; the charset now lives in the data, not in the lookup logic. Output for text types is unchanged (still "text/<sub>; charset=utf-8"). - Update `getExtension` to compare the base type (the part before the first `;`), so callers can pass either a raw type ("text/html") or one with parameters ("text/html; charset=utf-8") and still get the correct extension back. Tests: 5 mime.test.ts cases pass (3 existing + 2 new) covering the new charset values for svg/xhtml/xml/ics/mjs/css/csv and getExtension with parameters. Closes honojs#4510
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4510
Summary
Move the
; charset=utf-8suffix from a runtime check ingetMimeTypeinto the_baseMimesdata table itself, and extend it toimage/svg+xml,application/xhtml+xml, andapplication/xmlas the issue requested.Why
Per #4510:
text/*benefit from acharsetparameter (XML-based formats).charsetis not universal across alltext/*formats — keeping it as data lets us be precise per entry rather than running a genericstartsWith("text")check.The output of
getMimeType()for previously-charseted text types is unchanged (stilltext/<sub>; charset=utf-8), so this is backward-compatible at the call site.Changes
src/utils/mime.ts_baseMimes: append; charset=utf-8tocss,csv,htm,html,ics,js,mjs,txt,svg,xhtml,xml.getMimeType: drop theif (mimeType.startsWith("text"))block; data now carries the charset.getExtension: compare the base type (split on;) so callers can pass either"text/html"or"text/html; charset=utf-8"and still get the right extension. No behavior change for callers passing the raw type.src/utils/mime.test.tsitblocks: charset values for the new XML-based formats and the additional text types;getExtensionround-trip with and without parameters.Test plan
npx vitest run src/utils/mime.test.ts— 5/5 pass (3 existing + 2 new)npx vitest run src/middleware/compress src/middleware/jsx-renderer src/middleware/serve-static— 56/56 pass (covers the consumers I could trace:getMimeTypeis used byserve-static,BaseMimeis referenced bycontext.ts)npx tsc --noEmitcleanThe pre-existing
stream/streamSSEonAbortand a few other failures onmainare unrelated to this change (verified viagit stash+ re-run).