Skip to content

fix(mime): specify charset parameter per MIME type instead of mechanical detection#4912

Open
renatograsso10 wants to merge 1 commit intohonojs:mainfrom
renatograsso10:feat/mime-charset-per-type
Open

fix(mime): specify charset parameter per MIME type instead of mechanical detection#4912
renatograsso10 wants to merge 1 commit intohonojs:mainfrom
renatograsso10:feat/mime-charset-per-type

Conversation

@renatograsso10
Copy link
Copy Markdown

Closes #4510

Summary

Move the ; charset=utf-8 suffix from a runtime check in getMimeType into the _baseMimes data table itself, and extend it to image/svg+xml, application/xhtml+xml, and application/xml as the issue requested.

Why

Per #4510:

  1. Other entries besides text/* benefit from a charset parameter (XML-based formats).
  2. Per IANA media types, charset is not universal across all text/* formats — keeping it as data lets us be precise per entry rather than running a generic startsWith("text") check.

The output of getMimeType() for previously-charseted text types is unchanged (still text/<sub>; charset=utf-8), so this is backward-compatible at the call site.

Changes

src/utils/mime.ts

  • _baseMimes: append ; charset=utf-8 to css, csv, htm, html, ics, js, mjs, txt, svg, xhtml, xml.
  • getMimeType: drop the if (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.ts

  • 2 new it blocks: charset values for the new XML-based formats and the additional text types; getExtension round-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: getMimeType is used by serve-static, BaseMime is referenced by context.ts)
  • npx tsc --noEmit clean

The pre-existing stream/streamSSE onAbort and a few other failures on main are unrelated to this change (verified via git stash + re-run).

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal to change to specifying charset parameter individually for each MIME type

1 participant