Skip to content

Commit

Permalink
[gatsby-plugin-remove-trailing-slashes] - update docs (gatsbyjs#3226)
Browse files Browse the repository at this point in the history
* Formatting

* Adds note

* Updates source

* Remove unnecessary destructured arg

* Enabling worspace-experimental

* Formatting md

* Update creating-and-modifying-pages.md

* Update .yarnrc
  • Loading branch information
scottyeck authored and KyleAMathews committed Dec 15, 2017
1 parent 8bd5b16 commit ca2cc8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions docs/docs/creating-and-modifying-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,24 @@ trailing slashes.

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/)
plugin._

```javascript
// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onCreatePage = ({ page, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators;

return new Promise((resolve, reject) => {
// Remove trailing slash
const newPage = Object.assign({}, page, {
path: page.path === `/` ? page.path : page.path.replace(/\/$/, ``),
});

if (newPage.path !== page.path) {
// Remove the old page
deletePage(page);
// Add the new page
createPage(newPage);
return new Promise(resolve => {
const oldPage = Object.assign({}, page);
// Remove trailing slash unless page is /
page.path = _path => (_path === `/` ? _path : _path.replace(/\/$/, ``));
if (page.path !== oldPage.path) {
// Replace new page with old page
deletePage(oldPage);
createPage(page);
}

resolve();
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Replacing '/' would result in empty string which is invalid
const replacePath = _path => (_path === `/` ? _path : _path.replace(/\/$/, ``))

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

return new Promise(resolve => {
Expand Down

0 comments on commit ca2cc8e

Please sign in to comment.