Skip to content

Instantly share code, notes, and snippets.

@eprev
Last active January 4, 2017 09:41
Show Gist options
  • Save eprev/322cd355319483aaaebbb2da35052281 to your computer and use it in GitHub Desktop.
Save eprev/322cd355319483aaaebbb2da35052281 to your computer and use it in GitHub Desktop.

Revisions

  1. eprev revised this gist Jan 4, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions charset.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    'use strict';

    // See http://eprev.org/2017/01/04/the-importance-of-html-character-encoding/

    const http = require('http');

    http.createServer((request, response) => {
  2. eprev created this gist Jan 3, 2017.
    46 changes: 46 additions & 0 deletions charset.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    'use strict';
    const http = require('http');

    http.createServer((request, response) => {
    let charset;
    switch (request.url) {
    case '/charset-is-specified':
    charset = 'utf-8';
    case '/charset-is-not-specified':
    response.writeHead(200, {
    'Content-Type': charset ? 'text/html; charset=utf-8' : 'text/html',
    });
    // Early-head
    response.write(`
    <!doctype html>
    <html><head>
    <title>Charset Encoding Test</title>
    <script src="/scripts.js" async onload="console.log({scripts: performance.now()})"></script>
    <link href="/styles.css" rel="stylesheet" onload="console.log({styles: performance.now()})">
    <script>document.addEventListener('DOMContentLoaded', () => console.log({DOMContentLoaded: performance.now()}))</script>
    </head>
    `);
    // The rest of the document
    setTimeout(() => {
    response.end('<body></body></html>');
    }, 1000);
    break;
    case '/scripts.js':
    response.writeHead(200, {
    'Content-Type': 'application/javascript',
    });
    response.end();
    break;
    case '/styles.css':
    response.writeHead(200, {
    'Content-Type': 'text/css',
    });
    response.end('');
    break;
    default:
    response.writeHead(404, {
    'Content-Type': 'text/plain',
    });
    response.end();
    }
    }).listen(4000);