From 3d47221f679aafd6768bb1a9d9294a8c3dde6d51 Mon Sep 17 00:00:00 2001 From: Hugo Tiburtino <45924645+hugotiburtino@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:05:19 +0100 Subject: [PATCH] fix(asset-proxy): accept compressed images as it is common for svg --- __tests__/asset-proxy.ts | 24 +++++++++++++++++++++++- src/asset-proxy.ts | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/__tests__/asset-proxy.ts b/__tests__/asset-proxy.ts index 4c745961..09d55227 100644 --- a/__tests__/asset-proxy.ts +++ b/__tests__/asset-proxy.ts @@ -1,4 +1,4 @@ -import { http } from 'msw' +import { bypass, http } from 'msw' import { currentTestEnvironment, @@ -21,6 +21,13 @@ beforeEach(() => { headers: { 'content-type': 'application/json' }, }) }), + // it would be better to internally fake the response, but for some reason msw does not handle Content-Encoding header correctly + http.get( + 'https://upload.wikimedia.org/wikipedia/commons/8/8b/Sinus_mit_y.svg', + async ({ request }) => { + return await fetch(bypass(new Request(request.url, request))) + }, + ), ) }) @@ -38,6 +45,21 @@ test('request to https://asset-proxy.serlo.org/image?url=* gets asset from url q ) }) +test('request to asset-proxy works also in case of other encodings', async () => { + const env = currentTestEnvironment() + const response = await env.fetch({ + subdomain: 'asset-proxy', + pathname: + '/image?url=https://upload.wikimedia.org/wikipedia/commons/8/8b/Sinus_mit_y.svg', + }) + expect(response.status).toBe(200) + expect(response.headers.get('content-type')).toBe('image/svg+xml') + expect(response.headers.get('Set-Cookie')).toBeNull() + expect(response.headers.get('cache-control')).toBe( + 'public, max-age=31536000, immutable', + ) +}) + describe('returns placeholder', () => { test('when url parameter is empty', async () => { const response = await requestAsset('') diff --git a/src/asset-proxy.ts b/src/asset-proxy.ts index 496269fb..72427b3e 100644 --- a/src/asset-proxy.ts +++ b/src/asset-proxy.ts @@ -20,6 +20,7 @@ export async function assetProxy(request: Request): Promise { const originalResponse = await fetch(assetUrl, { cf: { cacheTtl: 24 * 60 * 60 * 30 }, + headers: { 'Accept-Encoding': '*' }, }) if (originalResponse.ok && isImageResponse(originalResponse)) {