Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(images): Return the proper content-type for the chosen format #6741

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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