Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-sharp): added option to allow sharp process to continue with errors #27345

Merged
merged 5 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-sharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ plugins: [
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
stripMetadata: true,
defaultQuality: 50,
failOnError: true,
},
},
]
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -426,6 +432,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -506,6 +513,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -586,6 +594,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -666,6 +675,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -724,6 +734,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -786,6 +797,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -848,6 +860,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -910,6 +923,7 @@ Array [
"pluginOptions": Object {
"base64Width": 20,
"defaultQuality": 50,
"failOnError": true,
"forceBase64Format": "",
"lazyImageGeneration": true,
"stripMetadata": true,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/src/plugin-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-plugin-sharp/src/process-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +64 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pipeline = sharp(file, { failOnError: options.failOnError })

why not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or

pipeline = sharp(file, { failOnError: !!options.failOnError })


// Keep Metadata
if (!options.stripMetadata) {
Expand Down