Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gatsby-plugin-contentful-pages #3725

Closed
wants to merge 13 commits into from
Closed

Add gatsby-plugin-contentful-pages #3725

wants to merge 13 commits into from

Conversation

felixjung
Copy link
Contributor

This PR adds a new plugin for creating pages straight from Contentful data. No need for gatsby-node.js scripts.

Changes

  • Add plugin
  • Update the "using-contentful" example replacing the gatsby-node.js script with a plugin config in the gatsby.config.js file.

@gatsbybot
Copy link
Collaborator

Deploy preview for gatsbygram ready!

Built with commit 53aa95f

https://deploy-preview-3725--gatsbygram.netlify.com

@gatsbybot
Copy link
Collaborator

Deploy preview for gatsbygram ready!

Built with commit c10e265

https://deploy-preview-3725--gatsbygram.netlify.com

@Khaledgarbaya
Copy link
Contributor

Hey @felixjung, is there a reason not to have this in the contentful-source-plugin ?
you can extend the opetion object to include these configs.

{
  resolve: 'contentful-source-contentful',
  ...
  pages:[
   {template: path.resolve('templates/contentType1.js', contentType: 'ContentType1')}
   ....
  ]
}

and if the pages config is not set the plugin won't create any. WDYT?

@felixjung
Copy link
Contributor Author

Sure. Just thought, since this is technically not a source functionality, it should be separate? But maybe I did not get the difference between different plugin types.

@KyleAMathews
Copy link
Contributor

This should definitely be seperate. Source plugins should only have responsibility for sourcing data. There are many ways to facilitate making pages from data and there's no need to draw source or transformer plugins into that.

@felixjung looks really good. One question I had reading through this is if it'd make sense to make this plugin a bit more generic and work for any type of data? E.g. gatsby-plugin-type-pages? There's another plugin I know kinda similar to this by @mikefowler https://www.npmjs.com/package/gatsby-plugin-collections for easily creating pages from "markdown collections".

One big argument against making this plugin more generic is then the UX would suffer as this is very simple to use if you're familiar with Contentful as you just copy the content type name over to your gatsby-config.js and off you go where if this was generic, you'd need to understand how gatsby-source-contentful converts Contentful type names into Gatsby's type names.

@felixjung
Copy link
Contributor Author

Thanks for the feedback. You're right that there are a lot of other use cases for this. And generalizing might not make it that much more difficult. I could imagine a config like this:

    {
      resolve: `gatsby-plugin-auto-pages`,
      options: {
        pageTypes: [
          {
            component: ({ prop1, prop2, ... }) => `.src/templates/${props1}${prop2}`,
            path: ({ prop1, prop2, ... }) => `/products/${prop1}/`,
            query: `
              allNodeTypeSelector {
                edges {
                  node {
                    prop1,
                    prop2,
                    ...
                  }
                }
              }
            `,
          },
        ],
      },

It's not that much harder and I guess you can make examples for popular source plugins?

@KyleAMathews
Copy link
Contributor

@felixjung yeah... so thinking some more — I'm not really sure I want to commit to higher-order patterns for creating pages just yet — I'd rather people try building more solutions like this as plugins first. I feel like there's too much experimentation to do still before making anything official.

So instead of adding it as an official plugin — could you publish it a personal repo and to NPM? Then add a mention of it to the contentful source plugin README?

I think this is an awesome solution for many people. Would love to hear more reports from people using this on projects in the wild!

@felixjung
Copy link
Contributor Author

OK, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants