Vscode-live-server: About of 'gbk' charset

Created on 15 Sep 2017  ·  19Comments  ·  Source: ritwickdey/vscode-live-server

while the html is 'gbk' charset, the preview will not work well, the chinese word do not display correctly;

bug

Most helpful comment

Okay it turns out that it's due to Node itself not able to serve GBK/GB2312 encoded files, or more precisely, send module messing content-type due to mime module it uses: https://github.com/pillarjs/send/blob/f60b775d4736635046ef25708b9493d215f05ff0/index.js#L845 maps application/javascript to UTF-8 in a quite rudimentary way.

One liner is to define new mappings in https://github.com/ritwickdey/vscode-live-server/blob/master/lib/live-server/index.js#L50, in the way described by https://github.com/pillarjs/send, e.g.

send.mime.define({
 'application/javascript': ['gb2312']
})

, which is locally modifiable as long as you locate the extension folder on your machine and this file is in %ext_folder%/node_modules/live-server.

Not sure how author wanna tackle this, but anyone who has similar issue should be able to proceed now.

All 19 comments

Sorry! I don't know about gbk' charset. Can you give me any link where it is described properly ?

Hi, Can you give a HTML file where you're facing issue in 'gbk' charset.

  1. first create a file like index.html
  2. Click right bottom UTF-8
    image

  3. Save/Reopen with Encoding (Save/Reopen both ok)
    image

  4. choose Gb2312
    image

  5. copy this text and insert to your file's body

  <p>一些中文文字</p>
  <p>我非常喜欢你写的插件</p>
  <p>并且我对于你的更新速度表示非常佩服</p>
  <p>很抱歉,让你艰难的理解我糟糕的英语</p>
  <p>this is cause nodejs doesn't support gbk charset</p>
  <p>I don't think you need to support charset of gbk</p>
  <p>Tips there's a npm package iconv-lite be good at handle different charset</p>
  <a href="https://www.npmjs.com/package/iconv-lite">npm iconv-lite page</a>
  1. open liveServer
  2. tata messy code

Hi, @bestvow , Is this not working properly ?

seriously you don't need fix this problem (^-^)
image

Lol 😄 ....

Okay, I tried. but it is working fine in utf-8. Why it is needed to convert HTML to gbk?

image

@johonz
I'm closing this for now, because we haven't received any response. Feel free to tag me and I will reopen this issue.

Sorry I have to reopen this issue because I'm encountering the same problem.
the reason why it does not work well is because the live server convert all the data it reads into UTF-8 and responds to the client browser with HTTP header "Content-Type: text/htm; charset=UTF-8" which is unnecessary (charset=UTF-8), because the HTML contains meta to sepcify which charset will be used to display content already. Or perhaps there is option to turn this off already?
qq 20180427134529

For the reason "Why it is needed to convert HTML to gbk?":
for a production, we need to display as much correct Chinese character as possibile, the UTF8 / GB2312 charset does not contain some uncommon Chinese character in it but it does in GBK charset.
for more detail you can refer to:
https://en.wikipedia.org/wiki/GBK_(character_encoding)#Encoding
https://en.wikipedia.org/wiki/GB_2312#Encodings_of_GB2312
https://en.wikipedia.org/wiki/UTF-8#Byte_order_mark

@ritwickdey @mxschmitt

@jcyuan Already showing Chinese characters on our end. Can you give us some screenshots?

Closing this issue. Reopen it if anyone found this issue further

@rjoydip Sorry to reopen this, below is a file encoded in GB2312 and serving it from live-server will show garbled text because live-server responds with Content-Type: application/javascript; charset=UTF-8, and charset is unnecessary as described above.

js_box.zip

Please instruct how I could disable that extra charset, thanks!

@mxschmitt Friendly ping.

@ritwickdey @rjoydip Friendly ping for any update.

@ritwickdey @rjoydip Friendly ping for any update again.

could live-server give a charset in settings?
to change all response charset, and default is utf8

@thiled I'm not too familiar with vscode plugin development and couldn't find how it's being forced in this repo. Otherwise I'd have changed it myself.

Hopefully author could get some time to take a look at this issue as this plugin is quite useful.

Okay it turns out that it's due to Node itself not able to serve GBK/GB2312 encoded files, or more precisely, send module messing content-type due to mime module it uses: https://github.com/pillarjs/send/blob/f60b775d4736635046ef25708b9493d215f05ff0/index.js#L845 maps application/javascript to UTF-8 in a quite rudimentary way.

One liner is to define new mappings in https://github.com/ritwickdey/vscode-live-server/blob/master/lib/live-server/index.js#L50, in the way described by https://github.com/pillarjs/send, e.g.

send.mime.define({
 'application/javascript': ['gb2312']
})

, which is locally modifiable as long as you locate the extension folder on your machine and this file is in %ext_folder%/node_modules/live-server.

Not sure how author wanna tackle this, but anyone who has similar issue should be able to proceed now.

Was this page helpful?
0 / 5 - 0 ratings