Gatsby: Add ignore option to gatsby-plugin-page-creator

Created on 20 Jan 2019  路  10Comments  路  Source: gatsbyjs/gatsby

Summary

I would like to give my client the option of choosing, via a config file, whether or not to include blog pages on their generated site.

Example project:

src/
    content/
        blog/
            post.md              <--- this becomes http://mysite.com/blog/post
        other/
            other.md
    pages/
        blog.js                    <--- this is http://mysite.com/blog
        other.js

The first part, dynamically selecting which content files to turn into pages, was easy.

The second part, dynamically selecting which pages/*.js files to process, eludes me, and is especially annoying when building results in a GraphQL error due to the blog content not existing for the blog index page.

Relevant information

I looked into gatsby-plugin-page-creator since it is responsible for creating /pages/*.js pages, but unfortunately it doesn't seem to support the ignore option that gatsby-source-filesystem uses.

Request

Could an ignore option be added to gatsby-plugin-page-creator? Or is there another way to programmatically decide which /pages files to create?

good first issue

Most helpful comment

How do I remove default gatsby-plugin-page-creator plugin? If I add my own gatsby-plugin-page-creator with ignore glob then gatsby runs both plugins - default without ignore and my version with ignore..

All 10 comments

Hey @Warped2713

This should be easy to add to https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-page-creator/src/gatsby-node.js

We could add a key in options called ignore and pass that to glob options in

https://github.com/gatsbyjs/gatsby/blob/d71b3107ba05c92b6cbb70f05092d197c372e5d1/packages/gatsby-plugin-page-creator/src/gatsby-node.js#L55

Would you like to try this? We'd love to accept a PR!

Okay, I will attempt a PR.

But looking into it, I don't think it's enough to just pass options: { ignorePatterns } into glob.

The files array is being updated by chokidar after glob has initialized the array, meaning there would be files added without checking against ignorePatterns

https://github.com/gatsbyjs/gatsby/blob/d71b3107ba05c92b6cbb70f05092d197c372e5d1/packages/gatsby-plugin-page-creator/src/gatsby-node.js#L61-L64

So instead I will add a src/ignore-path.js that gets called by _createPage and pass the ignorePatterns into _createPage which gets called on every file in the initial files array AND on all files added by chokidar.

I guess this means I will also need to add some tests for src/ignore-path.js since this is no longer glob's responsibility.

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

Thanks for being a part of the Gatsby community! 馃挭馃挏

This issue will be resolved when PR #11304 is accepted

How do I remove default gatsby-plugin-page-creator plugin? If I add my own gatsby-plugin-page-creator with ignore glob then gatsby runs both plugins - default without ignore and my version with ignore..

@lexaurin You can use gatsby-dev to run gatsby with a custom plugin as if it were a default plugin:

#install gatsby dev cli
npm install -g gatsby-dev-cli

# tell gatsby dev where o find your custom gatsby repo
gatsby-dev --set-path-to-repo /path/to/my/custom/version/gatsby

# run gatsby dev with specific packages from your custom repo
gatsby-dev --packages gatsby gatsby-plugin-page-creator

# (in a separate shell instance) run gatsby as usual
gatsby develop

That said, if you update your gatsby version, the default should already have ignore options: https://www.gatsbyjs.org/packages/gatsby-plugin-page-creator/#ignoring-specific-files

@lexaurin why not move away from src/pages? https://github.com/gatsbyjs/gatsby/issues/12937#issuecomment-478025499

How do I remove default gatsby-plugin-page-creator plugin? If I add my own gatsby-plugin-page-creator with ignore glob then gatsby runs both plugins - default without ignore and my version with ignore..

I'm in this boat too. I guess there's no way to pass options to the default plugin either... https://github.com/gatsbyjs/gatsby/issues/12937 (just reopened this)

you could remove all pages from src/pages with deletePage if you like.

I believe something along the lines of this should work

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

  if (!page.component.includes('src/pages')) {
    return;
  }

  deletePage(page);
};

Yep, I was using a variant of this approach and it was working great in dev (see https://github.com/gatsbyjs/gatsby/issues/12937#issuecomment-523030887).

However, this fails in production (gatsby build) when one of my JavaScript example files that I am calling deletePage on does not export a React component (error message also at link above).

I would be happy with a minimal fix of this approach just working in production the same way as in dev (https://github.com/gatsbyjs/gatsby/issues/12937#issuecomment-523071571).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3CordGuy picture 3CordGuy  路  3Comments

totsteps picture totsteps  路  3Comments

benstr picture benstr  路  3Comments

rossPatton picture rossPatton  路  3Comments

dustinhorton picture dustinhorton  路  3Comments