From 38c369e3b18289f262187076013745a5286f697b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Tue, 24 Jul 2018 17:12:23 +0200 Subject: [PATCH] fix config and setup script --- .contentful.json.sample | 13 +++---------- README.md | 6 +++--- gatsby-config.js | 31 +++++++++++++++---------------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/.contentful.json.sample b/.contentful.json.sample index 0f95c66..6a2368f 100644 --- a/.contentful.json.sample +++ b/.contentful.json.sample @@ -1,11 +1,4 @@ { - "development": { - "host": "preview.contentful.com", - "spaceId": "...", - "accessToken": "..." - }, - "production": { - "spaceId": "...", - "accessToken": "..." - } -} \ No newline at end of file + "spaceId": "...", + "accessToken": "..." +} diff --git a/README.md b/README.md index 229fce0..59edcba 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ With Contentful and Gatsby you can connect your favorite static site generator w * Simple content model and structure. Easy to adjust to your needs. * Contentful integration using our [Sync API](https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/synchronization/initial-synchronization-of-entries-of-a-specific-content-type) -* Using our [Preview API](https://www.contentful.com/developers/docs/references/content-preview-api/) for development and our [Delivery API](https://www.contentful.com/developers/docs/references/content-delivery-api/) for production. +* Using our [Delivery API](https://www.contentful.com/developers/docs/references/content-delivery-api/). * Responsive/adaptive images via [gatsby-image](https://www.gatsbyjs.org/packages/gatsby-image/) ## Contribution @@ -44,7 +44,7 @@ This project comes with a Contentful setup command `npm run setup`. ![Command line dialog of the npm run setup command](https://rawgit.com/contentful-userland/gatsby-contentful-starter/master/setup.jpg "Command line dialog of the npm run setup command") -This command will ask you for a space ID, and access tokens for the Contentful Management, Preview and Delivery API and then import the needed content model into the space you define and write a config file (`./contentful.json`). +This command will ask you for a space ID, and access tokens for the Contentful Management and Delivery API and then import the needed content model into the space you define and write a config file (`./contentful.json`). `npm run setup` automates that for you but if you want to do it yourself rename `.contentful.json.sample` to `.contentful.json` and add your configuration in this file. @@ -54,7 +54,7 @@ This project comes with a few handy commands for linting and code fixing. The mo ### `npm run dev` -Run in the project locally using the [Contentful Preview API](https://www.contentful.com/developers/docs/references/content-preview-api/). This can perfect to preview changes before they go into production. +Run in the project locally. ### `npm run build` diff --git a/gatsby-config.js b/gatsby-config.js index 6196159..7f6f054 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -1,23 +1,22 @@ let contentfulConfig try { - const configFile = require('./.contentful') - contentfulConfig = process.env.NODE_ENV === 'production' - ? configFile.production - : configFile.development -} catch (_) { - contentfulConfig = { - spaceId: process.env.CONTENTFUL_SPACE_ID, - accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN, - } -} finally { - const { spaceId, accessToken } = contentfulConfig + // Load the Contentful config from the .contentful.json + contentfulConfig = require('./.contentful') +} catch (_) {} - if (!spaceId || !accessToken) { - throw new Error( - 'Contentful spaceId and the delivery token need to be provided.' - ) - } +// Overwrite the Contentful config with environment variables if they exist +contentfulConfig = { + spaceId: process.env.CONTENTFUL_SPACE_ID || contentfulConfig.spaceId, + accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN || contentfulConfig.accessToken, +} + +const { spaceId, accessToken } = contentfulConfig + +if (!spaceId || !accessToken) { + throw new Error( + 'Contentful spaceId and the delivery token need to be provided.' + ) } module.exports = {