mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
Fix: Infer content type with charset in dev and prod (#3841)
* fix: add text/plain;charset;utf-8 header in dev * test: ensure content type for body shorthand * chore: changeset * feat: infer content type by pathname * feat: add charset to prod build handler * test: update for charset in prod build test
This commit is contained in:
parent
b2f53c37c5
commit
820a26dde5
4 changed files with 23 additions and 3 deletions
5
.changeset/ten-radios-rush.md
Normal file
5
.changeset/ten-radios-rush.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix: add default content type to endpoints with { body } shorthand
|
|
@ -149,7 +149,9 @@ export class App {
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
const mimeType = mime.getType(url.pathname);
|
const mimeType = mime.getType(url.pathname);
|
||||||
if (mimeType) {
|
if (mimeType) {
|
||||||
headers.set('Content-Type', mimeType);
|
headers.set('Content-Type', `${mimeType};charset=utf-8`);
|
||||||
|
} else {
|
||||||
|
headers.set('Content-Type', 'text/plain;charset=utf-8');
|
||||||
}
|
}
|
||||||
const bytes = this.#encoder.encode(body);
|
const bytes = this.#encoder.encode(body);
|
||||||
headers.set('Content-Length', bytes.byteLength.toString());
|
headers.set('Content-Length', bytes.byteLength.toString());
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import type http from 'http';
|
import type http from 'http';
|
||||||
import type * as vite from 'vite';
|
import type * as vite from 'vite';
|
||||||
|
import mime from 'mime';
|
||||||
import type { AstroConfig, ManifestData } from '../@types/astro';
|
import type { AstroConfig, ManifestData } from '../@types/astro';
|
||||||
import type { SSROptions } from '../core/render/dev/index';
|
import type { SSROptions } from '../core/render/dev/index';
|
||||||
|
|
||||||
|
@ -315,7 +316,12 @@ async function handleRequest(
|
||||||
if (result.type === 'response') {
|
if (result.type === 'response') {
|
||||||
await writeWebResponse(res, result.response);
|
await writeWebResponse(res, result.response);
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(200);
|
let contentType = 'text/plain';
|
||||||
|
const computedMimeType = route.pathname ? mime.getType(route.pathname) : null;
|
||||||
|
if (computedMimeType) {
|
||||||
|
contentType = computedMimeType;
|
||||||
|
}
|
||||||
|
res.writeHead(200, { 'Content-Type': `${contentType};charset=utf-8` });
|
||||||
res.end(result.body);
|
res.end(result.body);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -30,7 +30,7 @@ describe('API routes in SSR', () => {
|
||||||
const request = new Request('http://example.com/food.json');
|
const request = new Request('http://example.com/food.json');
|
||||||
const response = await app.render(request);
|
const response = await app.render(request);
|
||||||
expect(response.status).to.equal(200);
|
expect(response.status).to.equal(200);
|
||||||
expect(response.headers.get('Content-Type')).to.equal('application/json');
|
expect(response.headers.get('Content-Type')).to.equal('application/json;charset=utf-8');
|
||||||
expect(response.headers.get('Content-Length')).to.not.be.empty;
|
expect(response.headers.get('Content-Length')).to.not.be.empty;
|
||||||
const body = await response.json();
|
const body = await response.json();
|
||||||
expect(body.length).to.equal(3);
|
expect(body.length).to.equal(3);
|
||||||
|
@ -56,6 +56,13 @@ describe('API routes in SSR', () => {
|
||||||
expect(text).to.equal(`ok`);
|
expect(text).to.equal(`ok`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Infer content type with charset for { body } shorthand', async () => {
|
||||||
|
const response = await fixture.fetch('/food.json', {
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
expect(response.headers.get('Content-Type')).to.equal('application/json;charset=utf-8');
|
||||||
|
});
|
||||||
|
|
||||||
it('Can set multiple headers of the same type', async () => {
|
it('Can set multiple headers of the same type', async () => {
|
||||||
const response = await fixture.fetch('/login', {
|
const response = await fixture.fetch('/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
Loading…
Add table
Reference in a new issue