Caddy Providing the Wrong MIME type for Javascript Files

1. Caddy version (caddy version):

Version: v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

caddy run

a. System environment:

Windows 10

b. Command:

caddy run

c. Service/unit/compose file:

Not applicable.

d. My complete Caddyfile or JSON config:

localhost

root * ./src
file_server

3. The problem I’m having:

When caddy runs and I go to localhost I get the following error in my Chrome web browser console:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

So it seems Caddy is giving the wrong MIME type for my Javascript file. It should give application/javascript.

What can I do?

What is the request that is being made?

Here are the request headers from Chrome:

:authority: localhost
:method: GET
:path: /app.js
:scheme: https
accept: /
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: no-cache
origin: https://localhost
pragma: no-cache
referer: https://localhost/
sec-fetch-dest: script
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36

The script is being retrieved by an index.html file with this in it:

<script src="app.js" type="module" ></script>    

How else can I help you help me?

2 Likes

Welcome Nick! We appreciate that :slight_smile:

Your config works for me, with the main difference being that I don’t use Windows – does Windows have some sort of system MIME types file that the Go standard lib can load its file associations from?

1 Like

On linux, it’s often because the mailcap package is missing which provides the MIME type lookup tables.

It looks like this is the code that fetches them for Windows, looks like it gets them via a syscall to the registry.

I’m not sure what’s going on :thinking:

Hey, I got it fixed.

Look at this open issue for the Go language: mime: incorrect mime-type for .js files on windows 10 (text/plain instead of application/javascript) · Issue #32350 · golang/go · GitHub

On the issue I found a link to this project: GitHub - zekroTJA/go-win-mime-fix: Little tool to fix golangs mime.TypeByExtension issue with js files on windows plattform

I executed that program and it fixed the issue.

Thanks for your help!

2 Likes

Fascinating! Thanks for looking into it, and for explaining the fix. I’m sure it will help others searching for the same problem.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.