Skip to content

Commit

Permalink
Disable Image Optimization API when next.config.js has `unoptimized: …
Browse files Browse the repository at this point in the history
…true` (#44205)

This fixes a bug where next.config.js was configured with `images.unoptimzed: true` but the Image Optimization API was not truly disabled. Since there is no way to override the config at the component level, its safe to say the API can be disabled.
  • Loading branch information
styfle committed Dec 21, 2022
1 parent 0ac617e commit 6f1f3e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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

0 comments on commit 6f1f3e6

Please sign in to comment.