Skip to content

Adding MIME Type Mapping for .mjs Extension in DefaultMimeMappings #38900

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

Conversation

dlemaignent
Copy link

@dlemaignent dlemaignent commented Dec 21, 2023

Description

This pull request addresses the issue where the server responds with a MIME type of "application/octet-stream" for JavaScript module scripts with the ".mjs" extension. The problem manifests as the following error:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

I have tested this change in a local environment using the pdfjs-dist component with the following dependency:

<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>pdfjs-dist</artifactId>
    <version>4.0.269</version>
</dependency>

and using script tag like :

<script type="module" th:src="@{/webjars/pdfjs-dist/build/pdf.mjs}"></script>
<script type="module" th:src="@{/webjars/pdfjs-dist/build/pdf.worker.mjs}"></script>
<script type="module" th:src="@{/webjars/pdfjs-dist/web/pdf_viewer.mjs}"></script>

The test results confirm that the proposed changes resolve the reported issue.

To resolve this issue, I propose adding a default MIME type mapping for the ".mjs" extension to "text/javascript" directly within the Spring Boot application. This eliminates the need for a separate class like CustomMimeMapping with WebServerFactoryCustomizer like :

@Override
public void customize (ConfigurableServletWebServerFactory factory){
    MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
    mappings.add("mjs","text/javascript");
    factory.setMimeMappings(mappings);
}

Changes Made

* Added MIME type mapping for ".mjs" extension to "text/javascript" directly in DefaultMimeMappings.

Thanks

add mapping mjs to text/javascript
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 21, 2023
@philwebb
Copy link
Member

philwebb commented Dec 21, 2023

Thanks very much for the suggestion and pull request. We've had several similar requests in the past for additional mime types but we intentionally keep our list aligned with the defaults supported by Tomcat.

If you want to support additional types, you can do something like this in your application:

@Configuration
public class MyMimeMapping implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
        mappings.add(""mjs", "text/javascript");
        factory.setMimeMappings(mappings);
    }
}

@philwebb philwebb closed this Dec 21, 2023
@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants