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

[v2] Migration guide updates #5661

Merged
merged 27 commits into from Jun 5, 2018
Merged
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ddd19d6
Remove yarn examples for consistency with other docs
m-allanson Jun 1, 2018
8718398
Add rename responsive image queries section
m-allanson Jun 1, 2018
bf0a458
Add postcss plugins section
m-allanson Jun 1, 2018
231184a
Remove checklist (moved to issue 3986)
m-allanson Jun 1, 2018
00d68ee
Add mixed CommonJS/ES6 section
m-allanson Jun 1, 2018
8053835
Link to Gatsby webpack docs
m-allanson Jun 1, 2018
bb64597
Add node id section
m-allanson Jun 1, 2018
7afa9d9
Add section: remove explicit polyfills
m-allanson Jun 1, 2018
f616086
Add section: Change modifyBabelrc to onCreateBabelConfig
m-allanson Jun 1, 2018
163d7ea
Add section: Change `modifyWebpackConfig` to `onCreateWebpackConfig`
m-allanson Jun 1, 2018
e54d5f9
Add section: CSS is inlined automatically
m-allanson Jun 1, 2018
5413b73
Add introduction and small tweaks
m-allanson Jun 1, 2018
d10adad
Correct line numbers
m-allanson Jun 1, 2018
ca6d375
Improve explanation around movement of libs to peerDependencies
KyleAMathews Jun 1, 2018
433833a
chore: minor capitalization tweak
jlengstorf Jun 1, 2018
05b1e85
Add edit link
m-allanson Jun 4, 2018
d0cebc7
Split peer dep updates into two sections
m-allanson Jun 4, 2018
9124ad1
Declarative headings
m-allanson Jun 4, 2018
c5e1c15
Add table of contents
m-allanson Jun 4, 2018
2be2dca
Use neater Fragment syntax
m-allanson Jun 4, 2018
b8c4860
More detail on deprecation note
m-allanson Jun 4, 2018
405073e
Inline comments on code examples
m-allanson Jun 4, 2018
bfc9301
Add Jason's node ID query example
m-allanson Jun 4, 2018
6af965a
merge origin/v2-migration-guide
m-allanson Jun 4, 2018
d5beb1b
Use markdown diff syntax for before / after examples
m-allanson Jun 4, 2018
ad47463
Add 'in-progress' note about babel polyfills
m-allanson Jun 4, 2018
ed23990
Add instructions on how to install Gatsby v2
m-allanson Jun 4, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
253 changes: 219 additions & 34 deletions docs/docs/migrating-from-v1-to-v2.md
Expand Up @@ -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!
Copy link
Contributor

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?


```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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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"
Expand All @@ -51,7 +50,7 @@ export default ({ children }) => (
)
```

`layout in v2`
After:

```jsx{5}
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
  )

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

import React from "react"
Expand All @@ -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`
Expand All @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpicky and mostly unrelated, but you can also skip the Fragment import and just use <>:

<>
  <h1>Fragment shorthand!</h1>
  <p>Yay!</p>
</>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nitpicky is good :)

Expand All @@ -116,7 +119,7 @@ export const query = graphql`
`
```

`StaticQuery with layout in v2`
After:

```jsx
import React, { Fragment } from "react"
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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`.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not — we pass both pathContext and pageContext as props


<!--
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).
Copy link
Contributor

Choose a reason for hiding this comment

The 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 boundActionCreators note)?


## 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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');
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps eg "Rewrite node ID queries"?

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
          }
        }
      }
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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).
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

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

Gatsby isn't yet compiling code in node_modules so code there won't be polyfilled yet. See bullet number 5 #3870


## 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

😱 a switch statement?! 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps "Remove inlined css in html.js"?

Copy link
Contributor

Choose a reason for hiding this comment

The 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).