Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ function assignDefaults(userConfig: { [key: string]: any }) {
`Specified images.domains should be an Array received ${typeof images.domains}`
)
}

if (images.domains.length > 50) {
throw new Error(
`Specified images.domains exceeds length of 50, received length (${images.domains.length}), please reduce the length of the array to continue`
)
}

const invalid = images.domains.filter(
(d: unknown) => typeof d !== 'string'
)
Expand All @@ -252,10 +259,20 @@ function assignDefaults(userConfig: { [key: string]: any }) {
`Specified images.sizes should be an Array received ${typeof images.sizes}`
)
}
const invalid = images.sizes.filter((d: unknown) => typeof d !== 'number')

if (images.sizes.length > 50) {
throw new Error(
`Specified images.sizes exceeds length of 50, received length (${images.sizes.length}), please reduce the length of the array to continue`
)
}

const invalid = images.sizes.filter((d: unknown) => {
return typeof d !== 'number' || d < 0 || d > 10000
})

if (invalid.length > 0) {
throw new Error(
`Specified images.sizes should be an Array of numbers received invalid values (${invalid.join(
`Specified images.sizes should be an Array of numbers that are between 0 and 10,000, received invalid values (${invalid.join(
', '
)})`
)
Expand Down
78 changes: 78 additions & 0 deletions test/integration/image-optimizer/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
nextBuild,
nextStart,
File,
waitFor,
} from 'next-test-utils'
import sharp from 'sharp'

Expand Down Expand Up @@ -321,6 +322,83 @@ function runTests({ w, isDev, domains }) {
}

describe('Image Optimizer', () => {
describe('config checks', () => {
it('should error when domains length exceeds 50', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
domains: new Array(51).fill('google.com'),
},
})
)
let stderr = ''

app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg || ''
},
})
await waitFor(1000)
await killApp(app).catch(() => {})
await nextConfig.restore()

expect(stderr).toContain(
'Specified images.domains exceeds length of 50, received length (51), please reduce the length of the array to continue'
)
})

it('should error when sizes length exceeds 50', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
sizes: new Array(51).fill(1024),
},
})
)
let stderr = ''

app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg || ''
},
})
await waitFor(1000)
await killApp(app).catch(() => {})
await nextConfig.restore()

expect(stderr).toContain(
'Specified images.sizes exceeds length of 50, received length (51), please reduce the length of the array to continue'
)
})

it('should error when sizes contains invalid sizes', async () => {
await nextConfig.replace(
'{ /* replaceme */ }',
JSON.stringify({
images: {
sizes: [-1, 12000, 64, 128, 256],
},
})
)
let stderr = ''

app = await launchApp(appDir, await findPort(), {
onStderr(msg) {
stderr += msg || ''
},
})
await waitFor(1000)
await killApp(app).catch(() => {})
await nextConfig.restore()

expect(stderr).toContain(
'Specified images.sizes should be an Array of numbers that are between 0 and 10,000, received invalid values (-1, 12000)'
)
})
})

// domains for testing
const domains = ['localhost', 'example.com']

Expand Down