From 9b1dfe49c61c9a8aa01c93d9afc519c47b62c5fa Mon Sep 17 00:00:00 2001 From: Joe Sanchez Jr Date: Thu, 8 Oct 2020 11:13:39 -0500 Subject: [PATCH 1/4] added failOnError option to gatsby-plugin-sharp options --- packages/gatsby-plugin-sharp/src/plugin-options.js | 1 + packages/gatsby-plugin-sharp/src/process-file.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-sharp/src/plugin-options.js b/packages/gatsby-plugin-sharp/src/plugin-options.js index d024bd1dda8e1..10f6bf988666b 100644 --- a/packages/gatsby-plugin-sharp/src/plugin-options.js +++ b/packages/gatsby-plugin-sharp/src/plugin-options.js @@ -8,6 +8,7 @@ const pluginDefaults = { stripMetadata: true, lazyImageGeneration: true, defaultQuality: 50, + failOnError: true, // matches default of the sharp api constructor (https://sharp.pixelplumbing.com/api-constructor) } const generalArgs = { diff --git a/packages/gatsby-plugin-sharp/src/process-file.js b/packages/gatsby-plugin-sharp/src/process-file.js index be5e84f53c371..00e31434e54f1 100644 --- a/packages/gatsby-plugin-sharp/src/process-file.js +++ b/packages/gatsby-plugin-sharp/src/process-file.js @@ -61,7 +61,9 @@ sharp.concurrency(cpuCoreCount()) exports.processFile = (file, transforms, options = {}) => { let pipeline try { - pipeline = sharp(file) + pipeline = !options.failOnError + ? sharp(file, { failOnError: false }) + : sharp(file) // Keep Metadata if (!options.stripMetadata) { From 7f468bd3e7a13545de859248264c3a74bbd55a8b Mon Sep 17 00:00:00 2001 From: Joe Sanchez Jr Date: Thu, 8 Oct 2020 11:14:44 -0500 Subject: [PATCH 2/4] updated test snapshot to include "failOnError" default --- .../src/__tests__/__snapshots__/index.js.snap | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap index 3576938b7cdf9..437321e74ccc2 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap @@ -84,6 +84,7 @@ exports[`gatsby-plugin-sharp fixed correctly infers the width when only the heig "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -139,6 +140,7 @@ exports[`gatsby-plugin-sharp fixed does not warn when the requested width is equ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -194,6 +196,7 @@ exports[`gatsby-plugin-sharp fixed should give the same result with createJob as "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -235,6 +238,7 @@ exports[`gatsby-plugin-sharp fixed warns when the requested width is greater tha "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -304,6 +308,7 @@ exports[`gatsby-plugin-sharp fluid accepts srcSet breakpoints 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -345,6 +350,7 @@ exports[`gatsby-plugin-sharp fluid adds pathPrefix if defined 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -426,6 +432,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -506,6 +513,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -586,6 +594,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -666,6 +675,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -724,6 +734,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -786,6 +797,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -848,6 +860,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -910,6 +923,7 @@ Array [ "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -944,6 +958,7 @@ exports[`gatsby-plugin-sharp fluid does not change the arguments object it is gi "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1013,6 +1028,7 @@ exports[`gatsby-plugin-sharp fluid ensure maxWidth is in srcSet breakpoints 1`] "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1123,6 +1139,7 @@ exports[`gatsby-plugin-sharp fluid infers the maxWidth if only maxHeight is give "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1164,6 +1181,7 @@ exports[`gatsby-plugin-sharp fluid keeps original file name 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1226,6 +1244,7 @@ exports[`gatsby-plugin-sharp fluid prevents duplicate breakpoints 1`] = ` "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1295,6 +1314,7 @@ exports[`gatsby-plugin-sharp fluid reject any breakpoints larger than the origin "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1336,6 +1356,7 @@ exports[`gatsby-plugin-sharp fluid should give the same result with createJob as "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1392,6 +1413,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJob file name works w "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1435,6 +1457,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJob should round heig "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1478,6 +1501,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJobV2 file name works "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, @@ -1519,6 +1543,7 @@ exports[`gatsby-plugin-sharp queueImageResizing with createJobV2 should round he "pluginOptions": Object { "base64Width": 20, "defaultQuality": 50, + "failOnError": true, "forceBase64Format": "", "lazyImageGeneration": true, "stripMetadata": true, From 56d63dadae2202ee81b9a40f59af4b65f70c8c8c Mon Sep 17 00:00:00 2001 From: Joe Sanchez Jr Date: Fri, 9 Oct 2020 12:45:59 -0500 Subject: [PATCH 3/4] added note about new option for gatsby-plugin-sharp --- packages/gatsby-plugin-sharp/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/gatsby-plugin-sharp/README.md b/packages/gatsby-plugin-sharp/README.md index 719d084ac4ddd..4caa88cb59931 100644 --- a/packages/gatsby-plugin-sharp/README.md +++ b/packages/gatsby-plugin-sharp/README.md @@ -36,6 +36,7 @@ plugins: [ useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`, stripMetadata: true, defaultQuality: 50, + failOnError: true, }, }, ] @@ -332,6 +333,10 @@ options, the [environment variable](/docs/environment-variables/#environment-var GATSBY_JPEG_ENCODER=MOZJPEG ``` +### Allow build to continue on image processing error + +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. + ### EXIF and ICC metadata By default, `gatsby-plugin-sharp` strips all EXIF, ICC and other metadata From 735c19edad2d1353e1b4112bfaf919c9dc609822 Mon Sep 17 00:00:00 2001 From: Joe Sanchez Jr Date: Mon, 12 Oct 2020 12:57:31 -0500 Subject: [PATCH 4/4] added failOnError to validation object --- packages/gatsby-plugin-sharp/src/gatsby-node.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/gatsby-plugin-sharp/src/gatsby-node.js b/packages/gatsby-plugin-sharp/src/gatsby-node.js index 6cc87fb270d8d..b2f9db348ef89 100644 --- a/packages/gatsby-plugin-sharp/src/gatsby-node.js +++ b/packages/gatsby-plugin-sharp/src/gatsby-node.js @@ -78,5 +78,6 @@ if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) { ), stripMetadata: Joi.boolean().default(true), defaultQuality: Joi.number().default(50), + failOnError: Joi.boolean().default(true), }) }