Skip to content

Commit 7998b63

Browse files
authored
Fix missing Content-Length header from Image Optimization API (#36581)
Fixes #35514
1 parent c838b5f commit 7998b63

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/next/server/image-optimizer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ export function sendResponse(
621621
contentSecurityPolicy
622622
)
623623
if (!result.finished) {
624+
res.setHeader('Content-Length', Buffer.byteLength(buffer))
624625
res.end(buffer)
625626
}
626627
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ export async function fsToJson(dir, output = {}) {
7070
return output
7171
}
7272

73-
export async function expectWidth(res, w) {
73+
export async function expectWidth(res, w, { expectAnimated = false } = {}) {
7474
const buffer = await res.buffer()
7575
const d = sizeOf(buffer)
7676
expect(d.width).toBe(w)
77+
const lengthStr = res.headers.get('Content-Length')
78+
expect(lengthStr).toBe(Buffer.byteLength(buffer).toString())
79+
expect(isAnimated(buffer)).toBe(expectAnimated)
7780
}
7881

7982
export const cleanImagesDir = async (ctx) => {
@@ -170,7 +173,7 @@ export function runTests(ctx) {
170173
expect(res.headers.get('Content-Disposition')).toBe(
171174
`inline; filename="animated.gif"`
172175
)
173-
expect(isAnimated(await res.buffer())).toBe(true)
176+
await expectWidth(res, 50, { expectAnimated: true })
174177
})
175178

176179
it('should maintain animated png', async () => {
@@ -186,7 +189,7 @@ export function runTests(ctx) {
186189
expect(res.headers.get('Content-Disposition')).toBe(
187190
`inline; filename="animated.png"`
188191
)
189-
expect(isAnimated(await res.buffer())).toBe(true)
192+
await expectWidth(res, 100, { expectAnimated: true })
190193
})
191194

192195
it('should maintain animated png 2', async () => {
@@ -202,7 +205,7 @@ export function runTests(ctx) {
202205
expect(res.headers.get('Content-Disposition')).toBe(
203206
`inline; filename="animated2.png"`
204207
)
205-
expect(isAnimated(await res.buffer())).toBe(true)
208+
await expectWidth(res, 1105, { expectAnimated: true })
206209
})
207210

208211
it('should maintain animated webp', async () => {
@@ -218,7 +221,7 @@ export function runTests(ctx) {
218221
expect(res.headers.get('Content-Disposition')).toBe(
219222
`inline; filename="animated.webp"`
220223
)
221-
expect(isAnimated(await res.buffer())).toBe(true)
224+
await expectWidth(res, 400, { expectAnimated: true })
222225
})
223226

224227
if (ctx.dangerouslyAllowSVG) {
@@ -227,6 +230,7 @@ export function runTests(ctx) {
227230
const opts = { headers: { accept: 'image/webp' } }
228231
const res = await fetchViaHTTP(ctx.appPort, '/_next/image', query, opts)
229232
expect(res.status).toBe(200)
233+
expect(res.headers.get('Content-Length')).toBe('603')
230234
expect(res.headers.get('Content-Type')).toContain('image/svg+xml')
231235
expect(res.headers.get('Cache-Control')).toBe(
232236
`public, max-age=0, must-revalidate`
@@ -329,6 +333,7 @@ export function runTests(ctx) {
329333
expect(res.headers.get('Content-Disposition')).toBe(
330334
`inline; filename="test.jpeg"`
331335
)
336+
await expectWidth(res, ctx.w)
332337
})
333338

334339
if (!ctx.isOutdatedSharp) {
@@ -348,6 +353,7 @@ export function runTests(ctx) {
348353
expect(res.headers.get('Content-Disposition')).toBe(
349354
`inline; filename="test.jpeg"`
350355
)
356+
await expectWidth(res, ctx.w)
351357
})
352358
}
353359

0 commit comments

Comments
 (0)