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

The serveDirectory middleware sends JS files as application/javascript (3.0.0-RC7) #2855

Closed
denisftw opened this issue May 19, 2024 · 5 comments · Fixed by #2890
Closed

The serveDirectory middleware sends JS files as application/javascript (3.0.0-RC7) #2855

denisftw opened this issue May 19, 2024 · 5 comments · Fixed by #2890
Labels
💎 Bounty bug Something isn't working 💰 Rewarded

Comments

@denisftw
Copy link

denisftw commented May 19, 2024

Describe the bug
The serveDirectory middleware sends JS files as application/javascript, whereas i.e caddy working as a file server sends them as text/javascript; charset=utf-8.

To Reproduce
Steps to reproduce the behaviour:

  1. Create a directory called public with at least one JavaScript file
  2. Append the following clause to the routes: Middleware.serveDirectory(Path.empty, new File("public"))
  3. Open Browser DevTools and somehow make the browser download the JS file (i.e, reference from HTML)
  4. Observe the errors in console (see below)
  5. Alternatively, run caddy as caddy file-server --listen :9000 --root public/

Expected behaviour
It seems that the main difference between Caddy and ZIO HTTP is content-type. I suspect that if ZIO HTTP sends the same content type, the browser will execute JS without a problem.

Screenshots
See the error in the attached screenshot.
character-error
zio-content-type
caddy-content-type

Desktop (please complete the following information):

  • OS: Arch Linux
  • Browser: LibreWolf
  • Version: 125.0.3-1

Additional context
The JS in question is a bundle that contains JSX and all kinds of stuff, but I suspect the problem might occur with any JS file. I'm using ZIO HTTP 3.0.0-RC7.

@denisftw denisftw added the bug Something isn't working label May 19, 2024
@denisftw
Copy link
Author

It seems that the main issue is the absence of the charset=utf-8 part. If I use the example code from your Trailing Path Example and "patch" the header with .addHeader(Header.ContentType.apply(MediaType.application.javascript, None, Some(Charsets.Utf8))) character class errors disappear.

@jdegoes
Copy link
Member

jdegoes commented Jun 4, 2024

/bounty $50

Copy link

algora-pbc bot commented Jun 4, 2024

💎 $50 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #2855 with your implementation plan
  2. Submit work: Create a pull request including /claim #2855 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @987Nabil #2890

Copy link

algora-pbc bot commented Jun 6, 2024

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented Jun 8, 2024

🎉🎈 @987Nabil has been awarded $50! 🎈🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty bug Something isn't working 💰 Rewarded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants