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

docs(gatsby-plugin-image): Initial migration guide #29036

Merged
merged 55 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
b2a2cd6
initial migration guide
Jan 14, 2021
fc11bd7
review notes
Jan 15, 2021
0bc2213
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
e1440e3
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
e6cb136
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
2b3b385
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
c353db4
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
53477aa
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
a9eb2b5
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
0cf7a0f
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
da30f42
reword per matt's review
Jan 21, 2021
4775321
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 21, 2021
c1a8971
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
db3365b
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
66d6d9a
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
50004ab
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
36145cf
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
9228736
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
0eb9b51
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
664bb23
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
9316863
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
6638257
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
c4be214
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
6454cfb
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
eab7c36
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
20556b5
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
13c4afb
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
0a413fe
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
ebbdf25
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
bbb0acf
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
d1bcf04
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
5740c27
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
07b4465
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
e11fe90
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
233b29b
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
7677c58
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
b53f834
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
25204e9
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
3b3c3b1
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
396e9dc
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
6f563e4
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
398951f
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
0b64d9a
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
e98ab26
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
7cbf5a2
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
ee5c423
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
c656c04
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 22, 2021
dfa3d22
not perfect but it'll work
Jan 22, 2021
8427777
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 25, 2021
e90efc9
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 25, 2021
8f043d0
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 25, 2021
b470378
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 25, 2021
fe21c99
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 25, 2021
3e8c7cc
Update docs/docs/reference/release-notes/image-migration-guide.md
Jan 25, 2021
7986572
Merge branch 'master' into image-migration-guide
Jan 25, 2021
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
234 changes: 234 additions & 0 deletions docs/docs/reference/release-notes/image-migration-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
---
title: Migrating from gatsby-image to gatsby-plugin-image
---

This document is provided to help users of `gatsby-image` migrate to using `gatsby-plugin-image`.

Note that you can use both packages at the same time. You may need to in situations where you're using a `gatsby-image`-compatible CMS as a data source, and that CMS plugin has not yet updated to a version compatible with the new API.

If you're looking for other documentation on the new plugin, see:

- How-To Guide: [Using the beta Gatsby Image plugin](/docs/how-to/images-and-media/using-gatsby-plugin-image/)
- Reference Guide: [Gatsby Image plugin](/docs/reference/built-in-components/gatsby-plugin-image/)

## What changed?

### New Syntax

The new plugin requires significant syntax changes. We've provided a [codemod](#codemod-instructions) to help you migrate, but this section will explain the changes.

#### Import change

Previously, `GatsbyImage` was the default export from `gatsby-image`. With `gatsby-plugin-image`, the component is now a named export.

```javascript
// import Img from "gatsby-image"
import { GatsbyImage } from "gatsby-plugin-image"
```

#### GraphQL changes

This is one of the bigger changes. There are no more fragments to use. Instead, things like `layout` and `format` are passed as arguments to the resolver.

This is the previous syntax.

```javascript
import { graphql } from "gatsby"

export const query = graphql`
{
file(relativePath: { eq: "images/example.jpg" }) {
childImageSharp {
fixed {
...GatsbyImageSharpFixed
}
}
}
}
`
```

The new syntax looks like this. The fragment is removed in favor of `gatsbyImageData`, which is used for all images. Previous differences - such as `fixed` vs. `fluid`, WebP generation, and placeholder type - are passed as arguments to the resolver.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import { graphql } from "gatsby"

export const query = graphql`
{
file(relativePath: { eq: "images/example.jpg" }) {
childImageSharp {
gatsbyImageData(layout: FIXED)
}
}
}
`
```

Other changes to the available argument structure are in the section on [API changes](#api-changes).

#### Component changes

The last change is to the JSX component. The import name is potentially different, and the query result is also different.

```javascript
// import Img from "gatsby-image"
import { GatsbyImage } from "gatsby-plugin-image"

