Unverified Commit deed8684 authored by Steven's avatar Steven Committed by GitHub

feat(next/image): Add experimental imgOptMozjpeg config (#98571)

In a previous PR, `mozjpeg` was set to true which might be the reason
output jpeg spikes cpu.

- https://github.com/vercel/next.js/pull/65846

I made a benchmark and found that disabling MozJPEG saved 72% CPU, with
average outputs 31% larger.

- https://github.com/lovell/sharp/issues/4603

So this PR adds a new experimental flag to enable (or rather disable)
mozjpeg and try with real workloads. This is useful becaues in many
cases, cpu is more costly than bandwidth since most CDNs provide [near
unlimited bandwidth](https://vercel.com/blog/introducing-flat-rate-cdn).
parent d155ba9e
...@@ -273,6 +273,7 @@ export const experimentalSchema = { ...@@ -273,6 +273,7 @@ export const experimentalSchema = {
imgOptTimeoutInSeconds: z.number().int().optional(), imgOptTimeoutInSeconds: z.number().int().optional(),
imgOptMaxInputPixels: z.number().int().optional(), imgOptMaxInputPixels: z.number().int().optional(),
imgOptSequentialRead: z.boolean().optional().nullable(), imgOptSequentialRead: z.boolean().optional().nullable(),
imgOptMozjpeg: z.boolean().optional(),
isrFlushToDisk: z.boolean().optional(), isrFlushToDisk: z.boolean().optional(),
largePageDataBytes: z.number().optional(), largePageDataBytes: z.number().optional(),
linkNoTouchStart: z.boolean().optional(), linkNoTouchStart: z.boolean().optional(),
......
...@@ -606,6 +606,7 @@ export interface ExperimentalConfig { ...@@ -606,6 +606,7 @@ export interface ExperimentalConfig {
imgOptTimeoutInSeconds?: number imgOptTimeoutInSeconds?: number
imgOptMaxInputPixels?: number imgOptMaxInputPixels?: number
imgOptSequentialRead?: boolean | null imgOptSequentialRead?: boolean | null
imgOptMozjpeg?: boolean
optimisticClientCache?: boolean optimisticClientCache?: boolean
/** /**
* @deprecated use config.expireTime instead * @deprecated use config.expireTime instead
...@@ -2333,6 +2334,7 @@ export const defaultConfig = Object.freeze({ ...@@ -2333,6 +2334,7 @@ export const defaultConfig = Object.freeze({
imgOptTimeoutInSeconds: 7, imgOptTimeoutInSeconds: 7,
imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D
imgOptSequentialRead: null, imgOptSequentialRead: null,
imgOptMozjpeg: true,
isrFlushToDisk: true, isrFlushToDisk: true,
workerThreads: false, workerThreads: false,
proxyTimeout: undefined, proxyTimeout: undefined,
...@@ -2502,6 +2504,7 @@ export interface NextConfigRuntime { ...@@ -2502,6 +2504,7 @@ export interface NextConfigRuntime {
| 'imgOptMaxInputPixels' | 'imgOptMaxInputPixels'
| 'imgOptSequentialRead' | 'imgOptSequentialRead'
| 'imgOptTimeoutInSeconds' | 'imgOptTimeoutInSeconds'
| 'imgOptMozjpeg'
| 'proxyClientMaxBodySize' | 'proxyClientMaxBodySize'
| 'proxyTimeout' | 'proxyTimeout'
| 'testProxy' | 'testProxy'
...@@ -2573,6 +2576,7 @@ export function getNextConfigRuntime( ...@@ -2573,6 +2576,7 @@ export function getNextConfigRuntime(
imgOptMaxInputPixels: ex.imgOptMaxInputPixels, imgOptMaxInputPixels: ex.imgOptMaxInputPixels,
imgOptSequentialRead: ex.imgOptSequentialRead, imgOptSequentialRead: ex.imgOptSequentialRead,
imgOptTimeoutInSeconds: ex.imgOptTimeoutInSeconds, imgOptTimeoutInSeconds: ex.imgOptTimeoutInSeconds,
imgOptMozjpeg: ex.imgOptMozjpeg,
proxyClientMaxBodySize: ex.proxyClientMaxBodySize, proxyClientMaxBodySize: ex.proxyClientMaxBodySize,
proxyTimeout: ex.proxyTimeout, proxyTimeout: ex.proxyTimeout,
testProxy: ex.testProxy, testProxy: ex.testProxy,
......
...@@ -752,6 +752,7 @@ export async function imageOptimizer( ...@@ -752,6 +752,7 @@ export async function imageOptimizer(
| 'imgOptMaxInputPixels' | 'imgOptMaxInputPixels'
| 'imgOptSequentialRead' | 'imgOptSequentialRead'
| 'imgOptTimeoutInSeconds' | 'imgOptTimeoutInSeconds'
| 'imgOptMozjpeg'
> >
images: Pick< images: Pick<
NextConfigComplete['images'], NextConfigComplete['images'],
......
...@@ -49,6 +49,7 @@ export type ImageOptimizerTransformConfig = { ...@@ -49,6 +49,7 @@ export type ImageOptimizerTransformConfig = {
| 'imgOptMaxInputPixels' | 'imgOptMaxInputPixels'
| 'imgOptSequentialRead' | 'imgOptSequentialRead'
| 'imgOptTimeoutInSeconds' | 'imgOptTimeoutInSeconds'
| 'imgOptMozjpeg'
> >
images: Pick< images: Pick<
NextConfigComplete['images'], NextConfigComplete['images'],
...@@ -139,6 +140,7 @@ export async function optimizeImage({ ...@@ -139,6 +140,7 @@ export async function optimizeImage({
limitInputPixels, limitInputPixels,
sequentialRead, sequentialRead,
timeoutInSeconds, timeoutInSeconds,
mozjpeg = true,
}: { }: {
buffer: Buffer buffer: Buffer
contentType: string contentType: string
...@@ -150,6 +152,7 @@ export async function optimizeImage({ ...@@ -150,6 +152,7 @@ export async function optimizeImage({
limitInputPixels?: number limitInputPixels?: number
sequentialRead?: boolean | null sequentialRead?: boolean | null
timeoutInSeconds?: number timeoutInSeconds?: number
mozjpeg?: boolean
}): Promise<Buffer> { }): Promise<Buffer> {
const sharp = getSharp(concurrency, operationCache) const sharp = getSharp(concurrency, operationCache)
const transformer = sharp(buffer, { const transformer = sharp(buffer, {
...@@ -182,7 +185,7 @@ export async function optimizeImage({ ...@@ -182,7 +185,7 @@ export async function optimizeImage({
} else if (contentType === PNG) { } else if (contentType === PNG) {
transformer.png({ quality }) transformer.png({ quality })
} else if (contentType === JPEG) { } else if (contentType === JPEG) {
transformer.jpeg({ quality, mozjpeg: true }) transformer.jpeg({ quality, mozjpeg })
} }
const optimizedBuffer = await transformer.toBuffer() const optimizedBuffer = await transformer.toBuffer()
...@@ -283,6 +286,7 @@ export async function imageOptimizerTransform( ...@@ -283,6 +286,7 @@ export async function imageOptimizerTransform(
limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels, limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels,
sequentialRead: nextConfig.experimental.imgOptSequentialRead, sequentialRead: nextConfig.experimental.imgOptSequentialRead,
timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds, timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds,
mozjpeg: nextConfig.experimental.imgOptMozjpeg,
}) })
if (opts.handleDevOutput) { if (opts.handleDevOutput) {
const output = await opts.handleDevOutput(optimizedBuffer, contentType) const output = await opts.handleDevOutput(optimizedBuffer, contentType)
......
/* eslint-env jest */ /* eslint-env jest */
import { readFile } from 'fs-extra' import { readFile } from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { imageOptimizerTransform } from 'next/dist/server/image-optimizer/transform' import {
getSharp,
imageOptimizerTransform,
type ImageOptimizerTransformConfig,
} from 'next/dist/server/image-optimizer/transform'
import type { import type {
CachedRouteKind, CachedRouteKind,
IncrementalResponseCacheEntry, IncrementalResponseCacheEntry,
...@@ -24,7 +28,11 @@ const config = { ...@@ -24,7 +28,11 @@ const config = {
}, },
} }
async function transform(filename: string, mimeType = 'image/webp') { async function transform(
filename: string,
mimeType = 'image/webp',
nextConfig: ImageOptimizerTransformConfig = config
) {
const buffer = await getImage(filename) const buffer = await getImage(filename)
return imageOptimizerTransform( return imageOptimizerTransform(
{ {
...@@ -34,7 +42,7 @@ async function transform(filename: string, mimeType = 'image/webp') { ...@@ -34,7 +42,7 @@ async function transform(filename: string, mimeType = 'image/webp') {
etag: 'source-etag', etag: 'source-etag',
}, },
{ href: `/${filename}`, width: 64, quality: 75, mimeType }, { href: `/${filename}`, width: 64, quality: 75, mimeType },
config nextConfig
) )
} }
...@@ -88,6 +96,24 @@ describe('imageOptimizerTransform', () => { ...@@ -88,6 +96,24 @@ describe('imageOptimizerTransform', () => {
expect(result.maxAge).toBe(120) expect(result.maxAge).toBe(120)
}) })
it.each([undefined, true, false])(
'encodes JPEGs with imgOptMozjpeg=%s',
async (imgOptMozjpeg) => {
const result = await transform('test.jpg', 'image/jpeg', {
...config,
experimental: { ...config.experimental, imgOptMozjpeg },
})
expect(result.error).toBeUndefined()
expect(result.contentType).toBe('image/jpeg')
const sharp = getSharp(1, false)
expect(await sharp(result.buffer).metadata()).toMatchObject({
format: 'jpeg',
width: 64,
isProgressive: imgOptMozjpeg ?? true,
})
}
)
it('preserves the source format when no output format is requested', async () => { it('preserves the source format when no output format is requested', async () => {
const result = await transform('test.png', '') const result = await transform('test.png', '')
expect(result.contentType).toBe('image/png') expect(result.contentType).toBe('image/png')
......
...@@ -108,6 +108,27 @@ describe('config', () => { ...@@ -108,6 +108,27 @@ describe('config', () => {
expect(disabledConfig.experimental.devMemoryThresholdRestart).toBe(false) expect(disabledConfig.experimental.devMemoryThresholdRestart).toBe(false)
}) })
it.each([undefined, true, false])(
'Should preserve imgOptMozjpeg=%s in the runtime config',
async (imgOptMozjpeg) => {
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, '<rootDir>', {
customConfig: {
experimental: {
imgOptMozjpeg,
runtimeServerDeploymentId: true,
},
},
})
const { getNextConfigRuntime } = await import(
'next/dist/server/config-shared'
)
expect(config.experimental.imgOptMozjpeg).toBe(imgOptMozjpeg ?? true)
expect(getNextConfigRuntime(config).experimental.imgOptMozjpeg).toBe(
imgOptMozjpeg ?? true
)
}
)
it('Should allow bundler-specific options during the test phase', async () => { it('Should allow bundler-specific options during the test phase', async () => {
const config = await loadConfig(PHASE_TEST, '<rootDir>-test-phase', { const config = await loadConfig(PHASE_TEST, '<rootDir>-test-phase', {
customConfig: { customConfig: {
......
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