Unverified Commit 0dce6bb4 authored by Steven's avatar Steven Committed by GitHub

feat: add experiment for sharpjs cpu flags (#71733)

This PR adds two experimental flags to configure sharpjs settings that
may improve cpu for image optimization.
parent dbc94523
...@@ -314,6 +314,9 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -314,6 +314,9 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
forceSwcTransforms: z.boolean().optional(), forceSwcTransforms: z.boolean().optional(),
fullySpecified: z.boolean().optional(), fullySpecified: z.boolean().optional(),
gzipSize: z.boolean().optional(), gzipSize: z.boolean().optional(),
imgOptConcurrency: z.number().int().optional().nullable(),
imgOptTimeoutInSeconds: z.number().int().optional(),
imgOptMaxInputPixels: z.number().int().optional(),
internal_disableSyncDynamicAPIWarnings: z.boolean().optional(), internal_disableSyncDynamicAPIWarnings: z.boolean().optional(),
isrFlushToDisk: z.boolean().optional(), isrFlushToDisk: z.boolean().optional(),
largePageDataBytes: z.number().optional(), largePageDataBytes: z.number().optional(),
......
...@@ -286,6 +286,9 @@ export interface ExperimentalConfig { ...@@ -286,6 +286,9 @@ export interface ExperimentalConfig {
extensionAlias?: Record<string, any> extensionAlias?: Record<string, any>
allowedRevalidateHeaderKeys?: string[] allowedRevalidateHeaderKeys?: string[]
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
imgOptConcurrency?: number | null
imgOptTimeoutInSeconds?: number
imgOptMaxInputPixels?: number
optimisticClientCache?: boolean optimisticClientCache?: boolean
/** /**
* @deprecated use config.expireTime instead * @deprecated use config.expireTime instead
...@@ -1112,6 +1115,9 @@ export const defaultConfig: NextConfig = { ...@@ -1112,6 +1115,9 @@ export const defaultConfig: NextConfig = {
(os.cpus() || { length: 1 }).length) - 1 (os.cpus() || { length: 1 }).length) - 1
), ),
memoryBasedWorkersCount: false, memoryBasedWorkersCount: false,
imgOptConcurrency: null,
imgOptTimeoutInSeconds: 7,
imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D
isrFlushToDisk: true, isrFlushToDisk: true,
workerThreads: false, workerThreads: false,
proxyTimeout: undefined, proxyTimeout: undefined,
......
...@@ -47,7 +47,7 @@ const BLUR_QUALITY = 70 // should match `next-image-loader` ...@@ -47,7 +47,7 @@ const BLUR_QUALITY = 70 // should match `next-image-loader`
let _sharp: typeof import('sharp') let _sharp: typeof import('sharp')
function getSharp() { function getSharp(concurrency: number | null | undefined) {
if (_sharp) { if (_sharp) {
return _sharp return _sharp
} }
...@@ -59,7 +59,7 @@ function getSharp() { ...@@ -59,7 +59,7 @@ function getSharp() {
// https://sharp.pixelplumbing.com/api-utility#concurrency // https://sharp.pixelplumbing.com/api-utility#concurrency
const divisor = process.env.NODE_ENV === 'development' ? 4 : 2 const divisor = process.env.NODE_ENV === 'development' ? 4 : 2
_sharp.concurrency( _sharp.concurrency(
Math.floor(Math.max(_sharp.concurrency() / divisor, 1)) concurrency ?? Math.floor(Math.max(_sharp.concurrency() / divisor, 1))
) )
} }
} catch (e: unknown) { } catch (e: unknown) {
...@@ -512,15 +512,27 @@ export async function optimizeImage({ ...@@ -512,15 +512,27 @@ export async function optimizeImage({
quality, quality,
width, width,
height, height,
concurrency,
limitInputPixels,
timeoutInSeconds,
}: { }: {
buffer: Buffer buffer: Buffer
contentType: string contentType: string
quality: number quality: number
width: number width: number
height?: number height?: number
concurrency?: number | null
limitInputPixels?: number
timeoutInSeconds?: number
}): Promise<Buffer> { }): Promise<Buffer> {
const sharp = getSharp() const sharp = getSharp(concurrency)
const transformer = sharp(buffer).timeout({ seconds: 7 }).rotate() const transformer = sharp(buffer, {
limitInputPixels,
})
.timeout({
seconds: timeoutInSeconds ?? 7,
})
.rotate()
if (height) { if (height) {
transformer.resize(width, height) transformer.resize(width, height)
...@@ -631,6 +643,10 @@ export async function imageOptimizer( ...@@ -631,6 +643,10 @@ export async function imageOptimizer(
'href' | 'width' | 'quality' | 'mimeType' 'href' | 'width' | 'quality' | 'mimeType'
>, >,
nextConfig: { nextConfig: {
experimental: Pick<
NextConfigComplete['experimental'],
'imgOptConcurrency' | 'imgOptMaxInputPixels' | 'imgOptTimeoutInSeconds'
>
images: Pick< images: Pick<
NextConfigComplete['images'], NextConfigComplete['images'],
'dangerouslyAllowSVG' | 'minimumCacheTTL' 'dangerouslyAllowSVG' | 'minimumCacheTTL'
...@@ -735,6 +751,9 @@ export async function imageOptimizer( ...@@ -735,6 +751,9 @@ export async function imageOptimizer(
contentType, contentType,
quality, quality,
width, width,
concurrency: nextConfig.experimental.imgOptConcurrency,
limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels,
timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds,
}) })
if (optimizedBuffer) { if (optimizedBuffer) {
if (isDev && width <= BLUR_IMG_SIZE && quality === BLUR_QUALITY) { if (isDev && width <= BLUR_IMG_SIZE && quality === BLUR_QUALITY) {
......
...@@ -734,6 +734,37 @@ describe('Image Optimizer', () => { ...@@ -734,6 +734,37 @@ describe('Image Optimizer', () => {
}) })
}) })
describe('experimental.imgOptMaxInputPixels in next.config.js', () => {
let app
let appPort
beforeAll(async () => {
nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
experimental: {
imgOptMaxInputPixels: 100,
},
})
)
await cleanImagesDir({ imagesDir })
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
it('should fallback to source image when input exceeds imgOptMaxInputPixels', async () => {
const size = 256 // defaults defined in lib/image-config.ts
const query = { w: size, q: 75, url: '/test.jpg' }
const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(appPort, '/_next/image', query, opts)
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('image/jpeg')
})
})
describe('External rewrite support with for serving static content in images', () => { describe('External rewrite support with for serving static content in images', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode', 'production mode',
......
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