Skip to content

Commit

Permalink
Documentation fix: Execute trailing slash stripping function, rather …
Browse files Browse the repository at this point in the history
…than assign it. (#9924)

The trailing slash stripping plugin [executes the function](https://github.com/gatsbyjs/gatsby/blob/f9714a5646523bc728586e3692190507ad68e6c1/packages/gatsby-plugin-remove-trailing-slashes/src/gatsby-node.js#L9) rather than just assigning it, so I'd imagine that's what the example code should be doing too.
  • Loading branch information
stevetweeddale authored and DSchau committed Nov 29, 2018
1 parent 043b43b commit 994ac97
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/docs/creating-and-modifying-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ _Note: There's also a plugin that will remove all trailing slashes from pages au
[gatsby-plugin-remove-trailing-slashes](/packages/gatsby-plugin-remove-trailing-slashes/)_.

```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 = _path => (_path === `/` ? _path : _path.replace(/\/$/, ``))
page.path = replacePath(page.path)
if (page.path !== oldPage.path) {
// Replace new page with old page
deletePage(oldPage)
Expand Down

0 comments on commit 994ac97

Please sign in to comment.