Giter Site home page Giter Site logo

uniform-optimize-gatsby-contentful-starter's Introduction

Optimize Contentful Gatsby.js Example Project

This is a Gatsby project bootstrapped with Gatsby cli.

Getting Started

Configure environment variables

  1. Copy .env.example to .env
  2. Set Contentful variable values to match your CMS see example config
  3. Configure your uniform data source

Install Gatsby CLI tool if you don't have

npm install -g @gatsby/cli

Install packages

npm i
# or
yarn

Run the development server

npm run develop
# or
yarn develop

Build for production and launch server

npm run build
# or
yarn build

Example gatsby-config.js

 const dotenv = require('dotenv');

 dotenv.config({
   path: `.env`
 });

module.exports = {
  siteMetadata: {
    title: 'uniform-optimize-gatsby-contentful-starter',    
    description: 'UniformConf, a Uniform Optimize demo site'
  },
  /* Your site config here */
  plugins: [
    'gatsby-plugin-postcss',
    'gatsby-plugin-react-helmet',
    {
      resolve: 'gatsby-source-contentful',
      options: {
        spaceId: process.env.CONTENTFUL_SPACE_ID,
        accessToken: process.env.NODE_ENV === 'production' ? process.env.CONTENTFUL_CDA_ACCESS_TOKEN : process.env.CONTENTFUL_CPA_ACCESS_TOKEN,
        host: process.env.NODE_ENV === 'production' ? process.env.CONTENTFUL_HOST : process.env.CONTENTFUL_HOST_PREVIEW,
        downloadLocal: process.env.NODE_ENV === 'production' ? false : true
      }
    }
  ],
}

Example gatsby-node.js

Before setting you the gatsby-node.js configuration, you must create a template inside src/templates/. In our example we create a ContentfulPage.tsx file in the templates directory which we map to in our gatsby-nodes.js file.

const path = require("path")

exports.onPostBuild = ({ reporter }) => {
  reporter.info("Your Gatsby site has been built!")
}

// creatre contentful pages dynamically
exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions
  const contentfulPageTemplate = path.resolve(
    "./src/templates/ContentfulPage.tsx"
  )

  const result = await graphql(`
    query {
      allContentfulPage {
        edges {
          node {
            id            
            slug
            title
            sys {
              contentType {
                sys {
                  id
                  linkType
                  type
                }
              }
              revision
              type
            }
          }
        }
      }
    }
  `)

  result.data.allContentfulPage.edges.forEach(edge => {
    console.log(edge.node.components)
    createPage({
      path: `${edge.node.slug}`,
      component: contentfulPageTemplate,
      context: {
        title: edge.node.title,
        slug: edge.node.slug,
        sys: {
          ...edge.node.sys,
        },
        components: edge.node.components,
      },
    })
  })
}

Learn More

To learn more about Gatsby.js, take a look at the following resources:

Known Issues

Gatsby will try and make requests to page-data and throw a console error which can impact lighthouse scores. See Github issue for more details

uniform-optimize-gatsby-contentful-starter's People

Contributors

kamsar avatar

Watchers

Mark Demeny avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.