const HomePage = ({ data }) => {
return (
// <Img fixed={data.file.childImageSharp.fixed} />
<GatsbyImage image={data.file.childImageSharp.gatsbyImageData} />
)
}
```

### API Changes

In addition to the syntax changes for using `gatsby-plugin-image`, there are also changes to the API that affect the resolver arguments (and `StaticImage`).
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

#### fluid

The `fluid` image type has been deprecated in favor of two alternatives.

The first is an image type called `fullWidth`. This image is designed to be used for things like hero images and generates image sizes accordingly. Instead of passing something like `maxWidth`, it takes an array called `breakpoints` that will generate images designed for those screen sizes. In most cases you will not need to provide these, as there are default values that work for most sites. Note that `fullWidth` images will expand to fit the width of their container, even if that width is larger than the source image.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

The second is a responsive image type called `constrained` that generates smaller images, but nothing larger than the original image source size. Additionally, you can pass `width` and/or `height` to limit the displayed image to that size. In this case, larger images may be generated for high-density screens.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

#### maxWidth

`maxWidth` and `maxHeight` are deprecated for all image types. For `constrained` and `fixed` images, use `width` and `height`. For `fullWidth` images, look at the `breakpoints` option.

#### aspectRatio

`aspectRatio` is a new argument that takes a number (or fraction). If you pass it without also passing in `width` or `height`, it will default to using the source image width and then adjusting the height to the correct aspect ratio. Alternatively, you can pass your own `width` or `height` and it will set the other dimension. Passing both `height` and `width` will cause `aspectRatio` to be ignored in favor of the inferred aspect ratio.

#### formats

Previously, images generated their own type, e.g. JPG, PNG, etc. You could also generate WebP images when using the appropriate fragment. This is now controlled using the `formats` argument. This field takes an array, `[AUTO, WEBP]` by default, where `AUTO` means the same format as the source image.`AVIF` is now also a valid format.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

#### Options nested inside objects

Previously, transformations like `grayscale` and quality options such as `pngQuality` were top level query arguments. This has changed.

`grayscale` now exists within the `transformOptions` argument, and `pngQuality` inside `pngOptions`. See the [`gatsby-plugin-image` How-To Guide](/docs/how-to/images-and-media/using-gatsby-plugin-image/) for specifics.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

### Breaking changes

Due to the changes to `gatsby-plugin-image`, there is some functionality that is no longer supported.

1. `GatsbyImage` is no longer a class component and therefore cannot be extended. You can use [composition](https://reactjs.org/docs/composition-vs-inheritance.html) instead.
2. `fluid` images no longer exist, and the `fullWidth` replacement does not take `maxWidth` or `maxHeight`.
3. Art direction is no longer supported.
4. The component no longer takes a decomposed object, and the following code is not valid. You should avoid accessing or changing the contents of the `gatsbyImageData` object, as it is not considered to be a public API, so can be changed without notice.

```javascript
// THIS IS NOT VALID
<GatsbyImage image={{ src: example.src, srcSet: ``, width: 100 }} />
```

## How to Migrate

1. Install and update dependencies.

```shell
npm install gatsby-plugin-image gatsby-plugin-sharp gatsby-transformer-sharp
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
```

2. Configure plugins.

```javascript:title=gatsby-config.js
module.exports = {
plugins: [
`gatsby-plugin-image`,
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
],
}
```

3. Run the codemod.

Note that if you need to do a partial migration, e.g. because you're using a CMS that doesn't yet support the new plugin alongside local image files, you'll want to make use of the `optional-path` to run against individual files.

```shell
npx gatsby-codemods gatsby-plugin-image <optional-path>
```

Without an `optional-path`, the codemod will run against all the files in your current directory, so running it in root is recommended. It will ignore `node_modules`, `.cache`, and `public` automatically. It will also respect any local Babel configuration in your project. The codemod is designed to run against files with the extensions `.ts`, `.js`, `.tsx`, and `.jsx`. If this does not cover your project, or you require other customizations, see the section on using [`jscodeshift`](#jscodeshift).

Due to the API changes, the codemod is not a pure 1:1 mapping. There are some changes introduced.

* Fluid images will map to either `fullWidth` or `constrained` images. This decision is made based on the existence of `maxWidth` and its value. If `maxWidth` does not exist, it will be a `fullWidth` image. If it does, and the `maxWidth` is less than 1000, it will be a `constrained` image, otherwise a `fullWidth` image. `fullWidth` images do not retain their `maxWidth` or `maxHeight` fields; `constrained` images do, as `width` and `height`.

2. All images will generate WebP.

The API change to options is not currently codemoded. TODO: should this be?
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

The codemod will warn the user in a number of different scenarios and point you to the file in question so you can inspect the changes manually.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

4. Consider manual changes
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

For images using static query, you should move to use the `StaticImage` component instead. This component takes `src` which can be a remote image URL or a relative path to an image. Make sure you've installed `gatsby-source-filesystem` if you're going to use this component.
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import { StaticImage } from "gatsby-plugin-image"

const HomePage = () => (
<StaticImage src="./example.jpg" alt="please include an alt" />
)
}
```
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

You may also consider refactoring code to make use of the `getImage` helper function.
Copy link
Contributor

Choose a reason for hiding this comment

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

What would be the considerations they're weighing? Like how would they know if this is a good use case for getImage?

Could just be one sentence like, "This might be a good approach if..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We sort of want everyone to use it going forward, but for those migrating it's likely too much manual work to bother with. So I sort of just threw it in there as an FYI.


```javascript
import { getImage, GatsbyImage } from "gatsby-plugin-image"

const HomePage = ({ data }) => {
const image = getImage(data.file)
return (
<>
<GatsbyImage image={image} alt="please include an alt" />
</>
)
}
```
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

Finally, if you were previously using `src`, e.g. for an SEO component, you'll want to use the `getSrc` helper function as the internal structure of the return object has changed.

```javascript
import { getSrc } from "gatsby-plugin-image"

const HomePage = ({ data }) => {
const imagePath = getSrc(data.file)
return (
<>
<SEO imageSrc={imagePath} />
</>
)
}
```
laurieontech marked this conversation as resolved.
Show resolved Hide resolved

## `jscodeshift`

This section is for people who need to run the codemod with extra flags exposed by `jscodeshift`, e.g. to transform file types other than those with extensions `.js`, `.ts`, `.tsx`, or `.jsx`.

1. Install `jscodeshift`.

```shell
npm install --global jscodeshift
```

2. Install the codemods package in your project.

```shell
npm install gatsby-codemods
```

3. Run the codemod using `jscodeshift`.

```shell
jscodeshift -t node_modules/gatsby-codemods/transforms/gatsby-plugin-image.js .
```

See the [jscodeshift docs](https://github.com/facebook/jscodeshift) for all the available flags.
1 change: 0 additions & 1 deletion packages/gatsby-plugin-image/src/components/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function useGatsbyImage({
pluginName = `useGatsbyImage`,
...args
}: IGatsbyImageHelperArgs): IGatsbyImageData {
// TODO: use context to get default plugin options and spread them in here
return generateImageData({ pluginName, ...args })
}

Expand Down