Skip to content

Commit

Permalink
docs(creating-and-modifying-pages): remove unnecessary promise (#11697)
Browse files Browse the repository at this point in the history
* Remove unnecessary promise wrapper in example

I also added a "note" indicating that you can still use this function asynchronous.

* Lint
  • Loading branch information
jkjustjoshing authored and m-allanson committed Feb 12, 2019
1 parent ccd9dcd commit df2506c
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions docs/docs/creating-and-modifying-pages.md
Expand Up @@ -113,24 +113,24 @@ To do this, in your site's `gatsby-node.js` add code similar to the following:
_Note: There's also a plugin that will remove all trailing slashes from pages automatically:
[gatsby-plugin-remove-trailing-slashes](/packages/gatsby-plugin-remove-trailing-slashes/)_.

_Note: If you need to perform an asynchronous action within `onCreatePage` you can return a promise or use an `async` function._

```javascript:title=gatsby-node.js
// Replacing '/' would result in empty string which is invalid
const replacePath = path => (path === `/` ? path : path.replace(/\/$/, ``))
// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions
return new Promise(resolve => {
const oldPage = Object.assign({}, page)
// Remove trailing slash unless page is /
page.path = replacePath(page.path)
if (page.path !== oldPage.path) {
// Replace new page with old page
deletePage(oldPage)
createPage(page)
}
resolve()
})

const oldPage = Object.assign({}, page)
// Remove trailing slash unless page is /
page.path = replacePath(page.path)
if (page.path !== oldPage.path) {
// Replace new page with old page
deletePage(oldPage)
createPage(page)
}
}
```

Expand Down

0 comments on commit df2506c

Please sign in to comment.