Skip to content

Commit 9160160

Browse files
authored
[metadata] refine metadata image error for webpack (#83139)
1 parent ada23ff commit 9160160

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed

packages/next/src/build/webpack/loaders/next-metadata-image-loader.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ async function nextMetadataImageLoader(
126126
}`
127127
}
128128

129+
let imageError
129130
const imageSize: { width?: number; height?: number } = await getImageSize(
130131
content
131-
).catch((err) => err)
132-
133-
if (imageSize instanceof Error) {
134-
const err = imageSize
135-
err.name = 'InvalidImageFormatError'
136-
throw err
132+
).catch((error) => {
133+
const message = `Process image "${path.posix.join(segment || '/', interpolatedName)}" failed: ${error}`
134+
imageError = new Error(message)
135+
return {}
136+
})
137+
138+
if (imageError) {
139+
throw imageError
137140
}
138141

139142
const imageData: Omit<MetadataImageModule, 'url'> = {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
malformed image content
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('metadata-invalid-image-file', () => {
4+
const { next, isTurbopack, isNextDev, skipped } = nextTestSetup({
5+
files: __dirname,
6+
skipDeployment: true,
7+
skipStart: true,
8+
})
9+
10+
if (skipped) return
11+
12+
it('should error on invalid metadata image file', async () => {
13+
// In dev, it needs to render the page first
14+
if (isNextDev) {
15+
await next.start()
16+
await next.fetch('/')
17+
} else {
18+
await next.build()
19+
}
20+
21+
if (isTurbopack) {
22+
// In turbopack the image decoding error is displayed in multiple lines
23+
expect(next.cliOutput).toContain('app/favicon.ico')
24+
expect(next.cliOutput).toContain('Processing image failed')
25+
expect(next.cliOutput).toContain('unable to decode image data')
26+
} else {
27+
expect(next.cliOutput).toContain(
28+
'Error: Process image "/favicon.ico" failed:'
29+
)
30+
}
31+
32+
if (!isNextDev) {
33+
// `next build` should fail
34+
if (isTurbopack) {
35+
expect(next.cliOutput).toContain('Build error occurred')
36+
} else {
37+
expect(next.cliOutput).toContain(
38+
'Build failed because of webpack errors'
39+
)
40+
}
41+
}
42+
})
43+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig

0 commit comments

Comments
 (0)