Unverified Commit 1c219b16 authored by Steven's avatar Steven Committed by GitHub

feat: add experimental `imgOptSequentialRead` config (#72411)

This setting was originally added in PR
https://github.com/vercel/next.js/pull/44881 and then removed in PR
https://github.com/vercel/next.js/pull/65846 however it seems like it
may still be useful in some cases.

The [sharp docs](https://sharp.pixelplumbing.com/api-constructor) are
not very clear:

> false to use random access, true to use sequential reads. Some
operations will do this automatically.

Let's add it back as an experimental setting so we can measure
performance.
parent 93a720f1
...@@ -317,6 +317,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -317,6 +317,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
imgOptConcurrency: z.number().int().optional().nullable(), imgOptConcurrency: z.number().int().optional().nullable(),
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(),
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(),
......
...@@ -289,6 +289,7 @@ export interface ExperimentalConfig { ...@@ -289,6 +289,7 @@ export interface ExperimentalConfig {
imgOptConcurrency?: number | null imgOptConcurrency?: number | null
imgOptTimeoutInSeconds?: number imgOptTimeoutInSeconds?: number
imgOptMaxInputPixels?: number imgOptMaxInputPixels?: number
imgOptSequentialRead?: boolean | null
optimisticClientCache?: boolean optimisticClientCache?: boolean
/** /**
* @deprecated use config.expireTime instead * @deprecated use config.expireTime instead
...@@ -1101,6 +1102,7 @@ export const defaultConfig: NextConfig = { ...@@ -1101,6 +1102,7 @@ export const defaultConfig: NextConfig = {
imgOptConcurrency: null, imgOptConcurrency: null,
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,
isrFlushToDisk: true, isrFlushToDisk: true,
workerThreads: false, workerThreads: false,
proxyTimeout: undefined, proxyTimeout: undefined,
......
...@@ -514,6 +514,7 @@ export async function optimizeImage({ ...@@ -514,6 +514,7 @@ export async function optimizeImage({
height, height,
concurrency, concurrency,
limitInputPixels, limitInputPixels,
sequentialRead,
timeoutInSeconds, timeoutInSeconds,
}: { }: {
buffer: Buffer buffer: Buffer
...@@ -523,11 +524,13 @@ export async function optimizeImage({ ...@@ -523,11 +524,13 @@ export async function optimizeImage({
height?: number height?: number
concurrency?: number | null concurrency?: number | null
limitInputPixels?: number limitInputPixels?: number
sequentialRead?: boolean | null
timeoutInSeconds?: number timeoutInSeconds?: number
}): Promise<Buffer> { }): Promise<Buffer> {
const sharp = getSharp(concurrency) const sharp = getSharp(concurrency)
const transformer = sharp(buffer, { const transformer = sharp(buffer, {
limitInputPixels, limitInputPixels,
sequentialRead: sequentialRead ?? undefined,
}) })
.timeout({ .timeout({
seconds: timeoutInSeconds ?? 7, seconds: timeoutInSeconds ?? 7,
...@@ -645,7 +648,10 @@ export async function imageOptimizer( ...@@ -645,7 +648,10 @@ export async function imageOptimizer(
nextConfig: { nextConfig: {
experimental: Pick< experimental: Pick<
NextConfigComplete['experimental'], NextConfigComplete['experimental'],
'imgOptConcurrency' | 'imgOptMaxInputPixels' | 'imgOptTimeoutInSeconds' | 'imgOptConcurrency'
| 'imgOptMaxInputPixels'
| 'imgOptSequentialRead'
| 'imgOptTimeoutInSeconds'
> >
images: Pick< images: Pick<
NextConfigComplete['images'], NextConfigComplete['images'],
...@@ -753,6 +759,7 @@ export async function imageOptimizer( ...@@ -753,6 +759,7 @@ export async function imageOptimizer(
width, width,
concurrency: nextConfig.experimental.imgOptConcurrency, concurrency: nextConfig.experimental.imgOptConcurrency,
limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels, limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels,
sequentialRead: nextConfig.experimental.imgOptSequentialRead,
timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds, timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds,
}) })
if (optimizedBuffer) { if (optimizedBuffer) {
......
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