Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Oct 23, 2024
1 parent 242faf0 commit 76864f1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
forceSwcTransforms: z.boolean().optional(),
fullySpecified: 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(),
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export interface ExperimentalConfig {
extensionAlias?: Record<string, any>
allowedRevalidateHeaderKeys?: string[]
fetchCacheKeyPrefix?: string
imgOptConcurrency?: number | null
imgOptTimeoutInSeconds?: number
imgOptMaxInputPixels?: number
optimisticClientCache?: boolean
Expand Down Expand Up @@ -1108,6 +1109,9 @@ export const defaultConfig: NextConfig = {
(os.cpus() || { length: 1 }).length) - 1
),
memoryBasedWorkersCount: false,
imgOptConcurrency: null,
imgOptTimeoutInSeconds: 7,
imgOptMaxInputPixels: 268_402_689, // https://sharp.pixelplumbing.com/api-constructor#:~:text=%5Boptions.limitInputPixels%5D
isrFlushToDisk: true,
workerThreads: false,
proxyTimeout: undefined,
Expand Down
11 changes: 7 additions & 4 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const BLUR_QUALITY = 70 // should match `next-image-loader`

let _sharp: typeof import('sharp')

function getSharp() {
function getSharp(concurrency: number | null | undefined) {
if (_sharp) {
return _sharp
}
Expand All @@ -59,7 +59,7 @@ function getSharp() {
// https://sharp.pixelplumbing.com/api-utility#concurrency
const divisor = process.env.NODE_ENV === 'development' ? 4 : 2
_sharp.concurrency(
Math.floor(Math.max(_sharp.concurrency() / divisor, 1))
concurrency ?? Math.floor(Math.max(_sharp.concurrency() / divisor, 1))
)
}
} catch (e: unknown) {
Expand Down Expand Up @@ -512,6 +512,7 @@ export async function optimizeImage({
quality,
width,
height,
concurrency,
limitInputPixels,
timeoutInSeconds,
}: {
Expand All @@ -520,10 +521,11 @@ export async function optimizeImage({
quality: number
width: number
height?: number
concurrency?: number | null
limitInputPixels?: number
timeoutInSeconds?: number
}): Promise<Buffer> {
const sharp = getSharp()
const sharp = getSharp(concurrency)
const transformer = sharp(buffer, {
limitInputPixels,
})
Expand Down Expand Up @@ -643,7 +645,7 @@ export async function imageOptimizer(
nextConfig: {
experimental: Pick<
NextConfigComplete['experimental'],
'imgOptMaxInputPixels' | 'imgOptTimeoutInSeconds'
'imgOptConcurrency' | 'imgOptMaxInputPixels' | 'imgOptTimeoutInSeconds'
>
images: Pick<
NextConfigComplete['images'],
Expand Down Expand Up @@ -749,6 +751,7 @@ export async function imageOptimizer(
contentType,
quality,
width,
concurrency: nextConfig.experimental.imgOptConcurrency,
limitInputPixels: nextConfig.experimental.imgOptMaxInputPixels,
timeoutInSeconds: nextConfig.experimental.imgOptTimeoutInSeconds,
})
Expand Down
32 changes: 9 additions & 23 deletions test/integration/image-optimizer/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,42 +737,28 @@ describe('Image Optimizer', () => {
describe('experimental.imgOptMaxInputPixels in next.config.js', () => {
let app
let appPort
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' } }

afterEach(async () => {
await killApp(app)
nextConfig.restore()
})
it('should optimize when imgOptMaxInputPixels is larger than source image', async () => {
beforeAll(async () => {
nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
experimental: {
imgOptMaxInputPixels: 60_000_000,
imgOptMaxInputPixels: 100,
},
})
)
await cleanImagesDir({ imagesDir })
appPort = await findPort()
app = await launchApp(appDir, appPort)
const res = await fetchViaHTTP(appPort, '/_next/image', query, opts)
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('image/webp')
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
it('should fallback to source image when input exceeds imgOptMaxInputPixels', async () => {
nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
experimental: {
imgOptMaxInputPixels: 100,
},
})
)
await cleanImagesDir({ imagesDir })
appPort = await findPort()
app = await launchApp(appDir, appPort)
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')
Expand Down

0 comments on commit 76864f1

Please sign in to comment.