-
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][doc] v1 to v2 migration guide (WIP) #5575
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
--- | ||
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. | ||
|
||
```bash | ||
npm i react react-dom | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
yarn add react react-dom | ||
``` | ||
|
||
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. | ||
|
||
```bash | ||
npm i typography react-typography | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
yarn add typography react-typography | ||
``` | ||
|
||
Search for the plugins that you use in [Gatsby’s plugins page](/plugins) and check their installation instructions. | ||
|
||
## 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). | ||
|
||
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` | ||
|
||
```jsx | ||
import React from "react" | ||
|
||
export default ({ children }) => ( | ||
<div> | ||
{children()} | ||
</div> | ||
) | ||
``` | ||
|
||
`layout in v2` | ||
|
||
```jsx{5} | ||
import React from "react" | ||
|
||
export default ({ children }) => ( | ||
<div> | ||
{children} | ||
</div> | ||
) | ||
``` | ||
|
||
### 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` | ||
|
||
```jsx | ||
import React from "react" | ||
import Layout from "../components/layout" | ||
|
||
export default () => ( | ||
<Layout> | ||
<div>Hello World</div> | ||
</Layout> | ||
) | ||
``` | ||
|
||
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` | ||
|
||
```jsx | ||
import React, { Fragment } from "react" | ||
import Helmet from "react-helmet" | ||
|
||
export default ({ children, data }) => ( | ||
<Fragment> | ||
<Helmet titleTemplate={`%s | ${data.site.siteMetadata.title}`} defaultTitle={data.site.siteMetadata.title} /> | ||
<div> | ||
{children()} | ||
</div> | ||
</Fragment> | ||
) | ||
|
||
export const query = graphql` | ||
query LayoutQuery { | ||
site { | ||
siteMetadata { | ||
title | ||
} | ||
} | ||
} | ||
` | ||
``` | ||
|
||
`StaticQuery with layout in v2` | ||
|
||
```jsx | ||
import React, { Fragment } from "react" | ||
import { StaticQuery } from "gatsby" | ||
|
||
export default ({ children }) => ( | ||
<StaticQuery | ||
query={graphql` | ||
query LayoutQuery { | ||
site { | ||
siteMetadata { | ||
title | ||
} | ||
} | ||
} | ||
`} | ||
render={data => ( | ||
<Fragment> | ||
<Helmet titleTemplate={`%s | ${data.site.siteMetadata.title}`} defaultTitle={data.site.siteMetadata.title} /> | ||
<div> | ||
{children} | ||
</div> | ||
</Fragment> | ||
)} | ||
/> | ||
) | ||
``` | ||
|
||
### 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` | ||
|
||
```jsx | ||
import React from "react" | ||
|
||
export default ({ children, location }) => ( | ||
<div> | ||
<p>Path is {location.pathname}</p> | ||
{children} | ||
</div> | ||
) | ||
``` | ||
|
||
`src/pages/index.js` | ||
|
||
```jsx | ||
import React from "react" | ||
import Layout from "../components/layout.js" | ||
|
||
export default props => ( | ||
<Layout location={props.location}> | ||
<div>Hello World</div> | ||
</Layout> | ||
) | ||
``` | ||
|
||
## Rename `boundActionCreators` to `actions` | ||
`boundActionCreators` is deprecated in v2. You can continue using it, but it’s recommended that you rename it to `actions`. | ||
|
||
## Rename `pathContext` to `pageContext` | ||
Similar to `boundActionCreators` above, `pathContext` is deprecated in favor of `pageContext`. | ||
|
||
<!-- | ||
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. | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There should be a better migration strategy here i think, for sites with lots of pages, this edit will take forever since they have to go into each site and manually wrap them with a layout.
I would suggest demostating a migration path that uses
replaceComponentRenderer
api to automatically wrap every page in the right layout. That or wrap the logic up into a gatsby plugingatsby-plugin-layout
or something?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.
Oh neat, yeah. Have you done this? If you've got an example of using
replaceComponentRenderer
like this, I can write it up into this guide.This sounds like a good idea too.
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.
We also talked about adding a codemod #5038