Skip to content

Commit

Permalink
fix(images): Return the proper content-type for the chosen format (#6741
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Princesseuh authored Apr 3, 2023
1 parent 73fcc76 commit 4c347ab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-ladybugs-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix content-type header being wrong in dev on images from `astro:assets`
4 changes: 2 additions & 2 deletions packages/astro/src/assets/image-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mime from 'mime';
import mime from 'mime/lite.js';
import type { APIRoute } from '../@types/astro.js';
import { isRemotePath } from '../core/path.js';
import { getConfiguredImageService } from './internal.js';
Expand Down Expand Up @@ -54,7 +54,7 @@ export const get: APIRoute = async ({ request }) => {
return new Response(data, {
status: 200,
headers: {
'Content-Type': mime.getType(format) || '',
'Content-Type': mime.getType(format) ?? `image/${format}`,
'Cache-Control': 'public, max-age=31536000',
ETag: etag(data.toString()),
Date: new Date().toUTCString(),
Expand Down
7 changes: 2 additions & 5 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bold } from 'kleur/colors';
import MagicString from 'magic-string';
import mime from 'mime';
import mime from 'mime/lite.js';
import fs from 'node:fs/promises';
import { Readable } from 'node:stream';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -133,10 +133,7 @@ export default function assets({
format = result.format;
}

res.setHeader(
'Content-Type',
mime.getType(fileURLToPath(filePathURL)) || `image/${format}`
);
res.setHeader('Content-Type', mime.getType(format) ?? `image/${format}`);
res.setHeader('Cache-Control', 'max-age=360000');

const stream = Readable.from(data);
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ describe('astro:image', () => {
expect(res.status).to.equal(200);
});

it('returns proper content-type', async () => {
let $img = $('#local img');
let src = $img.attr('src');
let res = await fixture.fetch(src);
expect(res.headers.get('content-type')).to.equal('image/webp');
});

it('errors on unsupported formats', async () => {
logs.length = 0;
let res = await fixture.fetch('/unsupported-format');
Expand Down

0 comments on commit 4c347ab

Please sign in to comment.