You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/gatsby-plugin-image/README.md
+58-127
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,40 @@
1
-
# Experimental image plugin
1
+
# gatsby-plugin-image (beta)
2
2
3
-
This plugin is a replacement for gatsby-image. It adds [static images](#static-images), and a [new higher-performance gatsby-image component](#gatsby-image-next-generation).
4
-
It also adds [a new GraphQL resolver](#graphql-resolver) to gatsby-transformer-sharp
3
+
This plugin is a replacement for gatsby-image. It adds [static images](#staticimage), and a [new higher-performance gatsby-image component](#gatsbyimage). It also adds [a new GraphQL resolver](#graphql-resolver) to gatsby-transformer-sharp.
5
4
6
-
This package is in alpha, and the API will change. It is not ready for production use yet, but feedback would be great.
5
+
## Contents
6
+
7
+
-[StaticImage](#staticimage) - the new static image component
8
+
-[GatsbyImage](#gatsbyimage) - a high-performance gatsby-image component
9
+
-[gatsbyImageData](#graphql-resolver) - a simpler GraphQL API
7
10
8
11
## Usage
9
12
10
-
Install `gatsby-plugin-image` and `gatsby-plugin-sharp`, then add them to your `gatsby-config.js`. Upgrade `gatsby` to at least `2.24.78`.
13
+
1.Install `gatsby-plugin-image` and `gatsby-plugin-sharp`:
This plugin is a proof of concept for a simpler way to use Gatsby's image processing tools and components without needing to write GraphQL queries. It is designed for static images such as logos rather than ones loaded dynamically from a CMS.
19
+
If you're using the new `GatsbyImage` in addition to `StaticImage`, you'll also want to install `gatsby-transformer-sharp`.
Using this plugin, the code above can be written as follows:
35
+
# StaticImage
36
+
37
+
This component is a new, simpler way to use Gatsby's image processing tools and components without needing to write GraphQL queries. It is designed for static images such as logos rather than ones loaded dynamically from a CMS.
40
38
41
39
```js
42
40
importReactfrom"react"
@@ -49,7 +47,7 @@ export const Dino = () => (
49
47
50
48
The `src` prop is relative to the source file, like in static HTML.
51
49
52
-
You can pass the same options as those available via `ImageSharp` queries:
50
+
You can pass the same options as those available via [`gatsbyImageData`](#graphql-resolver) queries:
When your site is compiled, any references to StaticImage components are extracted, the images are resized by Sharp in a similar way to `gatsby-transformer-sharp`, and then the resulting sharp object is written to `.cache/caches/gatsby-plugin-image/`, with the filename generated as a hash of the normalized image props. Next, a Babel plugin finds any references to StaticImage, calculates the same hash, then adds a `require()` to that JSON file as a new `__imageData` prop. It then returns a GatsbyImage using that **imageData. Errors don't cause the build to fail, but instead are written to the component as an `**error` prop, which is then logged in develop.
96
-
97
68
### Are there restrictions to how this is used?
98
69
99
-
The props must be able to be statically-analyzed at build time. You can't pass them as props from outside the component, or use the results of function calls, for example.
70
+
Because the images still need to be resized during build, the props must be able to be statically-analyzed at build time. You can't pass them as props from outside the component, or use the results of function calls, for example.
The only required prop is `src`. The default type is `fixed`. The other props match those of [the new GatsbyImage component](#gatsby-image-next-generation)
114
+
The only required prop is `src`. The default type is `fixed`. The other props match those of [the new GatsbyImage component](#gatsbyimage). You can also pass in options which are forwarded to [`gatsbyImageData`](#graphql-resolver).
159
115
160
-
## gatsby-image next generation
116
+
## GatsbyImage
161
117
162
118
Speedy, optimized images without the work.
163
119
164
-
gatsby-image is a React component specially designed to give your users a great image experience. It combines speed and best practices. You can use any image processing library that you want. We suggest using gatsby-plugin-sharp as your image processor. Saving images locally improves [the important health metrics](https://web.dev/vitals/) for your site.
120
+
GatsbyImage is a React component specially designed to give your users a great image experience. It combines speed and best practices.
165
121
166
-
Note: gatsby-image is not a drop-in replacement for <img />. It's optimized for fixed width/height images and images that stretch the full-width of a container. You can build your own Gatsby-Image with the utilities we export from this package.
122
+
Note: GatsbyImage is not a drop-in replacement for `<img>`. It's optimized for fixed width/height images and images that stretch the full-width of a container. You can build your own GatsbyImage with the utilities we export from this package.
167
123
168
124
## Table of Contents
169
125
170
126
-[Problem](#problem)
171
127
-[Solution](#solution)
172
-
- [Install](#install)
173
128
-[How to use](#how-to-use)
174
129
-[Types of Responsive Images](#three-types-of-responsive-images)
175
130
-[Gatsby Image Props](#gatsby-plugin-image-props)
@@ -208,34 +163,14 @@ With Gatsby, we can make images way _way_ better.
208
163
processing capabilities powered by GraphQL and Sharp. To produce perfect images,
209
164
you need only:
210
165
211
-
1. Import `{ GatsbyImage } from "gatsby-plugin-image"` and use it in place of the built-in `img`.
166
+
1. Import `{ GatsbyImage } from "gatsby-plugin-image"`.
212
167
2. Write a GraphQL query with all necessary fields needed by `gatsby-plugin-image`.
213
168
214
169
The GraphQL query creates multiple thumbnails with optimized JPEG and PNG
215
170
compression. The `gatsby-plugin-image` component automatically sets up the "blur-up"
216
171
effect as well as lazy loading of images further down the screen.
217
172
218
-
## Install
219
-
220
-
`npm install gatsby-plugin-image`
221
-
222
-
Depending on the gatsby starter you used, you may need to include [gatsby-transformer-sharp](/packages/gatsby-transformer-sharp/) and [gatsby-plugin-sharp](/packages/gatsby-plugin-sharp/) as well, and make sure they are installed and included in your gatsby-config.
Also, make sure you have set up a source plugin, so your images are available in GraphQL queries. For example, if your images live in a project folder on the local filesystem, you would set up `gatsby-source-filesystem` in `gatsby-config.js` like so:
173
+
Make sure you have set up a source plugin, so your images are available in GraphQL queries. For example, if your images live in a project folder on the local filesystem, you would set up `gatsby-source-filesystem` in `gatsby-config.js` like so:
@@ -354,44 +287,42 @@ In Gatsby's GraphQL implementation, you specify the type of image with the `layo
354
287
355
288
# GraphQL resolver
356
289
357
-
We have added a new resolver to the `ImageSharp` node, with a single field `imageData`. Unlike the existing `fixed` and `fluid` resolvers, this returns a
290
+
We have added a new `gatsbyImageData`resolver to the `ImageSharp` node. Unlike the existing `fixed` and `fluid` resolvers, this returns a
358
291
JSON type, meaning you don't specify the individual fields, but are instead given the whole object. This is because the object is then passed in to the `<GatsbyImage>` component. The API is like this:
The helper function `getImage` takes a file node and returns `file?.childImageSharp?.gatsbyImage?.imageData`
312
+
The optional helper function `getImage` takes a file node and returns `file?.childImageSharp?.gatsbyImageData`
380
313
381
314
Because this no longer uses fragments to specify which fields to return, it instead uses arguments passed to the resolver. These include:
382
315
383
316
-`placeholder`: Format of generated placeholder image.
384
-
DOMINANT*COLOR: a solid color, calculated from the dominant color of the image. (default) \_Currently disabled until sharp is updated*
385
-
BLURRED: a blurred, lowresolution image, encoded as a base64 data URI
386
-
TRACED_SVG: a low-resolution traced SVG of the image.
387
-
NONE: no placeholder. Set "background" to use a fixed background color.
317
+
-`BLURRED`: (default) a blurred, low resolution image, encoded as a base64 data URI
318
+
-`TRACED_SVG`: a low-resolution traced SVG of the image.
319
+
-`NONE`: no placeholder. Set "background" to use a fixed background color.
320
+
-`DOMINANT_COLOR`: a solid color, calculated from the dominant color of the image. _Currently disabled until sharp is updated_
388
321
-`layout`: The layout for the image.
389
-
FIXED: A static image sized, that does not resize according to the screen width
390
-
FLUID: The image resizes to fit its container. Pass a "sizes" option if it isn't going to be the full width of the screen.
391
-
CONSTRAINED: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
392
-
- `outputPixelDensities`: A list of image pixel densities to generate, for high-resolution (retina) screens. It will never generate images larger than the source, and will always a 1x image.
393
-
Default is [ 0.25, 0.5, 1, 2 ], for fluid/constrained images, and [ 1, 2 ] for fixed. In this case, an image with a fluid layout and maxWidth = 400 would generate images at 100, 200, 400 and 800px wide
394
-
395
-
- `sizes`: The "sizes" property, passed to the img tag. This describes the display size of the image.
396
-
This does not affect the generated images, but is used by the browser to decide which images to download. You can leave this blank for fixed images, or if the responsive image
397
-
container will be the full width of the screen. In these cases we will generate an appropriate value.
322
+
-`FIXED:` A static image sized, that does not resize according to the screen width
323
+
-`FLUID`: The image resizes to fit its container. Pass a "sizes" option if it isn't going to be the full width of the screen.
324
+
-`CONSTRAINED`: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
325
+
-`outputPixelDensities`: A list of image pixel densities to generate, for high-resolution (retina) screens. It will never generate images larger than the source, and will always include a 1x image.
326
+
Default is `[ 0.25, 0.5, 1, 2 ]`, for fluid/constrained images, and `[ 1, 2 ]` for fixed. In this case, an image with a fluid layout and maxWidth = 400 would generate images at 100, 200, 400 and 800px wide
327
+
-`sizes`: The "[sizes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images)" attribute, passed to the `<img>` tag. This describes the display size of the image. This does not affect the generated images, but is used by the browser to decide which images to download. You can leave this blank for fixed images, or if the responsive image container will be the full width of the screen. In these cases we will generate an appropriate value. If, however, you are generating responsive images that are not the full width of the screen, you should provide a sizes property for best performance.
328
+
-`formats`: an array of file formats to generate. The default is `[AUTO, WEBP]`, which means it will generate images in the same format as the source image, as well as in the next-generation [WebP](https://developers.google.com/speed/webp) format. We strongly recommend you do not change this option, as doing so will affect performance scores.
0 commit comments