Skip to content

Commit 582ad80

Browse files
committed
Fix 400 when upstream content-type: octet-stream
1 parent 6a5279a commit 582ad80

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

packages/next/next-server/server/image-optimizer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,27 +434,27 @@ function parseCacheControl(str: string | null): Map<string, string> {
434434
* https://en.wikipedia.org/wiki/List_of_file_signatures
435435
*/
436436
function detectContentType(buffer: Buffer) {
437-
if ([0xff, 0xd8, 0xff].every((i, b) => buffer[i] === b)) {
437+
if ([0xff, 0xd8, 0xff].every((b, i) => buffer[i] === b)) {
438438
return JPEG
439439
}
440440
if (
441441
[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].every(
442-
(i, b) => buffer[i] === b
442+
(b, i) => buffer[i] === b
443443
)
444444
) {
445445
return PNG
446446
}
447-
if ([0x47, 0x49, 0x46, 0x38].every((i, b) => buffer[i] === b)) {
447+
if ([0x47, 0x49, 0x46, 0x38].every((b, i) => buffer[i] === b)) {
448448
return GIF
449449
}
450450
if (
451451
[0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50].every(
452-
(i, b) => buffer[i] === b
452+
(b, i) => !b || buffer[i] === b
453453
)
454454
) {
455455
return WEBP
456456
}
457-
if ([0x3c, 0x3f, 0x78, 0x6d, 0x6c].every((i, b) => buffer[i] === b)) {
457+
if ([0x3c, 0x3f, 0x78, 0x6d, 0x6c].every((b, i) => buffer[i] === b)) {
458458
return SVG
459459
}
460460
return null

test/integration/image-optimizer/test/index.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ function runTests({ w, isDev, domains }) {
330330
expect(res.headers.get('etag')).toBeTruthy()
331331
await expectWidth(res, w)
332332
})
333+
334+
it('should automatically detect image type when content-type is octet-stream', async () => {
335+
const url =
336+
'https://image-optimization-test.vercel.app/png-as-octet-stream'
337+
const resOrig = await fetch(url)
338+
expect(resOrig.status).toBe(200)
339+
expect(resOrig.headers.get('Content-Type')).toBe(
340+
'application/octet-stream'
341+
)
342+
const query = { url, w, q: 80 }
343+
const opts = { headers: { accept: 'image/webp' } }
344+
const res = await fetchViaHTTP(appPort, '/_next/image', query, opts)
345+
expect(res.status).toBe(200)
346+
expect(res.headers.get('Content-Type')).toBe('image/webp')
347+
expect(res.headers.get('cache-control')).toBe(
348+
'public, max-age=0, must-revalidate'
349+
)
350+
expect(res.headers.get('etag')).toBeTruthy()
351+
await expectWidth(res, w)
352+
})
333353
}
334354

335355
it('should fail when url has file protocol', async () => {
@@ -697,7 +717,11 @@ describe('Image Optimizer', () => {
697717
})
698718

699719
// domains for testing
700-
const domains = ['localhost', 'example.com']
720+
const domains = [
721+
'localhost',
722+
'example.com',
723+
'image-optimization-test.vercel.app',
724+
]
701725

702726
describe('dev support w/o next.config.js', () => {
703727
const size = 384 // defaults defined in server/config.ts

0 commit comments

Comments
 (0)