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

[Bug]: mjs file extension for browser code is not working in nginx #20157

Closed
eirslett-visma opened this issue Dec 8, 2022 · 19 comments
Closed

Comments

@eirslett-visma
Copy link

eirslett-visma commented Dec 8, 2022

Describe the bug

This bug applies to Storybook 7, when serving the finished build of Storybook.

With some servers (nginx for example) the .mjs files (manager.mjs, runtime.mjs) are served to the browser with content-type application/octet-stream instead of text/javascript.

One could make a point that this is a bug in the HTTP servers, and the way to fix it is to tweak MIME types so that mjs and cjs are identified as text/javascript. However, the issues could also be fixed in Storybook by serving the manager and runtime with a .js extension.

To Reproduce

1) `npx sb@next repro`
2) Choose React -> React as a template
3) `cd test-repro`
4) `yarn run build-storybook`
5) Serve the assets from nginx, for example on MacOS:


docker run -it --rm -p 9000:80 --name web -v $(pwd)/storybook-static:/usr/share/nginx/html nginx
  1. Open the browser (http://localhost:9000)
  2. The page should be blank, with errors in the console:
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.

System

Environment Info:

  System:
    OS: macOS 12.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 3.3.0 - /usr/local/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
  Browsers:
    Chrome: 108.0.5359.94
    Firefox: 107.0.1
    Safari: 16.0
  npmPackages:
    @storybook/addon-essentials: ^7.0.0-beta.0 => 7.0.0-beta.0 
    @storybook/addon-interactions: ^7.0.0-beta.0 => 7.0.0-beta.0 
    @storybook/addon-links: ^7.0.0-beta.0 => 7.0.0-beta.0 
    @storybook/blocks: ^7.0.0-beta.0 => 7.0.0-beta.0 
    @storybook/react: ^7.0.0-beta.0 => 7.0.0-beta.0 
    @storybook/react-webpack5: ^7.0.0-beta.0 => 7.0.0-beta.0 
    @storybook/testing-library: ^0.0.13 => 0.0.13

Additional context

No response

@stefanetoh
Copy link

Had the same issue and could fix it in the nginx conf by adding:

  include mime.types;
  types {
      application/javascript js mjs;
  }

if it helps anyone :)

@ndelangen ndelangen self-assigned this Dec 14, 2022
@ndelangen
Copy link
Member

We could fix this the proper way, or the dirty way (or not at all).

Dirty way:
We keep generating .mjs files, but when copying files we rename them.
A HUGE problem with this method is that it's not just a few files to rename, it's also the files contents, because they make references to each other via relative paths, WITH the file extension (because that's required in ESM / browser environments.
We'd ALSO need to add smart rewrite rules for express.

Proper way:
We generate ESM as .js and CJS as .cjs. This would require us to change lib/preview to be type=module in package.json.
The changes to the bundling code are moderate, but I've experimented, and it's an easy extension to do.

I think doing the proper option is too high risk for 7.0 beta. In fact I'd rather not mess with mixing type=module at all, and would rather do a big bang switch everywhere.

The dirty option is an big no-no for me personally, Search-and-replace in source-code is a no-no.

SO with all that in mind, My recommendation would be to: not support this.

mjs is a valid file-extention. We generate it to avoid having commonjs/esm loading issues within node/etc.

I'm curious about your thoughts @tmeasday @kasperpeulen

@tmeasday
Copy link
Member

I'm inclined to agree with your analysis @ndelangen.

@ndelangen
Copy link
Member

@shilman if you also agree, I propose we close this issue, or move it from the 7.0 project board

@shilman
Copy link
Member

shilman commented Dec 15, 2022

Shiver me timbers!! I just released https://github.com/storybookjs/storybook/releases/tag/v7.0.0-beta.9 containing PR #20277 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb upgrade --prerelease

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Dec 15, 2022
Repository owner moved this from Nice to have to Done in Core Team Projects Dec 15, 2022
@MineDrum
Copy link

Still coming across this issue in Storybook 7.0.0-beta.24

@ndelangen
Copy link
Member

@MineDrum yes, that's to be expected, the resolution we ended up on is:

My recommendation would be to: not support this.

You'll need to add .mjs support to your http-server for now.

@mariohs22
Copy link

mariohs22 commented Mar 11, 2023

Still coming across this issue in Storybook 7.0.0-beta.60. Thanks to @stefanetoh. My docker solution:

FROM nginxinc/nginx-unprivileged:1.23-alpine
RUN echo "types { application/javascript js mjs; }" > /etc/nginx/conf.d/mjs.conf
COPY --chown=nginx:nginx storybook-static /usr/share/nginx/html
EXPOSE 8080
ENTRYPOINT ["nginx", "-g", "daemon off"]

@FilipSwiatczak
Copy link

FilipSwiatczak commented Mar 21, 2023

Does this mean it can't be hosted in aws s3 as a static website anymore? I don't think s3 supports .mjs. It's a bit of a pain honestly

Mine is throwing in console:
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.

@redonkulus
Copy link

Also a pain for serving storybook off of github pages that do not have an updated mime-db package. There is no way to configure mime types in github pages, so we cannot serve .mjs files properly.

It would be nice if there was an option to transpile to .js.

@ndelangen
Copy link
Member

@redonkulus perhaps you could write to the GitHub support forum?

@soullivaneuh
Copy link

Had the same issue and could fix it in the nginx conf by adding:

  include mime.types;
  types {
      application/javascript js mjs;
  }

if it helps anyone :)

