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

feat: add js response content-type charset #48

Closed
wants to merge 1 commit into from

Conversation

whyer11
Copy link

@whyer11 whyer11 commented Jun 25, 2024

when js file use as a jsonp response, the lack of 'charset=utf-8' will lead to some text is garbled. something like below pic
image
origin text be like
image

@whyer11
Copy link
Author

whyer11 commented Jun 25, 2024

of course, when the html file doesn't have a meta tag, which set the charset type

@stephank
Copy link
Owner

stephank commented Jul 1, 2024

Ah, I understand, thanks for looking into this. I'm a little unsure about hardcoding UTF-8 here. Apparently JS source code can also use different encodings? What do you think?

@whyer11
Copy link
Author

whyer11 commented Jul 1, 2024

Ah, I understand, thanks for looking into this. I'm a little unsure about hardcoding UTF-8 here. Apparently JS source code can also use different encodings? What do you think?

Setting the appropriate Content-Type and charset in HTTP responses is crucial for ensuring that clients properly parse and display the data. For JavaScript files, the common Content-Type to use is application/javascript. Here are some key points regarding character encoding:

  1. Content-Type for JavaScript Files:

    • Typically, the Content-Type for JavaScript files should be set to application/javascript, like so:
      Content-Type: application/javascript
  2. Charset Information:

    • If your JavaScript files contain non-ASCII characters (such as Chinese, Japanese, Korean, etc.), or if you want to ensure that the client interprets the JavaScript files using a specific character encoding, you can specify the charset parameter as utf-8.
    • This might look like:
      Content-Type: application/javascript; charset=utf-8
  3. Browser Default Behavior:

    • Modern browsers are generally capable of automatically detecting and correctly handling UTF-8 encoded JavaScript files, even if the charset is not explicitly specified. However, explicitly defining the charset is still a good practice to avoid potential encoding issues.
  4. Static Asset Configuration:

    • If you are using a web server (like Nginx or Apache) to serve static files, you can typically configure the server to set the Content-Type and charset for JavaScript files globally or for specific paths.

In summary, while it is not strictly necessary for all JavaScript files to specify charset=utf-8, it is a recommended practice, especially when the content involves multiple languages or special characters. Doing so ensures that the file content is parsed and executed correctly across all clients.

@stephank
Copy link
Owner

stephank commented Jul 1, 2024

It looks like mime_guess released abonander/mime_guess#86 a couple of days ago, and if you cargo update you now get text/javascript.

I release 0.9.6 and 0.10.1 with this, and also added charset to text/javascript.

@stephank stephank closed this Jul 1, 2024
@whyer11
Copy link
Author

whyer11 commented Jul 2, 2024

It looks like mime_guess released abonander/mime_guess#86 a couple of days ago, and if you cargo update you now get text/javascript.

I release 0.9.6 and 0.10.1 with this, and also added charset to text/javascript.

Translation:

Alternatively, you can determine by the file extension and maintain an enumeration just like mime_guess to ensure it is not affected by mime_guess. The return value of mime_guess may change, so hardcoding text/javascript could indeed cause problems.

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

Successfully merging this pull request may close these issues.

2 participants