mjackson/unpkg

`*.cjs` extension files served as text/plain

Swivelgames opened this issue · 10 comments

Modern Node.js modules can be served as ESMs or CommonJS modules. These are differentiated either by their package.json, or by the extension of the file. It appears that unpkg properly serves .mjs files, but seems to treat *.cjs files as plaintext incorrectly.

Instead of text/plain, the content-type of .cjs files should be application/javascript.

Node's documentation can serve as reference for the honoring of these files: https://nodejs.org/api/modules.html#enabling

Running into this as well. Looks like this issue has been reported in 2020 in #268.
Would be nice to have as this basically makes UNPKG unusable for a large part of the JS ecosystem. 😞

If I understand correctly (based on #65), the content-type comes from the mime package?

It is currently pinned at 2.4.*

"mime": "^2.4.0",

The cjs was added in v2.4.5 as application/node, so it should work.

Not sure why it doesn't. What am I missing?

As a workaround, I am using the ?module parameter to get the correct content-type.

The cjs was added in v2.4.5 as application/node, so it should work.
Not sure why it doesn't. What am I missing?

Looks like the lockfile needs to be updated:

unpkg/yarn.lock

Line 4620 in af8c8db

version "2.4.3"

#364 sounds like an easy merge 🥹

Can we get this done?

This is still an issue as of February 2024. unpkg.com/three (direct link https://unpkg.com/three@0.161.0/build/three.cjs) throws up a nosniffer error and is being read as "text/plain."

The cjs was added in v2.4.5 as application/node, so it should work.

I was working off of this statement when I sent the PR, but am not sure it's accurate. I don't think Chrome will treat application/node the same as application/javascript. The PR should still be merged so that .cjs files will be served according to the spec, but I'm not so sure it's actually going to help anyone

This is still an issue. As you can see with this packages: https://unpkg.com/browse/@revealbi/ui@0.2.0/

The .cjs files are marked as text/plain and will load not as javascript using a script tag

<script src="https://unpkg.com/@revealbi/ui@0.2.0/index.umd.cjs"></script>

@brianlagunas Unfortunately, it looks like unpkg hasn't been touched for about 3 years. I'm starting to lose hope, unfortunately.

It doesn't look like the SPONSORS.md file has been updated for even longer suggesting that, despite its popularity, it might not have the funding necessary to continue to be maintained.

This is all purely speculation, of course, and I hope that the project is updated and maintained going forward!

#364 was opened about a year ago and it was approved a couple months later, but there's been no movement since.

@Potherca described a workaround in the other issue (as well as above) for the time-being. Appending the .cjs URL with ?module apparently forces the correct MIME type.