Skip to content

Commit 33eac8a

Browse files
authored
Image Optimization API should 404 when loader is not default (#18211)
We currently always accept requests to the new `/_next/image` endpoint, even when it should not be used. Instead, we should check to see if the default loader is used as a signal to enable this API. Other loaders (such as cloudinary) will not go through the Next.js API so there is no need to expose this, instead we 404. - Analogous to vercel/vercel#5321 - Related to #18122
1 parent 6d3b065 commit 33eac8a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ export async function imageOptimizer(
3030
parsedUrl: UrlWithParsedQuery
3131
) {
3232
const { nextConfig, distDir } = server
33-
const { sizes = [], domains = [] } = nextConfig?.images || {}
33+
const { sizes = [], domains = [], loader } = nextConfig?.images || {}
34+
35+
if (loader !== 'default') {
36+
await server.render404(req, res, parsedUrl)
37+
return { finished: true }
38+
}
39+
3440
const { headers } = req
3541
const { url, w, q } = parsedUrl.query
3642
const mimeType = mediaType(headers.accept, MIME_TYPES) || ''

test/integration/image-optimizer/test/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,29 @@ describe('Image Optimizer', () => {
421421

422422
runTests({ w: size, isDev: false, domains })
423423
})
424+
425+
describe('dev support next.config.js cloudinary loader', () => {
426+
beforeAll(async () => {
427+
const json = JSON.stringify({
428+
images: {
429+
loader: 'cloudinary',
430+
path: 'https://example.com/act123/',
431+
},
432+
})
433+
nextConfig.replace('{ /* replaceme */ }', json)
434+
appPort = await findPort()
435+
app = await launchApp(appDir, appPort)
436+
})
437+
afterAll(async () => {
438+
await killApp(app)
439+
nextConfig.restore()
440+
await fs.remove(imagesDir)
441+
})
442+
it('should 404 when loader is not default', async () => {
443+
const query = { w: 320, q: 90, url: '/test.svg' }
444+
const opts = { headers: { accept: 'image/webp' } }
445+
const res = await fetchViaHTTP(appPort, '/_next/image', query, opts)
446+
expect(res.status).toBe(404)
447+
})
448+
})
424449
})

0 commit comments

Comments
 (0)