Skip to content

Commit

Permalink
fix(asset-proxy): accept compressed images as it is common for svg
Browse files Browse the repository at this point in the history
  • Loading branch information
hugotiburtino committed Nov 20, 2024
1 parent 5b64549 commit 3d47221
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion __tests__/asset-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http } from 'msw'
import { bypass, http } from 'msw'

import {
currentTestEnvironment,
Expand All @@ -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)))
},
),
)
})

Expand All @@ -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('')
Expand Down
1 change: 1 addition & 0 deletions src/asset-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function assetProxy(request: Request): Promise<Response | null> {

const originalResponse = await fetch(assetUrl, {
cf: { cacheTtl: 24 * 60 * 60 * 30 },
headers: { 'Accept-Encoding': '*' },
})

if (originalResponse.ok && isImageResponse(originalResponse)) {
Expand Down

0 comments on commit 3d47221

Please sign in to comment.