-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[v2] Migration guide updates #5661
Changes from 15 commits
ddd19d6
8718398
bf0a458
231184a
00d68ee
8053835
bb64597
7afa9d9
f616086
163d7ea
e54d5f9
5413b73
d10adad
ca6d375
433833a
05b1e85
d0cebc7
9124ad1
c5e1c15
2be2dca
b8c4860
405073e
bfc9301
6af965a
d5beb1b
ad47463
ed23990
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,44 +2,43 @@ | |
title: Migrating from v1 to v2 | ||
--- | ||
|
||
## Install React, ReactDOM, and each plugins’ peer dependencies manually | ||
In v1, React and ReactDOM were magically resolved. This “feature” has been removed and you are now required to install them manually. | ||
> This document is a work in progress. Have you upgraded your site and run into something that's not covered here? Add your changes on GitHub! | ||
|
||
```bash | ||
npm i react react-dom | ||
``` | ||
## Introduction | ||
|
||
or | ||
This is a reference for upgrading your site from Gatsby v1 to Gatsby v2. While there's a lot covered here, you probably won't need to do everything for your site. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a ToC will be really helpful for making this easier to consume, e.g. "What we'll cover:" with bulleted anchor links. |
||
|
||
```bash | ||
yarn add react react-dom | ||
``` | ||
Let's start with the most important two steps - install React and update your layout components: | ||
|
||
Depending on the plugins you use, there may be more dependencies you need to install. For example: if you use typography.js, you now also need to install its dependencies. | ||
## Install React, ReactDOM, and each plugins’ peer dependencies manually | ||
|
||
In v1, the `react` and `react-dom` packages were included as part of the `gatsby` package. They are now `peerDependencies` so you are required to install them into your project. | ||
|
||
```bash | ||
npm i typography react-typography | ||
npm i react react-dom | ||
``` | ||
|
||
or | ||
Some plugins had dependencies that were also made peerDependencies. For example, if you use gatsby-plugin-typography, you now need to install: | ||
|
||
```bash | ||
yarn add typography react-typography | ||
npm i typography react-typography | ||
``` | ||
|
||
Search for the plugins that you use in [Gatsby’s plugins page](/plugins) and check their installation instructions. | ||
Search for the plugins that you use in the [plugin library](/plugins) and check their installation instructions for additional packages that now need installed. | ||
|
||
## Layout component | ||
|
||
The special layout component (`src/layouts/index.js`) that Gatsby v1 used to wrap every page has been removed. If the layout of your site appears to be broken, this is most likely the reason why. | ||
|
||
To learn more about the considerations behind this removal, read the [Gatsby RFC](https://github.com/gatsbyjs/rfcs/blob/master/text/0002-remove-special-layout-components.md). | ||
To learn more about the considerations behind this removal, read the [RFC for removing the special layout component](https://github.com/gatsbyjs/rfcs/blob/master/text/0002-remove-special-layout-components.md). | ||
|
||
The following is the recommended migration path: | ||
|
||
### 1. Convert children from function to normal prop (required) | ||
|
||
In v1, the `children` prop passed to layout was a function and needed to be executed. In v2, this is no longer the case. | ||
|
||
`layout in v1` | ||
Before: | ||
|
||
```jsx | ||
import React from "react" | ||
|
@@ -51,7 +50,7 @@ export default ({ children }) => ( | |
) | ||
``` | ||
|
||
`layout in v2` | ||
After: | ||
|
||
```jsx{5} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way to demonstrate these kind of changes that I've found more concise is to use a diff: export default ({ children }) => (
<div>
- {children()}
+ {children}
</div>
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
import React from "react" | ||
|
@@ -64,11 +63,13 @@ export default ({ children }) => ( | |
``` | ||
|
||
### 2. Move `layout/index.js` to `src/components/layout.js` (optional, but recommended) | ||
|
||
```bash | ||
git mv src/layouts/index.js src/components/layout.js | ||
``` | ||
|
||
### 3. Import and wrap pages with layout component | ||
|
||
Adhering to normal React composition model, you import the layout component and use it to wrap the content of the page. | ||
|
||
`src/pages/index.js` | ||
|
@@ -87,10 +88,12 @@ export default () => ( | |
Repeat for every page and template that needs this layout. | ||
|
||
### 4. Change query to use `StaticQuery` | ||
<!-- TODO: link StaticQuery text to StaticQuery doc page (if one will be made) --> | ||
|
||
Since layout is no longer special, you now need to make use of v2’s StaticQuery feature. | ||
|
||
`Query with layout in v1` | ||
> TODO: document StaticQuery and link to it from here | ||
|
||
Before: | ||
|
||
```jsx | ||
import React, { Fragment } from "react" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicky and mostly unrelated, but you can also skip the <>
<h1>Fragment shorthand!</h1>
<p>Yay!</p>
</> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicky is good :) |
||
|
@@ -116,7 +119,7 @@ export const query = graphql` | |
` | ||
``` | ||
|
||
`StaticQuery with layout in v2` | ||
After: | ||
|
||
```jsx | ||
import React, { Fragment } from "react" | ||
|
@@ -146,6 +149,7 @@ export default ({ children }) => ( | |
``` | ||
|
||
### 5. Pass `history`, `location`, and `match` props to layout | ||
|
||
In v1, layout component had access to `history`, `location`, and `match` props. In v2, only pages have access to these props; pass them to your layout component as needed. | ||
|
||
`layout` | ||
|
@@ -175,22 +179,203 @@ export default props => ( | |
``` | ||
|
||
## Rename `boundActionCreators` to `actions` | ||
|
||
`boundActionCreators` is deprecated in v2. You can continue using it, but it’s recommended that you rename it to `actions`. | ||
|
||
> TODO: document new actions - see [actions](/docs/actions) | ||
|
||
## Rename `pathContext` to `pageContext` | ||
|
||
Similar to `boundActionCreators` above, `pathContext` is deprecated in favor of `pageContext`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a breaking change? If so we should highlight that -- I don't believe boundActionCreators is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not — we pass both |
||
|
||
<!-- | ||
Taken from: https://github.com/gatsbyjs/gatsby/blob/v2/Breaking%20Changes.md | ||
* [] Remove postcss plugins (cssnext, cssimport) from default css loader config | ||
* [x] boundActionCreators => actions | ||
* [x] pathContext => pageContext | ||
* [] Source & transformer plugins now use UUIDs for ids. If you used glob or regex to query nodes by id then you'll need to query something else. | ||
* [] Mixed commonjs/es6 modules fail | ||
* [] Remove explicit polyfill and use the new builtins: usage support in babel 7. | ||
* [] Changed modifyBabelrc to onCreateBabelConfig | ||
* [] Changed modifyWebpackConfig to onCreateWebpackConfig | ||
* [] Inlining CSS changed — remove it from any custom html.js as done automatically by core now. | ||
* [x] Manually install react and react-dom, along with any dependencies required by your plugins. | ||
* [x] Layouts have been removed. To achieve the same behavior as v1, you have to wrap your pages and page templates with your own Layout component. Since Layout is a non-page component, making query has to be done with StaticQuery. | ||
--> | ||
## Rename responsive image queries | ||
|
||
The `sizes` and `resolutions` queries are deprecated in v2. These queries have been renamed to `fluid` and `fixed` to make them easier to understand. | ||
|
||
Before: | ||
|
||
```jsx | ||
const Example = ({ data }) => { | ||
<div> | ||
<Img sizes={data.foo.childImageSharp.sizes} /> | ||
<Img resolutions={data.bar.childImageSharp.resolutions} /> | ||
</div> | ||
} | ||
|
||
export default Example | ||
|
||
export const pageQuery = graphql` | ||
query IndexQuery { | ||
foo: file(relativePath: { regex: "/foo.jpg/" }) { | ||
childImageSharp { | ||
sizes(maxWidth: 700) { | ||
...GatsbyImageSharpSizes_tracedSVG | ||
} | ||
} | ||
} | ||
bar: file(relativePath: { regex: "/bar.jpg/" }) { | ||
childImageSharp { | ||
resolutions(width: 500) { | ||
...GatsbyImageSharpResolutions_withWebp | ||
} | ||
} | ||
} | ||
} | ||
` | ||
``` | ||
|
||
After: | ||
|
||
```jsx{3-4,14-15,21-22} | ||
const Example = ({ data }) => { | ||
<div> | ||
<Img fluid={data.foo.childImageSharp.fluid} /> | ||
<Img fixed={data.bar.childImageSharp.fixed} /> | ||
</div> | ||
} | ||
|
||
export default Example | ||
|
||
export const pageQuery = graphql` | ||
query IndexQuery { | ||
foo: file(relativePath: { regex: "/foo.jpg/" }) { | ||
childImageSharp { | ||
fluid(maxWidth: 700) { | ||
...GatsbyImageSharpFluid_tracedSVG | ||
} | ||
} | ||
} | ||
bar: file(relativePath: { regex: "/bar.jpg/" }) { | ||
childImageSharp { | ||
fixed(width: 500) { | ||
...GatsbyImageSharpFixed_withWebp | ||
} | ||
} | ||
} | ||
} | ||
` | ||
``` | ||
|
||
Further examples can be found in the [Gatsby Image docs](https://github.com/gatsbyjs/gatsby/tree/d0e29272ed7b009dae18d35d41a45e700cdcab0d/packages/gatsby-image). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a note that this is deprecated, but will continue working (similar to the |
||
|
||
## Manually specify postcss plugins | ||
|
||
Gatsby v2 removed `postcss-cssnext` and `postcss-import` from the default postcss setup. | ||
|
||
Use [`onCreateWebpackConfig`](/docs/add-custom-webpack-config) to specify your postcss plugins. | ||
|
||
Note: there will be a `postcss` plugin that allows you to configure postcss from a standard postcss config file. [Follow this discussion on issue 3284](https://github.com/gatsbyjs/gatsby/issues/3284). | ||
|
||
## Files mixing CommonJS with ES6 modules won't compile | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change this to something declarative, eg -- "Convert to either pure CommonJS or pure ES6"? |
||
|
||
Gatsby v2 uses babel 7 which is stricter about parsing files with mixed JS styles. | ||
|
||
ES6 Modules are ok: | ||
|
||
```js | ||
import foo from "foo" | ||
export default foo | ||
``` | ||
|
||
CommonJS is ok: | ||
|
||
```js | ||
const foo = require('foo'); | ||
module.exports = foo; | ||
``` | ||
|
||
Mixing `requires` and `export` is not ok: | ||
```js | ||
const foo = require('foo'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know the text is explaining this, but I'd still add a comment above each saying whether or not it's correct: // GOOD: ES modules syntax works
import foo from "foo"
export default foo // BAD: Mixed ES and CommonJS module syntax will cause failures
const foo = require('foo')
export default foo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 The Eslint docs do this too and it's really nice |
||
export default foo | ||
``` | ||
|
||
Mixing `import` and `module.exports` is not ok: | ||
```js | ||
import foo from "foo" | ||
module.exports = foo; | ||
``` | ||
|
||
See [Gatsby's babel docs for more details](/docs/babel). | ||
|
||
## Don't query nodes by ID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps eg "Rewrite node ID queries"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I think @m-allanson original wording is correct. There aren't "node ID queries" just queries that might try to query nodes by ID. |
||
|
||
Source and transformer plugins now use UUIDs for IDs. If you used glob or regex to query nodes by id then you'll need to query something else. | ||
|
||
[See the Pull Request that implemented this change](https://github.com/gatsbyjs/gatsby/pull/3807/files) | ||
|
||
> TODO: add example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a real example from one of my projects. 😄 query MyImageQuery {
allImageSharp(filter: {
- id: {regex: "/default.jpg/"}
+ sizes: {originalName: {regex: "/default.jpg/"}}
}) {
edges {
node {
id
sizes(maxWidth: 660) {
src
}
}
}
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks :) |
||
|
||
## Remove explicit polyfills | ||
|
||
If your Gatsby v1 site included any polyfills, you can remove them. Gatsby v2 ships with babel 7 and is configured to automatically include polyfills for your code. See [Gatsby's babel docs for more details](/docs/babel). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't appear to be actually true, or I'm misunderstanding. I had to ship an IntersectionObserver polyfill for marisamorby.com: https://github.com/jlengstorf/marisamorby.com/pull/8/files#diff-3863fe9c963d5c58d29a7f5af77e6480 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gatsby isn't yet compiling code in |
||
|
||
## Change `modifyBabelrc` to `onCreateBabelConfig` | ||
|
||
`modifyBabelrc` was renamed to [`onCreateBabelConfig`](/docs/node-apis/#modifyBabelrc) to bring it in line with the rest of Gatsby's API names. | ||
|
||
Before: | ||
|
||
```js | ||
exports.modifyBabelrc = ({ babelrc }) => { | ||
return { | ||
...babelrc, | ||
plugins: babelrc.plugins.concat([`foo`]), | ||
} | ||
} | ||
``` | ||
|
||
After: | ||
|
||
```js | ||
exports.onCreateBabelConfig = ({ actions }) => { | ||
actions.setBabelPlugin({ | ||
name: `babel-plugin-foo`, | ||
}) | ||
} | ||
``` | ||
|
||
Note usage of the new [`setBabelPlugin` action](/docs/actions/#setBabelPlugins). | ||
|
||
See [Gatsby's babel docs for more details](/docs/babel) about configuring babel. | ||
|
||
## Change `modifyWebpackConfig` to `onCreateWebpackConfig` | ||
|
||
`modifyWebpackConfig` was renamed to [`onCreateWebpackConfig`](/docs/node-apis/#onCreateWebpackConfig) to bring it in line with the rest of Gatsby's API names. | ||
|
||
Before: | ||
|
||
```js | ||
exports.modifyWebpackConfig = ({ config, stage }) => { | ||
switch (stage) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😱 a switch statement?! 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hah! I'll have you know this was copy and pasted from an official Gatsby plugin 🤣 |
||
case `build-javascript`: | ||
config.plugin(`Foo`, webpackFooPlugin, null) | ||
break | ||
} | ||
return config | ||
} | ||
``` | ||
|
||
|
||
After: | ||
|
||
```js | ||
exports.onCreateWebpackConfig = ({ stage, actions }) => { | ||
switch (stage) { | ||
case `build-javascript`: | ||
actions.setWebpackConfig({ | ||
plugins: [webpackFooPlugin], | ||
}) | ||
} | ||
} | ||
``` | ||
|
||
Note usage of the new [`setWebpackConfig` action](/docs/actions/#setWebpackConfig). | ||
|
||
See [Gatsby's webpack docs for more details](/docs/add-custom-webpack-config) about configuring webpack. | ||
|
||
## CSS is inlined automatically | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps "Remove inlined css in html.js"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
Gatsby v2 automatically inlines CSS. Gatsby v1 required you to create a [custom `html.js` file](/docs/custom-html) to do this. You can remove any custom CSS inlining from your custom `html.js`, in many cases this allows you to completely remove your `html.js` file. | ||
|
||
See an example in [this PR that upgrades the `using-remark` site to Gatsby v2](https://github.com/gatsbyjs/gatsby/commit/765b679cbc222fd5f527690427ee431cca7ccd61#diff-637c76e3c059ed8efacedf6e30de2d61). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link the "Add your changes on GitHub" part to open the "edit" page for this doc?