Skip to content

Commit

Permalink
Fix immutable header on static image (#25914)
Browse files Browse the repository at this point in the history
This PR fixes a bug where the first request has the correct immutable header but subsequent requests do not.

Depends on #25909
  • Loading branch information
styfle committed Jun 8, 2021
1 parent 50a36ee commit a79eb4f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/next/next-server/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ export async function imageOptimizer(
const contentType = getContentType(extension)
const fsPath = join(hashDir, file)
if (now < expireAt) {
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate')
res.setHeader(
'Cache-Control',
isStatic
? 'public, max-age=315360000, immutable'
: 'public, max-age=0, must-revalidate'
)
if (sendEtagResponse(req, res, etag)) {
return { finished: true }
}
Expand Down
16 changes: 13 additions & 3 deletions test/integration/image-optimizer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,21 @@ function runTests({ w, isDev, domains }) {
q: 100,
}
const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(appPort, '/_next/image', query, opts)
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(

const res1 = await fetchViaHTTP(appPort, '/_next/image', query, opts)
expect(res1.status).toBe(200)
expect(res1.headers.get('cache-control')).toBe(
'public, max-age=315360000, immutable'
)
await expectWidth(res1, w)

// Ensure subsequent request also has immutable header
const res2 = await fetchViaHTTP(appPort, '/_next/image', query, opts)
expect(res2.status).toBe(200)
expect(res2.headers.get('cache-control')).toBe(
'public, max-age=315360000, immutable'
)
await expectWidth(res2, w)
}
})

Expand Down

0 comments on commit a79eb4f

Please sign in to comment.