Skip to content
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

fix: Add correct mime types for Javascript #5661

Merged
merged 5 commits into from Jan 26, 2024
Merged

fix: Add correct mime types for Javascript #5661

merged 5 commits into from Jan 26, 2024

Conversation

jonnitto
Copy link
Contributor

@jonnitto jonnitto commented Dec 27, 2023

This add the correct MIME type for the Javascript file extension for Modules .mjs and for CommonJS .cjs

CJS, MJS, and .JS are file extensions used to denote different types of JavaScript files. Here's the difference between them:

JS (JavaScript):

The .js extension is the most common file extension for JavaScript files. It is used to indicate that a file contains JavaScript code. .js files can be executed in different JavaScript environments, such as web browsers, servers, and other JavaScript runtime environments.

CJS (CommonJS):

CommonJS is a module system for JavaScript used in server-side environments like Node.js. The CJS module format allows you to define modules using the require and module.exports syntax. In CommonJS, each file is treated as a separate module, and you can import/export functionality between modules using require and module.exports.

MJS (ECMAScript Modules):

MJS is an extension used for JavaScript files that adhere to the ECMAScript Modules (ESM) specification. ECMAScript modules are part of the JavaScript language standard and provide a more modern and standardized way to define modules. ECMAScript modules use the import and export keywords to define dependencies and expose functionality between modules.

The Issue

If I try to include a Javascript Module with the extension .mjs, I'll get the 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.

How This PR Solves The Issue

It adds the correct type for all kind of Javascript files

Manual Testing Instructions

mkdir pr5661 && cd pr5661 && touch test.{js,mjs,cjs} && ddev config --auto --omit-containers db

# see text/javascript for all three types in nginx
ddev start && ddev exec curl -I https://pr5661.ddev.site/test.{js,mjs,cjs}

# see text/javascript for all three types in Apache2
ddev config --webserver-type apache-fpm && ddev restart && ddev exec curl -I https://pr5661.ddev.site/test.{js,mjs,cjs}

Automated Testing Overview

If there is way to test this, please tell me

Release/Deployment Notes

This does not affect the behavior of other parts of ddev itself.

If a user's application relies on a non-standards-compliant content-type mimetype for .mjs / .cjs files, their application may fail, as we are now serving the files as javascript

@jonnitto jonnitto requested a review from a team as a code owner December 27, 2023 13:21
@jonnitto jonnitto changed the title Fix: Add correct mime types for Javascript fix: Add correct mime types for Javascript Dec 27, 2023
@github-actions github-actions bot added bugfix dependencies Pull requests that update a dependency file labels Dec 27, 2023
@stasadev
Copy link
Member

Thank you, @jonnitto.

I combined types directives into one, and pushed a new Docker tag for DDEV webserver.

Check the artifacts below ⬇️

Copy link

github-actions bot commented Dec 27, 2023

@stasadev
Copy link
Member

Rebased because the artifact bot comment links were invalid (see #5662).

@rpkoller
Copy link
Collaborator

in regards of consistency is the mime type already available for apache? cuz the patch only updates the nginx config but ddev is shipping with nginx and apache? so i wondered if the apache config would have to be updated as well that both support it?

@stasadev
Copy link
Member

Thank you @rpkoller, this is a good point.

I checked Apache2, and it gives the correct application/javascript for .js and .mjs out of the box, but not for .cjs.

Copy link
Member

@stasadev stasadev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Debian bullseye we have application/javascript:

cat /etc/mime.types | grep "js mjs"
application/javascript    js mjs

In Debian Bookworm we have text/javascript (source from media-types):

cat /etc/mime.types | grep "js mjs"
text/javascript    es js mjs

If this is the right file for Apache, we can add cjs here:

And it is better to use text/javascript instead of deprecated application/javascript.
See https://trac.nginx.org/nginx/ticket/2216

jonnitto and others added 5 commits January 23, 2024 21:35
Copy link
Member

@stasadev stasadev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased, updated MIME types for Apache2, used newer text/javascript, tested and updated the manual testing instructions.

@stasadev stasadev merged commit 9676950 into ddev:master Jan 26, 2024
22 of 23 checks passed
@jonnitto jonnitto deleted the patch-1 branch January 28, 2024 22:23
@rfay
Copy link
Member

rfay commented Mar 6, 2024

@jonnitto @stasadev I'm sure you saw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants