Skip to content

Commit

Permalink
chore(blog): fix code in sanity blog post (#12201)
Browse files Browse the repository at this point in the history
## Description
`createPages` doesn't trigger onCreatePage in the same plugin so I added
an extra bit of example code to demonstrate that.

## Update to
#11684

## Related Issues
Related to #5255
  • Loading branch information
williamtstanley authored and DSchau committed Mar 7, 2019
1 parent 5c449b2 commit 9b11381
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions docs/blog/2019-03-01-localization-with-gatsby-and-sanity/index.md
Expand Up @@ -222,18 +222,15 @@ export const query = graphql`

## Generating Pages with the Correct Context

In the `gatsby-node.js` file you need to add a function that generates pages for each supported language with a path prefix for each, such as 'es' for Spanish, and 'fr' for French.
In the `gatsby-node.js` file you need to add a function that generates pages for each supported language with a path prefix for each, such as 'es' for Spanish, and 'fr' for French. It needs to be called in `onCreatePage` so that every automatically generated page from your `pages` directory gets localized. It also needs to be added into the `createPages` to localize all of your dynamic content. This is needed as gatsby doesn't call `onCreatePage` for pages generated by the same plugin at the moment to avoid infinite loops.

```js:title=gatsby-node.js
// Get your list of languages from somewhere, env file, config.json, etc
// for sake of this snippet I am putting it here

const extraLanguages = ["es"] // English is currently the default so it isn't needed here.

exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions

deletePage(page)
const createLocalePage = (page, createPage) => {
const { context, ...rest } = page

createPage({
Expand Down Expand Up @@ -261,9 +258,32 @@ exports.onCreatePage = ({ page, actions }) => {
})
}
}

exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions

// generate your dynamic content here...
const page = {
path: "some-page",
component: path.resolve(`./src/templates/some-page.js`),
context: {
slug: "some-page-slug",
},
}

createLocalePage(page, createPage)
}

exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions

deletePage(page)

createLocalePage(page, createPage)
}
```

## Sanity.io + Gatsby.js + Easy Localization = Win
## Sanity.io + Gatsby.js + Localization = Win

If you haven't already started using [Sanity.io](https://www.sanity.io) as your content backend I encourage you to check it out. You and your content team will love it.

Expand Down
2 changes: 1 addition & 1 deletion docs/blog/author.yaml
Expand Up @@ -235,7 +235,7 @@
avatar: avatars/daniel-lemay.jpg
twitter: "@dslemay"
- id: Travis Stanley
bio: Techlead for the Webteam at @ Fitplan. Adoring partner, father of three, Codemonkey, gamer, and writer. Thats about the gist of it.
bio: Techlead for the Webteam @ Fitplan. Adoring partner, father of three, Codemonkey, gamer, and writer. Thats about the gist of it.
avatar: avatars/travis_stanley.jpg
twitter: "@cpt_silverfox"
- id: Katie Fujihara
Expand Down

0 comments on commit 9b11381

Please sign in to comment.