-
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
Add guide for programmatically creating pages #9069
Merged
amberleyromo
merged 4 commits into
gatsbyjs:master
from
jbranchaud:add-guide-for-programmatically-creating-pages
Nov 9, 2018
Merged
Changes from 3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,115 @@ | |
title: Programmatically create pages from data | ||
--- | ||
|
||
This is a stub. Help our community expand it. | ||
Gatsby and its ecosystem of plugins provide all kinds of data through a | ||
GraphQL interface. This guide will show how that data can be used to | ||
programmatically create pages. | ||
|
||
Please use the [Gatsby Style Guide](/docs/gatsby-style-guide/) to ensure your | ||
pull request gets accepted. | ||
### Prerequisites | ||
|
||
Though you can use any data source you'd like, this guide will show how to | ||
create pages from markdown files (following after the example introduced in | ||
[earlier guides](https://www.gatsbyjs.org/docs/adding-markdown-pages/)). | ||
|
||
### Creating Pages | ||
|
||
The Gatsby Node API provides the | ||
[`createPages`](https://www.gatsbyjs.org/docs/node-apis/#createPages) | ||
extension point which we'll use to add pages. This function will give us | ||
access to the | ||
[`createPage`](https://www.gatsbyjs.org/docs/actions/#createPage) action | ||
which is at the core of programmatically creating a page. | ||
|
||
```javascript{17-25}:title=gatsby-node.js | ||
exports.createPages = ({ graphql, actions }) => { | ||
const { createPage } = actions; | ||
return new Promise((resolve, reject) => { | ||
graphql(` | ||
{ | ||
allMarkdownRemark { | ||
edges { | ||
node { | ||
fields { | ||
slug | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`).then(result => { | ||
result.data.allMarkdownRemark.edges.forEach(({ node }) => { | ||
createPage({ | ||
path: node.fields.slug, | ||
component: path.resolve(`./src/templates/blog-post.js`), | ||
context: { | ||
slug: node.fields.slug, | ||
}, | ||
}); | ||
}); | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
``` | ||
|
||
For each page we want to create we must specify the `path` for visiting that | ||
page, the `component` template used to render that page, and any `context` | ||
we need in the component for rendering. The `context` parameter is | ||
_optional_ though often times it will include a unique identifier that can | ||
be used to query for associated data that will be rendered to the page. | ||
|
||
### Specifying A Template | ||
|
||
The `createPage` action required that we specify the `component` template | ||
that will be used to render the page. Here is an example of what the | ||
referenced template could look like: | ||
|
||
```javascript:title=gatsby-node.js | ||
import React from 'react'; | ||
import { graphql } from 'gatsby'; | ||
import Layout from '../components/layout'; | ||
|
||
export default ({ data }) => { | ||
const post = data.markdownRemark; | ||
return ( | ||
<Layout> | ||
<div> | ||
<h1> | ||
{post.frontmatter.title} | ||
</h1> | ||
<div dangerouslySetInnerHTML={{ __html: post.html }} /> | ||
</div> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export const query = graphql` | ||
query($slug: String!) { | ||
markdownRemark(fields: { slug: { eq: $slug } }) { | ||
html | ||
frontmatter { | ||
title | ||
} | ||
} | ||
} | ||
`; | ||
``` | ||
|
||
Notice that the `slug` value we specified in the `createPage` context 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. We can also use context directly in template component - it's accessible via |
||
used in the template's GraphQL query. As a result we can provide the `title` | ||
and `html` from the matching `markdownRemark` record to our component. The | ||
context is also available as the `pageContext` prop in the template | ||
component itself. | ||
|
||
### Not Just Markdown | ||
|
||
The | ||
[`gatsby-transformer-remark`](https://www.gatsbyjs.org/packages/gatsby-transformer-remark/) | ||
plugin is just one of a multitude of Gatsby plugins that can provide data | ||
through the GraphQL interface. Any of that data can be used to | ||
programmatically create pages. | ||
|
||
### Other Resources | ||
|
||
- [Example Repository](https://github.com/jbranchaud/gatsby-programmatic-pages) | ||
- [Using Unstructured Data](https://www.gatsbyjs.org/docs/using-unstructured-data/) |
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.
maybe mention that
context
is optional? By default gatsby will pass some context already - stuff likepath