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

[source-contentful] offline mode #5869

Merged
merged 8 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/gatsby-source-contentful/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-contentful/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 9 additions & 6 deletions packages/gatsby-source-contentful/src/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const contentful = require(`contentful`)
const _ = require(`lodash`)

const normalize = require(`./normalize`)

module.exports = async ({
Expand All @@ -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({
Expand All @@ -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)
}

Expand Down Expand Up @@ -91,12 +92,14 @@ module.exports = async ({
return null
})

return {
const result = {
currentSyncData,
contentTypeItems,
defaultLocale,
locales,
}

return result
}

/**
Expand Down
25 changes: 23 additions & 2 deletions packages/gatsby-source-contentful/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require(`path`)

const isOnline = require(`is-online`)
const _ = require(`lodash`)
const fs = require(`fs-extra`)

Expand Down Expand Up @@ -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.
Expand All @@ -62,8 +83,8 @@ exports.sourceNodes = async (
syncToken,
spaceId,
accessToken,
environment,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this removal intentional? I think fetchData does need environment param to work correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈

host,
environment,
})

const entryList = normalize.buildEntryList({
Expand Down