If you add that, you will get the following nginx warning:

[warn] 1#1: duplicate extension "js", content type: "application/javascript", previous content type: "application/javascript" in /etc/nginx/conf.d/nginx.conf:2

Also, include mime.types; directive generate tons of another duplicate like above, so I am not sure this is a good practice.

For my case, just specifying the mjs extension is enough without producing any warning:

types {
    application/javascript mjs;
}

@eunukasiko
Copy link

Depends on your setup. Anyways, should be using text/javascript

@russellmcc
Copy link

There are a lot of servers that don't serve .mjs files with the correct mime type by default, so it would be a really nice feature to be able to configure this to create .js files

@eunukasiko
Copy link

Yes, for example Bitbucket Web Pages don't serve it with a javascript mime type.

@Hecsall
Copy link

Hecsall commented Apr 17, 2023

Here to point out that even Salesforce Commerce Cloud doesn't (and can't) support serving .mjs with text/javascript MIME Type.

@denchen
Copy link

denchen commented Apr 18, 2023

I'm not sure this is the same issue, but I'm using @storybook/react-vite and I deployed to Github Enterprise Pages, and I'm getting a MIME type of text/plain.

@jsalis
Copy link

jsalis commented May 2, 2023

I have this workaround for deploying to s3 with bitbucket pipelines. The first pipe uploads everything except mjs files, and the second pipe uploads only the mjs files while setting the correct content-type.

image: node:16.16.0
pipelines:
  branches:
    master:
      - step:
          name: Build
          caches:
            - node
          script:
            - npm install
            - npm run build-storybook
          artifacts:
            - storybook-static/**
      - step:
          deployment: production
          name: Production
          script:
            - pipe: atlassian/aws-s3-deploy:0.3.7
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_KEY
                AWS_DEFAULT_REGION: "us-east-1"
                S3_BUCKET: "bucket-name"
                LOCAL_PATH: "storybook-static"
                DELETE_FLAG: "true"
                EXTRA_ARGS: "--exclude=*.mjs"
            - pipe: atlassian/aws-s3-deploy:0.3.7
              variables:
                AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY
                AWS_SECRET_ACCESS_KEY: $AWS_SECRET_KEY
                AWS_DEFAULT_REGION: "us-east-1"
                S3_BUCKET: "bucket-name"
                LOCAL_PATH: "storybook-static"
                EXTRA_ARGS: "--exclude=* --include=*.mjs --no-guess-mime-type --content-type=application/javascript"

@NazarianCarpet
Copy link

NazarianCarpet commented Feb 3, 2024

For truenas user
in nextcloud Jails do this
1-go to shell
2- cd ..
3- ee /usr/local/etc/nginx/mime.types
4- in line 32or ... add>
application/javascript mjs mjs js;
5- save
6-restart nexcloud

Untitled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests