diff --git a/packages/gatsby-source-contentful/README.md b/packages/gatsby-source-contentful/README.md index 04829b8f51dd0..ce9f8146b429e 100644 --- a/packages/gatsby-source-contentful/README.md +++ b/packages/gatsby-source-contentful/README.md @@ -44,6 +44,11 @@ plugins: [ ] ``` +### Offline + +If you don't have internet connection you can add `export GATSBY_CONTENTFUL_OFFLINE=true` to tell the plugin to fallback to the cached data, if there is any. + + ### Configuration options **`spaceId`** [string][required] diff --git a/packages/gatsby-source-contentful/package.json b/packages/gatsby-source-contentful/package.json index bda0ad5a57d73..c2ae08f43189f 100644 --- a/packages/gatsby-source-contentful/package.json +++ b/packages/gatsby-source-contentful/package.json @@ -14,6 +14,7 @@ "contentful": "^6.1.0", "deep-map": "^1.5.0", "fs-extra": "^4.0.2", + "is-online": "^7.0.0", "gatsby-plugin-sharp": "^2.0.0-beta.2", "json-stringify-safe": "^5.0.1", "lodash": "^4.17.4", diff --git a/packages/gatsby-source-contentful/src/fetch.js b/packages/gatsby-source-contentful/src/fetch.js index d4e7fb14c5c94..c8734731b6e05 100644 --- a/packages/gatsby-source-contentful/src/fetch.js +++ b/packages/gatsby-source-contentful/src/fetch.js @@ -1,6 +1,5 @@ const contentful = require(`contentful`) const _ = require(`lodash`) - const normalize = require(`./normalize`) module.exports = async ({ @@ -10,8 +9,9 @@ module.exports = async ({ syncToken, environment, }) => { - // Fetch articles. + // Fetch entries. console.time(`Fetch Contentful data`) + console.log(`Starting to fetch data from Contentful`) const client = contentful.createClient({ @@ -36,9 +36,10 @@ module.exports = async ({ console.log( `Accessing your Contentful space failed. Perhaps you're offline or the spaceId/accessToken is incorrect.` ) - // TODO perhaps continue if there's cached data? That would let - // someone develop a contentful site even if not connected to the internet. - // For prod builds though always fail if we can't get the latest data. + console.log( + `Try running setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache.` + ) + process.exit(1) } @@ -91,12 +92,14 @@ module.exports = async ({ return null }) - return { + const result = { currentSyncData, contentTypeItems, defaultLocale, locales, } + + return result } /** diff --git a/packages/gatsby-source-contentful/src/gatsby-node.js b/packages/gatsby-source-contentful/src/gatsby-node.js index e2b06404f651a..a226d31f7a56d 100644 --- a/packages/gatsby-source-contentful/src/gatsby-node.js +++ b/packages/gatsby-source-contentful/src/gatsby-node.js @@ -1,5 +1,5 @@ const path = require(`path`) - +const isOnline = require(`is-online`) const _ = require(`lodash`) const fs = require(`fs-extra`) @@ -37,6 +37,27 @@ exports.sourceNodes = async ( ) => { const { createNode, deleteNode, touchNode, setPluginStatus } = actions + const online = await isOnline() + + // If the user knows they are offline, serve them cached result + // For prod builds though always fail if we can't get the latest data + if ( + !online && + process.env.GATSBY_CONTENTFUL_OFFLINE === `true` && + process.env.NODE_ENV !== `production` + ) { + getNodes() + .filter(n => n.internal.owner === `gatsby-source-contentful`) + .forEach(n => touchNode({ nodeId: n.id })) + + console.log(`Using Contentful Offline cache ⚠️`) + console.log( + `Cache may be invalidated if you edit package.json, gatsby-node.js or gatsby-config.js files` + ) + + return + } + host = host || `cdn.contentful.com` environment = environment || `master` // default is always master // Get sync token if it exists. @@ -62,8 +83,8 @@ exports.sourceNodes = async ( syncToken, spaceId, accessToken, - environment, host, + environment, }) const entryList = normalize.buildEntryList({