Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data URI default charset

Is there a default charset for data URIs? I read the spec but I don't see one.

For instance, if I have a data URI for a source map which I expect to be reliably interpreted across browsers, is it OK to omit the charset?

//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJza...

vs

//@ sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJza...

I see in this GitHub issue that people have had problems using Chinese characters in source-mapped files without an explicit charset=utf-8. So if there is a default (or, at least, if we could expect browsers to have chosen one), it doesn't seem like utf-8 is the one...

like image 797
Jackson Avatar asked Nov 08 '22 12:11

Jackson


1 Answers

According to RFC 2397 § 2, a data URI without a specified charset defaults to US-ASCII. Because every Base64-encoded URI uses only ASCII characters. Moreover, “all US‑ASCII strings become valid UTF‑8” which means there’s “decent backwards compatibility in many cases”.1

Nevertheless, UTF-8 implementation remains uneven in 2019. Because of that – and because there is little cost to explicitly calling the charset to already-user-unfriendly data URIs – it’s probably not a bad idea to include charset=utf-8 in your sourcemap URIs in order to keep them reliably interpreted across browsers.


  1. Arjun Chaudhary’s answer to Is there a drastic difference between UTF-8 and UTF-16.
like image 186
Lucas Avatar answered Nov 24 '22 01:11

Lucas