Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Image Optimization API when next.config.js has unoptimized: true #44205

Merged
merged 4 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/next/build/index.ts
Expand Up @@ -2726,14 +2726,20 @@ export default async function build(
pathname: makeRe(p.pathname ?? '**').source,
}))

await promises.writeFile(
path.join(distDir, IMAGES_MANIFEST),
JSON.stringify({
version: 1,
images,
}),
'utf8'
)
if (images.unoptimized) {
// Skip writing the images manifest file
// so that any downstream consumer will know
// to disable the Image Optimization API
styfle marked this conversation as resolved.
Show resolved Hide resolved
} else {
await promises.writeFile(
path.join(distDir, IMAGES_MANIFEST),
JSON.stringify({
version: 1,
images,
}),
'utf8'
)
}
await promises.writeFile(
path.join(distDir, EXPORT_MARKER),
JSON.stringify({
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/next-server.ts
Expand Up @@ -407,7 +407,7 @@ export default class NextNodeServer extends BaseServer {
}
const imagesConfig = this.nextConfig.images

if (imagesConfig.loader !== 'default') {
if (imagesConfig.loader !== 'default' || imagesConfig.unoptimized) {
await this.render404(req, res)
return { finished: true }
}
Expand Down
30 changes: 30 additions & 0 deletions test/integration/image-optimizer/test/index.test.ts
Expand Up @@ -558,6 +558,36 @@ describe('Image Optimizer', () => {
})
})

describe('images.unoptimized in next.config.js', () => {
let app
let appPort

beforeAll(async () => {
nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
unoptimized: true,
},
})
)
await cleanImagesDir({ imagesDir })
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
it('should 404 when unoptimized', async () => {
const size = 384 // defaults defined in server/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(404)
})
})

describe('External rewrite support with for serving static content in images', () => {
let app
let appPort
Expand Down