Skip to content

Commit

Permalink
fix(gatsby-plugin-sharp): Pass failOn to all pipelines (#37177)
Browse files Browse the repository at this point in the history
Co-authored By: Jeff Tian <jeff.tian@outlook.com>
  • Loading branch information
LekoArts committed Dec 8, 2022
1 parent b7d7b07 commit 639ae08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 99 deletions.
103 changes: 19 additions & 84 deletions packages/gatsby-plugin-sharp/README.md
Expand Up @@ -26,35 +26,28 @@ npm install gatsby-plugin-sharp

## How to use

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-sharp`,
options: {
// Defaults used for gatsbyImageData and StaticImage
defaults: {},
// Set to none to allow builds to continue on image errors
failOn: `none`,
// deprecated options and their defaults:
base64Width: 20,
forceBase64Format: ``, // valid formats: png,jpg,webp
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
stripMetadata: true,
defaultQuality: 50,
```js:title=gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-sharp`,
options: {
// Defaults used for gatsbyImageData and StaticImage
defaults: {},
// Relates to "options.failOn" in https://sharp.pixelplumbing.com/api-constructor#parameters
failOn: `warning`,
},
},
},
]
]
}
```

## Options

- `defaults`: default values used for `gatsbyImageData` and `StaticImage` from [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image).
- `defaults`: Default values used for `gatsbyImageData` and `StaticImage` from [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image).
Available options are: `formats`,`placeholder`,`quality`,`breakpoints`,`backgroundColor`,`tracedSVGOptions`,`blurredOptions`,`jpgOptions`,`pngOptions`,`webpOptions`,`avifOptions`.
For details of these, see [the reference guide](https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-plugin-image).
- `failOn`: default = `warning`. By default builds will fail if there is a corrupted image. Set to `none` to continue the build on error. The image will return `undefined`. You can customize this option, see [`options.failOn`](https://sharp.pixelplumbing.com/api-constructor#parameters).

Other options are deprecated, and should only be used for the legacy `fixed` and `fluid` functions.
- `failOn`: default = `warning`. By default, builds will fail if sharp finds an image with corrupted pixel values. When setting `failOn` to `none` the image will return `undefined` instead. You can customize this option, see [`options.failOn`](https://sharp.pixelplumbing.com/api-constructor#parameters). Images with corrupt image headers/metadata will always fail, regardless of this setting.

## Methods

Expand Down Expand Up @@ -215,8 +208,8 @@ Rotate the image (after cropping). See Sharp's [rotate][7].

#### grayscale

Uses Sharp's [greyscale][8] to convert the source image to 8-bit greyscale, 256
shades of grey, e.g.
Uses Sharp's [grayscale][8] to convert the source image to 8-bit grayscale, 256
shades of gray, e.g.

```graphql
allImageSharp {
Expand Down Expand Up @@ -287,67 +280,9 @@ quoting the Sharp documentation:
> <a href="https://en.wikipedia.org/wiki/Alpha_compositing">premultiplication</a>
> will occur.
#### tracedSVG

Generates a traced SVG of the image (see [the original GitHub issue][9]) and
returns the SVG as "[optimized URL-encoded][10]" `data:` URI. It used in
[gatsby-image](/plugins/gatsby-image/) to provide an
alternative to the default inline base64 placeholder image.

Uses [node-potrace][11] and [SVGO][12] under the hood. Default settings for
node-potrace:

```javascript
{
color: `lightgray`,
optTolerance: 0.4,
turdSize: 100,
turnPolicy: TURNPOLICY_MAJORITY,
}
```

All [node-potrace `Potrace` parameters][13] are exposed and can be set via the
`traceSVG` argument:

```javascript
fixed(
traceSVG: {
color: "#f00e2e"
turnPolicy: TURNPOLICY_MINORITY
blackOnWhite: false
}
) {
src
srcSet
tracedSVG
}
```

### Setting a default quality

You can pass a default image quality to `sharp` by setting the `defaultQuality` option.

### Using MozJPEG

You can opt-in to use [MozJPEG][16] for jpeg-encoding. MozJPEG provides even
better image compression than the default encoder used in `gatsby-plugin-sharp`.
However, when using MozJPEG the build time of your Gatsby project will increase
significantly.

To enable MozJPEG, you can set the `useMozJpeg` plugin option to `true` in
`gatsby-config.js`.

For backwards compatible reasons, if `useMozJpeg` is not defined in the plugin
options, the [environment variable](/docs/environment-variables/#environment-variables)
`GATSBY_JPEG_ENCODER` acts as a fallback if set to `MOZJPEG`:

```shell
GATSBY_JPEG_ENCODER=MOZJPEG
```

### Allow build to continue on image processing error
### Setting sharp's level of sensitivity to invalid images

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.
By default, the build will fail when sharp encounters an error while processing an image. You can change parts of this behavior by changing the `failOn` setting to `none`. In that case sharp will then ignore any errors relating to the pixel values/file structure of your file. However, if your image has corrupt image headers/metadata the build will still fail. 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

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/src/gatsby-node.js
Expand Up @@ -208,7 +208,7 @@ exports.pluginOptionsSchema = ({ Joi }) =>
),
stripMetadata: Joi.boolean().default(true),
defaultQuality: Joi.number().default(50),
// TODO(v5): Remove deprecated failOnError option
// TODO(v6): Remove deprecated failOnError option
failOnError: Joi.boolean().default(true),
failOn: Joi.any()
.valid(`none`, `truncated`, `error`, `warning`)
Expand Down
11 changes: 0 additions & 11 deletions packages/gatsby-plugin-sharp/src/image-data.ts
Expand Up @@ -396,17 +396,6 @@ export async function generateImageData({
imageProps.placeholder = {
fallback,
}
} else if (placeholder === `tracedSVG`) {
const fallback: string = await traceSVG({
file,
args: tracedSVGOptions,
fileArgs: args,
cache,
reporter,
})
imageProps.placeholder = {
fallback,
}
} else if (metadata?.dominantColor) {
imageProps.backgroundColor = metadata.dominantColor
}
Expand Down
8 changes: 5 additions & 3 deletions packages/gatsby-plugin-sharp/src/index.js
Expand Up @@ -395,9 +395,10 @@ async function traceSVG(args) {
}

async function stats({ file, reporter }) {
const pluginOptions = getPluginOptions()
let imgStats
try {
const pipeline = sharp()
const pipeline = sharp({ failOn: pluginOptions.failOn })
fs.createReadStream(file.absolutePath).pipe(pipeline)

imgStats = await pipeline.stats()
Expand All @@ -417,11 +418,12 @@ async function stats({ file, reporter }) {

let didShowTraceSVGRemovalWarningFluid = false
async function fluid({ file, args = {}, reporter, cache }) {
const options = healOptions(getPluginOptions(), args, file.extension)
const pluginOptions = getPluginOptions()
const options = healOptions(pluginOptions, args, file.extension)

let metadata
try {
const pipeline = sharp()
const pipeline = sharp({ failOn: pluginOptions.failOn })
fs.createReadStream(file.absolutePath).pipe(pipeline)

metadata = await pipeline.metadata()
Expand Down

0 comments on commit 639ae08

Please sign in to comment.