ensure utf8 encoding when serving html (#2654)

* ensure utf8 encoding on servers

* Create spicy-tomatoes-act.md

* Update spicy-tomatoes-act.md

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Fred K. Schott 2022-02-24 12:39:41 -08:00 committed by GitHub
parent 2e5c3b5126
commit a0fc5cb5ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fix an issue where utf8 encoding was skipped in the dev server.

View file

@ -18,7 +18,8 @@ async function handle(req, res) {
const html = await app.render(req, route);
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': Buffer.byteLength(html, 'utf-8'),
});
res.end(html);
} else if (/^\/api\//.test(req.url)) {

View file

@ -3,7 +3,6 @@ import type http from 'http';
import type { AstroConfig, ManifestData, RouteData } from '../@types/astro';
import { info, LogOptions } from '../core/logger.js';
import { createRouteManifest, matchRoute } from '../core/routing/index.js';
import mime from 'mime';
import stripAnsi from 'strip-ansi';
import { createSafeError } from '../core/util.js';
import { ssr } from '../core/render/dev/index.js';
@ -30,8 +29,8 @@ function removeViteHttpMiddleware(server: vite.Connect.Server) {
function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: string) {
res.writeHead(statusCode, {
'Content-Type': mime.getType('.html') as string,
'Content-Length': Buffer.byteLength(html, 'utf8'),
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': Buffer.byteLength(html, 'utf-8'),
});
res.write(html);
res.end();