Skip to content

Instantly share code, notes, and snippets.

@PaulieScanlon
Created May 17, 2021 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulieScanlon/0fae5556f77c3f5bcb7b12942326c02b to your computer and use it in GitHub Desktop.
Save PaulieScanlon/0fae5556f77c3f5bcb7b12942326c02b to your computer and use it in GitHub Desktop.
Contentful Render - Component
import React from 'react';
const ContentfulRender = ({ children }) => {
return <>{process.env.GATSBY_CONTENTFUL_KEY ? <>{children}</> : null}</>;
};
export default ContentfulRender;
@PaulieScanlon
Copy link
Author

PaulieScanlon commented May 17, 2021

Usage

import React from 'react';

import ContentfulRender from '../components/contentful-render';

const IndexPage = () => {
  return (
    <main>
      <h1>Index Page</h1>
      <ContentfulRender>
        <SomeContentfulComponent />
      </ContentfulRender>
    </main>
  );
};

export default IndexPage;

This was what i was thinking but i think the problems might come if <SomeContentfulComponent /> has a GraphQL query inside of it that relies on Contentful data. Reason being is that if you're using a Gatsby plugin to source the data from Contentful then the API key needs to be there or you'll get an error when Gatsby builds

The other option i was thinking about would be to create multiple Contentful spaces which have their own keys. Folks who shouldn't see certain content would get a key to a dummy or blank workspace. This way the site sill works as expected it just returns no data. You might need to add a few data ? <div /> : null in places where the UI is expecting an array and it might be null when the blank key is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment