Skip to content

Commit 12472b4

Browse files
authored
Fixed image-optimizer crashing when using outputFileTracing (#48513)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation or adding/fixing Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change --> ### What? This PR fixes the issue where using `output: "standalone"` breaks `next/image`, `opengraph-image.tsx`,... (those that use `image-optimizer`), which was introduced by #47208 as it added `next/dist/compiled/jest-worker` to `ignores`. ### How? Dynamically import `squoosh` so that it does not throw. Fixes #48077 Fixes #48173
1 parent 77204c9 commit 12472b4

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/next/src/server/image-optimizer.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import contentDisposition from 'next/dist/compiled/content-disposition'
99
import { join } from 'path'
1010
import nodeUrl, { UrlWithParsedQuery } from 'url'
1111
import { NextConfigComplete } from './config-shared'
12-
import {
13-
processBuffer,
14-
decodeBuffer,
15-
Operation,
16-
getMetadata,
17-
} from './lib/squoosh/main'
12+
// Do not import anything other than types from this module
13+
// because it will throw an error when using `outputFileTracing`
14+
// because `jest-worker` is ignored in file tracing. Use `await import`
15+
// or `require` instead.
16+
import { Operation } from './lib/squoosh/main'
1817
import { sendEtagResponse } from './send-payload'
1918
import { getContentType, getExtension } from './serve-static'
2019
import chalk from 'next/dist/compiled/chalk'
@@ -454,8 +453,6 @@ export async function optimizeImage({
454453
// End sharp transformation logic
455454
} else {
456455
if (showSharpMissingWarning && nextConfigOutput === 'standalone') {
457-
// TODO: should we ensure squoosh also works even though we don't
458-
// recommend it be used in production and this is a production feature
459456
console.error(
460457
`Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly. Read more at: https://nextjs.org/docs/messages/sharp-missing-in-production`
461458
)
@@ -494,6 +491,9 @@ export async function optimizeImage({
494491
operations.push({ type: 'resize', width })
495492
}
496493

494+
const { processBuffer } =
495+
require('./lib/squoosh/main') as typeof import('./lib/squoosh/main')
496+
497497
if (contentType === AVIF) {
498498
optimizedBuffer = await processBuffer(buffer, operations, 'avif', quality)
499499
} else if (contentType === WEBP) {
@@ -637,6 +637,8 @@ export async function imageOptimizer(
637637
})
638638
if (optimizedBuffer) {
639639
if (isDev && width <= BLUR_IMG_SIZE && quality === BLUR_QUALITY) {
640+
const { getMetadata } =
641+
require('./lib/squoosh/main') as typeof import('./lib/squoosh/main')
640642
// During `next dev`, we don't want to generate blur placeholders with webpack
641643
// because it can delay starting the dev server. Instead, `next-image-loader.js`
642644
// will inline a special url to lazily generate the blur placeholder at request time.
@@ -778,6 +780,8 @@ export async function getImageSize(
778780
const { width, height } = await transformer.metadata()
779781
return { width, height }
780782
} else {
783+
const { decodeBuffer } =
784+
require('./lib/squoosh/main') as typeof import('./lib/squoosh/main')
781785
const { width, height } = await decodeBuffer(buffer)
782786
return { width, height }
783787
}

0 commit comments

Comments
 (0)