diff --git a/.changeset/honest-pugs-hang.md b/.changeset/honest-pugs-hang.md new file mode 100644 index 000000000..a1c5cb168 --- /dev/null +++ b/.changeset/honest-pugs-hang.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix returning hex / base64 images from endpoints not working in dev diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 5bd551ca9..85643034f 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -200,7 +200,7 @@ export async function handleRoute( if (computedMimeType) { contentType = computedMimeType; } - const response = new Response(result.body, { + const response = new Response(Buffer.from(result.body, result.encoding), { status: 200, headers: { 'Content-Type': `${contentType};charset=utf-8`, diff --git a/packages/astro/test/dev-routing.test.js b/packages/astro/test/dev-routing.test.js index e7e5c82c5..c0f87375a 100644 --- a/packages/astro/test/dev-routing.test.js +++ b/packages/astro/test/dev-routing.test.js @@ -270,6 +270,15 @@ describe('Development Routing', () => { const response = await fixture.fetch('/images/1.svg'); expect(response.headers.get('content-type')).to.match(/image\/svg\+xml/); }); + + it('correct encoding when loading /images/hex.ts', async () => { + const response = await fixture.fetch('/images/hex'); + const body = await response.arrayBuffer(); + const hex = Buffer.from(body).toString('hex', 0, 4); + + // Check if we have a PNG + expect(hex).to.equal('89504e47'); + }); }); describe('file format routing', () => { diff --git a/packages/astro/test/fixtures/with-endpoint-routes/src/astro.png b/packages/astro/test/fixtures/with-endpoint-routes/src/astro.png new file mode 100644 index 000000000..115453790 Binary files /dev/null and b/packages/astro/test/fixtures/with-endpoint-routes/src/astro.png differ diff --git a/packages/astro/test/fixtures/with-endpoint-routes/src/pages/images/hex.ts b/packages/astro/test/fixtures/with-endpoint-routes/src/pages/images/hex.ts new file mode 100644 index 000000000..546796fed --- /dev/null +++ b/packages/astro/test/fixtures/with-endpoint-routes/src/pages/images/hex.ts @@ -0,0 +1,9 @@ +import { readFileSync } from "node:fs"; + +export async function get({ params, request }) { + const buffer = readFileSync(new URL('../../astro.png', import.meta.url)); + return { + body: buffer.toString('hex'), + encoding: 'hex', + }; +}