Unverified Commit 2373320f authored by Steven's avatar Steven Committed by GitHub

Add upstream `max-age` to optimized image (#26739)

This solves the main use case from Issue #19914.

Previously, we would set the `Cache-Control` header to a constant and rely on the server cache. This would mean the browser would always request the image and the server could response with 304 Not Modified to omit the response body.

This PR changes the behavior such that the `max-age` will propagate from the upstream server to the Next.js Image Optimization Server and allow browser caching. ("upstream" meaning external server or just an internal route to an image)

This PR does not change the `max-age` for static imports which will remain `public, max-age=315360000, immutable`.

#### Pros:
- Fewer HTTP requests after initial browser visit
- User configurable `max-age` via the upstream image `Cache-Control` header

#### Cons:
- ~~Might be annoying for `next dev` when modifying a source image~~ (solved: use `max-age=0` for dev)
- Might cause browser to cache longer than expected (up to 2x longer than the server cache if requested in the last second before expiration)

## Bug

- [x] Related issues linked using `fixes #number`
parent b046a059
...@@ -22,7 +22,7 @@ const PNG = 'image/png' ...@@ -22,7 +22,7 @@ const PNG = 'image/png'
const JPEG = 'image/jpeg' const JPEG = 'image/jpeg'
const GIF = 'image/gif' const GIF = 'image/gif'
const SVG = 'image/svg+xml' const SVG = 'image/svg+xml'
const CACHE_VERSION = 2 const CACHE_VERSION = 3
const MODERN_TYPES = [/* AVIF, */ WEBP] const MODERN_TYPES = [/* AVIF, */ WEBP]
const ANIMATABLE_TYPES = [WEBP, PNG, GIF] const ANIMATABLE_TYPES = [WEBP, PNG, GIF]
const VECTOR_TYPES = [SVG] const VECTOR_TYPES = [SVG]
...@@ -35,7 +35,8 @@ export async function imageOptimizer( ...@@ -35,7 +35,8 @@ export async function imageOptimizer(
res: ServerResponse, res: ServerResponse,
parsedUrl: UrlWithParsedQuery, parsedUrl: UrlWithParsedQuery,
nextConfig: NextConfig, nextConfig: NextConfig,
distDir: string distDir: string,
isDev = false
) { ) {
const imageData: ImageConfig = nextConfig.images || imageConfigDefault const imageData: ImageConfig = nextConfig.images || imageConfigDefault
const { deviceSizes = [], imageSizes = [], domains = [], loader } = imageData const { deviceSizes = [], imageSizes = [], domains = [], loader } = imageData
...@@ -158,24 +159,24 @@ export async function imageOptimizer( ...@@ -158,24 +159,24 @@ export async function imageOptimizer(
if (await fileExists(hashDir, 'directory')) { if (await fileExists(hashDir, 'directory')) {
const files = await promises.readdir(hashDir) const files = await promises.readdir(hashDir)
for (let file of files) { for (let file of files) {
const [prefix, etag, extension] = file.split('.') const [maxAgeStr, expireAtSt, etag, extension] = file.split('.')
const expireAt = Number(prefix) const maxAge = Number(maxAgeStr)
const expireAt = Number(expireAtSt)
const contentType = getContentType(extension) const contentType = getContentType(extension)
const fsPath = join(hashDir, file) const fsPath = join(hashDir, file)
if (now < expireAt) { if (now < expireAt) {
res.setHeader( const result = setResponseHeaders(
'Cache-Control', req,
isStatic res,
? 'public, max-age=315360000, immutable' etag,
: 'public, max-age=0, must-revalidate' maxAge,
contentType,
isStatic,
isDev
) )
if (sendEtagResponse(req, res, etag)) { if (!result.finished) {
return { finished: true } createReadStream(fsPath).pipe(res)
} }
if (contentType) {
res.setHeader('Content-Type', contentType)
}
createReadStream(fsPath).pipe(res)
return { finished: true } return { finished: true }
} else { } else {
await promises.unlink(fsPath) await promises.unlink(fsPath)
...@@ -271,8 +272,22 @@ export async function imageOptimizer( ...@@ -271,8 +272,22 @@ export async function imageOptimizer(
const animate = const animate =
ANIMATABLE_TYPES.includes(upstreamType) && isAnimated(upstreamBuffer) ANIMATABLE_TYPES.includes(upstreamType) && isAnimated(upstreamBuffer)
if (vector || animate) { if (vector || animate) {
await writeToCacheDir(hashDir, upstreamType, expireAt, upstreamBuffer) await writeToCacheDir(
sendResponse(req, res, upstreamType, upstreamBuffer, isStatic) hashDir,
upstreamType,
maxAge,
expireAt,
upstreamBuffer
)
sendResponse(
req,
res,
maxAge,
upstreamType,
upstreamBuffer,
isStatic,
isDev
)
return { finished: true } return { finished: true }
} }
...@@ -342,13 +357,35 @@ export async function imageOptimizer( ...@@ -342,13 +357,35 @@ export async function imageOptimizer(
} }
if (optimizedBuffer) { if (optimizedBuffer) {
await writeToCacheDir(hashDir, contentType, expireAt, optimizedBuffer) await writeToCacheDir(
sendResponse(req, res, contentType, optimizedBuffer, isStatic) hashDir,
contentType,
maxAge,
expireAt,
optimizedBuffer
)
sendResponse(
req,
res,
maxAge,
contentType,
optimizedBuffer,
isStatic,
isDev
)
} else { } else {
throw new Error('Unable to optimize buffer') throw new Error('Unable to optimize buffer')
} }
} catch (error) { } catch (error) {
sendResponse(req, res, upstreamType, upstreamBuffer, isStatic) sendResponse(
req,
res,
maxAge,
upstreamType,
upstreamBuffer,
isStatic,
isDev
)
} }
return { finished: true } return { finished: true }
...@@ -362,37 +399,64 @@ export async function imageOptimizer( ...@@ -362,37 +399,64 @@ export async function imageOptimizer(
async function writeToCacheDir( async function writeToCacheDir(
dir: string, dir: string,
contentType: string, contentType: string,
maxAge: number,
expireAt: number, expireAt: number,
buffer: Buffer buffer: Buffer
) { ) {
await promises.mkdir(dir, { recursive: true }) await promises.mkdir(dir, { recursive: true })
const extension = getExtension(contentType) const extension = getExtension(contentType)
const etag = getHash([buffer]) const etag = getHash([buffer])
const filename = join(dir, `${expireAt}.${etag}.${extension}`) const filename = join(dir, `${maxAge}.${expireAt}.${etag}.${extension}`)
await promises.writeFile(filename, buffer) await promises.writeFile(filename, buffer)
} }
function sendResponse( function setResponseHeaders(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
etag: string,
maxAge: number,
contentType: string | null, contentType: string | null,
buffer: Buffer, isStatic: boolean,
isStatic: boolean isDev: boolean
) { ) {
const etag = getHash([buffer])
res.setHeader( res.setHeader(
'Cache-Control', 'Cache-Control',
isStatic isStatic
? 'public, max-age=315360000, immutable' ? 'public, max-age=315360000, immutable'
: 'public, max-age=0, must-revalidate' : `public, max-age=${isDev ? 0 : maxAge}, must-revalidate`
) )
if (sendEtagResponse(req, res, etag)) { if (sendEtagResponse(req, res, etag)) {
return // already called res.end() so we're finished
return { finished: true }
} }
if (contentType) { if (contentType) {
res.setHeader('Content-Type', contentType) res.setHeader('Content-Type', contentType)
} }
res.end(buffer) return { finished: false }
}
function sendResponse(
req: IncomingMessage,
res: ServerResponse,
maxAge: number,
contentType: string | null,
buffer: Buffer,
isStatic: boolean,
isDev: boolean
) {
const etag = getHash([buffer])
const result = setResponseHeaders(
req,
res,
etag,
maxAge,
contentType,
isStatic,
isDev
)
if (!result.finished) {
res.end(buffer)
}
} }
function getSupportedMimeType(options: string[], accept = ''): string { function getSupportedMimeType(options: string[], accept = ''): string {
......
...@@ -789,7 +789,8 @@ export default class Server { ...@@ -789,7 +789,8 @@ export default class Server {
res, res,
parsedUrl, parsedUrl,
server.nextConfig, server.nextConfig,
server.distDir server.distDir,
this.renderOpts.dev
), ),
}, },
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment