Skip to content

Commit 40bf5e8

Browse files
authored
Improve JS MIME type detection
Enhances the `_setContentTypeFromPath` method in `AsyncFileResponse` to better handle JavaScript MIME types. Previously, `.js` files were correctly recognized, but `.mjs` files (ES modules) were not and defaulted to `application/javascript`. This enhancement ensures both `.js` and `.mjs` files are assigned `text/javascript`, improving compatibility with modern browsers. All other MIME type handling remains unchanged. This change enhances robustness and standards compliance of the file server. Fix the issue #267
1 parent 879d57c commit 40bf5e8

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

src/WebResponses.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ void AsyncFileResponse::_setContentTypeFromPath(const String &path) {
639639
_contentType = T_text_html;
640640
} else if (strcmp(dot, T__css) == 0) {
641641
_contentType = T_text_css;
642-
} else if (strcmp(dot, T__js) == 0) {
643-
_contentType = T_application_javascript;
642+
} else if (strcmp(dot, T__js) == 0 || strcmp(dot, T__mjs) == 0) {
643+
_contentType = T_text_javascript;
644644
} else if (strcmp(dot, T__json) == 0) {
645645
_contentType = T_application_json;
646646
} else if (strcmp(dot, T__png) == 0) {

src/literals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static constexpr const char *T__jpg = ".jpg"; // JPEG/JPG: Photos. Legacy s
115115
static constexpr const char *T__js = ".js"; // JavaScript: Interactive functionality
116116
static constexpr const char *T__json = ".json"; // JSON: Data exchange format
117117
static constexpr const char *T__mp4 = ".mp4"; // MP4: Proprietary format. Worse compression than WEBM.
118+
static constexpr const char *T__mjs = ".mjs"; // MJS: JavaScript module format
118119
static constexpr const char *T__opus = ".opus"; // OPUS: High compression audio format
119120
static constexpr const char *T__pdf = ".pdf"; // PDF: Universal document format
120121
static constexpr const char *T__png = ".png"; // PNG: Icons, logos, transparency. Legacy support

0 commit comments

Comments
 (0)