Skip to content

Commit 1da5926

Browse files
authored
feat(gatsby-plugin-sharp): added option to allow sharp process to continue with errors (#27345)
* added failOnError option to gatsby-plugin-sharp options * updated test snapshot to include "failOnError" default * added note about new option for gatsby-plugin-sharp * added failOnError to validation object
1 parent 63532f7 commit 1da5926

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

packages/gatsby-plugin-sharp/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ plugins: [
3636
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
3737
stripMetadata: true,
3838
defaultQuality: 50,
39+
failOnError: true,
3940
},
4041
},
4142
]
@@ -332,6 +333,10 @@ options, the [environment variable](/docs/environment-variables/#environment-var
332333
GATSBY_JPEG_ENCODER=MOZJPEG
333334
```
334335

336+
### Allow build to continue on image processing error
337+
338+
By default, the build will fail when it encounters an error while processing an image. You can change this so that it continues the build process by setting the plugin option `failOnError` to `false`. Sharp will still throw an error and display it in the console as a GraphQL error, but it will not exit the process. It is important to note that any images that would have otherwise failed will not be accessible via `childImageSharp` until the underlying issue with the image is addressed.
339+
335340
### EXIF and ICC metadata
336341

337342
By default, `gatsby-plugin-sharp` strips all EXIF, ICC and other metadata

packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap

+25
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ exports[`gatsby-plugin-sharp fixed correctly infers the width when only the heig
8484
"pluginOptions": Object {
8585
"base64Width": 20,
8686
"defaultQuality": 50,
87+
"failOnError": true,
8788
"forceBase64Format": "",
8889
"lazyImageGeneration": true,
8990
"stripMetadata": true,
@@ -139,6 +140,7 @@ exports[`gatsby-plugin-sharp fixed does not warn when the requested width is equ
139140
"pluginOptions": Object {
140141
"base64Width": 20,
141142
"defaultQuality": 50,
143+
"failOnError": true,
142144
"forceBase64Format": "",
143145
"lazyImageGeneration": true,
144146
"stripMetadata": true,
@@ -194,6 +196,7 @@ exports[`gatsby-plugin-sharp fixed should give the same result with createJob as
194196
"pluginOptions": Object {
195197
"base64Width": 20,
196198
"defaultQuality": 50,
199+
"failOnError": true,
197200
"forceBase64Format": "",
198201
"lazyImageGeneration": true,
199202
"stripMetadata": true,
@@ -235,6 +238,7 @@ exports[`gatsby-plugin-sharp fixed warns when the requested width is greater tha
235238
"pluginOptions": Object {
236239
"base64Width": 20,
237240
"defaultQuality": 50,
241+
"failOnError": true,
238242
"forceBase64Format": "",
239243
"lazyImageGeneration": true,
240244
"stripMetadata": true,
@@ -304,6 +308,7 @@ exports[`gatsby-plugin-sharp fluid accepts srcSet breakpoints 1`] = `
304308
"pluginOptions": Object {
305309
"base64Width": 20,
306310
"defaultQuality": 50,
311+
"failOnError": true,
307312
"forceBase64Format": "",
308313
"lazyImageGeneration": true,
309314
"stripMetadata": true,
@@ -345,6 +350,7 @@ exports[`gatsby-plugin-sharp fluid adds pathPrefix if defined 1`] = `
345350
"pluginOptions": Object {
346351
"base64Width": 20,
347352
"defaultQuality": 50,
353+
"failOnError": true,
348354
"forceBase64Format": "",
349355
"lazyImageGeneration": true,
350356
"stripMetadata": true,
@@ -426,6 +432,7 @@ Array [
426432
"pluginOptions": Object {
427433
"base64Width": 20,
428434
"defaultQuality": 50,
435+
"failOnError": true,
429436
"forceBase64Format": "",
430437
"lazyImageGeneration": true,
431438
"stripMetadata": true,
@@ -506,6 +513,7 @@ Array [
506513
"pluginOptions": Object {
507514
"base64Width": 20,
508515
"defaultQuality": 50,
516+
"failOnError": true,
509517
"forceBase64Format": "",
510518
"lazyImageGeneration": true,
511519
"stripMetadata": true,
@@ -586,6 +594,7 @@ Array [
586594
"pluginOptions": Object {
587595
"base64Width": 20,
588596
"defaultQuality": 50,
597+
"failOnError": true,
589598
"forceBase64Format": "",
590599
"lazyImageGeneration": true,
591600
"stripMetadata": true,
@@ -666,6 +675,7 @@ Array [
666675
"pluginOptions": Object {
667676
"base64Width": 20,
668677
"defaultQuality": 50,
678+
"failOnError": true,
669679
"forceBase64Format": "",
670680
"lazyImageGeneration": true,
671681
"stripMetadata": true,
@@ -724,6 +734,7 @@ Array [
724734
"pluginOptions": Object {
725735
"base64Width": 20,
726736
"defaultQuality": 50,
737+
"failOnError": true,
727738
"forceBase64Format": "",
728739
"lazyImageGeneration": true,
729740
"stripMetadata": true,
@@ -786,6 +797,7 @@ Array [
786797
"pluginOptions": Object {
787798
"base64Width": 20,
788799
"defaultQuality": 50,
800+
"failOnError": true,
789801
"forceBase64Format": "",
790802
"lazyImageGeneration": true,
791803
"stripMetadata": true,
@@ -848,6 +860,7 @@ Array [
848860
"pluginOptions": Object {
849861
"base64Width": 20,
850862
"defaultQuality": 50,
863+
"failOnError": true,
851864
"forceBase64Format": "",
852865
"lazyImageGeneration": true,
853866
"stripMetadata": true,
@@ -910,6 +923,7 @@ Array [
910923
"pluginOptions": Object {
911924
"base64Width": 20,
912925
"defaultQuality": 50,
926+
"failOnError": true,
913927
"forceBase64Format": "",
914928
"lazyImageGeneration": true,
915929
"stripMetadata": true,
@@ -944,6 +958,7 @@ exports[`gatsby-plugin-sharp fluid does not change the arguments object it is gi
944958
"pluginOptions": Object {
945959
"base64Width": 20,
946960
"defaultQuality": 50,
961+
"failOnError": true,
947962
"forceBase64Format": "",
948963
"lazyImageGeneration": true,
949964
"stripMetadata": true,
@@ -1013,6 +1028,7 @@ exports[`gatsby-plugin-sharp fluid ensure maxWidth is in srcSet breakpoints 1`]
10131028
"pluginOptions": Object {
10141029
"base64Width": 20,
10151030
"defaultQuality": 50,
1031+
"failOnError": true,
10161032
"forceBase64Format": "",
10171033
"lazyImageGeneration": true,
10181034
"stripMetadata": true,
@@ -1123,6 +1139,7 @@ exports[`gatsby-plugin-sharp fluid infers the maxWidth if only maxHeight is give
11231139
"pluginOptions": Object {
11241140
"base64Width": 20,
11251141
"defaultQuality": 50,
1142+
"failOnError": true,
11261143
"forceBase64Format": "",
11271144
"lazyImageGeneration": true,
11281145
"stripMetadata": true,
@@ -1164,6 +1181,7 @@ exports[`gatsby-plugin-sharp fluid keeps original file name 1`] = `
11641181
"pluginOptions": Object {
11651182
"base64Width": 20,
11661183
"defaultQuality": 50,
1184+
"failOnError": true,
11671185
"forceBase64Format": "",
11681186
"lazyImageGeneration": true,
11691187
"stripMetadata": true,
@@ -1226,6 +1244,7 @@ exports[`gatsby-plugin-sharp fluid prevents duplicate breakpoints 1`] = `
12261244
"pluginOptions": Object {
12271245
"base64Width": 20,
12281246
"defaultQuality": 50,
1247+
"failOnError": true,
12291248
"forceBase64Format": "",
12301249
"lazyImageGeneration": true,
12311250
"stripMetadata": true,
@@ -1295,6 +1314,7 @@ exports[`gatsby-plugin-sharp fluid reject any breakpoints larger than the origin
12951314
"pluginOptions": Object {
12961315
"base64Width": 20,
12971316
"defaultQuality": 50,
1317+
"failOnError": true,
12981318
"forceBase64Format": "",
12991319
"lazyImageGeneration": true,
13001320
"stripMetadata": true,
@@ -1336,6 +1356,7 @@ exports[`gatsby-plugin-sharp fluid should give the same result with createJob as
13361356
"pluginOptions": Object {
13371357
"base64Width": 20,
13381358
"defaultQuality": 50,
1359+
"failOnError": true,
13391360
"forceBase64Format": "",
13401361
"lazyImageGeneration": true,
13411362
"stripMetadata": true,
@@ -1392,6 +1413,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJob file name works w
13921413
"pluginOptions": Object {
13931414
"base64Width": 20,
13941415
"defaultQuality": 50,
1416+
"failOnError": true,
13951417
"forceBase64Format": "",
13961418
"lazyImageGeneration": true,
13971419
"stripMetadata": true,
@@ -1435,6 +1457,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJob should round heig
14351457
"pluginOptions": Object {
14361458
"base64Width": 20,
14371459
"defaultQuality": 50,
1460+
"failOnError": true,
14381461
"forceBase64Format": "",
14391462
"lazyImageGeneration": true,
14401463
"stripMetadata": true,
@@ -1478,6 +1501,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJobV2 file name works
14781501
"pluginOptions": Object {
14791502
"base64Width": 20,
14801503
"defaultQuality": 50,
1504+
"failOnError": true,
14811505
"forceBase64Format": "",
14821506
"lazyImageGeneration": true,
14831507
"stripMetadata": true,
@@ -1519,6 +1543,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJobV2 should round he
15191543
"pluginOptions": Object {
15201544
"base64Width": 20,
15211545
"defaultQuality": 50,
1546+
"failOnError": true,
15221547
"forceBase64Format": "",
15231548
"lazyImageGeneration": true,
15241549
"stripMetadata": true,

packages/gatsby-plugin-sharp/src/gatsby-node.js

+1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
7878
),
7979
stripMetadata: Joi.boolean().default(true),
8080
defaultQuality: Joi.number().default(50),
81+
failOnError: Joi.boolean().default(true),
8182
})
8283
}

packages/gatsby-plugin-sharp/src/plugin-options.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const pluginDefaults = {
88
stripMetadata: true,
99
lazyImageGeneration: true,
1010
defaultQuality: 50,
11+
failOnError: true, // matches default of the sharp api constructor (https://sharp.pixelplumbing.com/api-constructor)
1112
}
1213

1314
const generalArgs = {

packages/gatsby-plugin-sharp/src/process-file.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ sharp.concurrency(cpuCoreCount())
6161
exports.processFile = (file, transforms, options = {}) => {
6262
let pipeline
6363
try {
64-
pipeline = sharp(file)
64+
pipeline = !options.failOnError
65+
? sharp(file, { failOnError: false })
66+
: sharp(file)
6567

6668
// Keep Metadata
6769
if (!options.stripMetadata) {

0 commit comments

Comments
 (0)