Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Setting text/javascript encoding to utf8 with Jetty 10.0.16

Thank you Joakim for the extensive answer, however -

On Tue, Oct 3, 2023 at 2:23 PM Joakim Erdfelt via jetty-users <jetty-users@xxxxxxxxxxx> wrote:
Per the _javascript_ RFC, the `text/_javascript_` mime-type has an optional `charset` parameter.

Essentially, if the charset is unspecified, then the encoding is UTF-8.

> the encoding is unfortunately not set to utf8 (like it is for the served json files).

For JSON, the charset parameter is not used.
Per spec, JSON is always UTF-8.

In Jetty, the json encoding is specified as an assumed UTF-8.
This means the `charset` parameter is not produced when generating the `Content-Type` header, and is ignored when parsing the `Content-Type` header.

 > Is there a way to enforce that without compiling a custom version of Jetty?

You can customize the in-place `MimeTypes` for a context.

servletContextHandler.getMimeTypes().addMimeMapping("txt", "text/_javascript_;charset=UTF-8");
webappContext.getMimeTypes().addMimeMapping("txt", "text/_javascript_;charset=UTF-8");

or, If you have a WEB-INF/web.xml in your webapp, you can add a `<mime-mapping>` entry.


I have problems that my UTF8 file is displayed wrongly in the browser -

The browser displays pairs of junk characters instead of cyrillic letters and from my (limited) experience having ";charset=utf8" in the header would help it.

So I have followed your suggestion and have extended the src/main/webapp/WEB-INF/web.xml with:

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="false" 

Unfortunately the URL still returns the headers without "UTF-8" in there:

HTTP/1.1 200 OK
last-modified: Tue, 03 Oct 2023 09:52:40 GMT
content-type: text/_javascript_
accept-ranges: bytes
vary: Accept-Encoding
content-encoding: gzip
server: Jetty(10.0.16)
connection: close

Best regards

Back to the top