-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Allow Gatsby/Contentful to work offline #3977
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,35 @@ | ||
| const contentful = require(`contentful`) | ||
| const _ = require(`lodash`) | ||
| const fs = require(`fs-extra`) | ||
|
|
||
| const normalize = require(`./normalize`) | ||
|
|
||
| module.exports = async ({ spaceId, accessToken, host, syncToken }) => { | ||
| const cacheFilename = `contentful-result.json` | ||
|
|
||
| async function writeCacheFile(dir, result) { | ||
| try { | ||
| await fs.writeJson(`${dir}/${cacheFilename}`, result) | ||
| } catch (err) { | ||
| console.error(err) | ||
| } | ||
| } | ||
|
|
||
| module.exports = async ({ spaceId, accessToken, host, syncToken, cacheDir }) => { | ||
| // Fetch articles. | ||
| console.time(`Fetch Contentful data`) | ||
|
|
||
| // If the user knows they are offline, serve them cached result | ||
| if (process.env.GATSBY_CONTENTFUL_OFFLINE === `true`) { | ||
| console.log(`Using Contentful Offline cache ⚠️`) | ||
| try { | ||
| return await fs.readJson(`${cacheDir}/${cacheFilename}`) | ||
| } | ||
| catch (err) { | ||
| console.error(`Sorry, cached CMS data does not exist - unable to run offline`) | ||
| process.exit(1) | ||
| } | ||
| } | ||
|
|
||
| console.log(`Starting to fetch data from Contentful`) | ||
|
|
||
| const client = contentful.createClient({ | ||
|
|
@@ -29,9 +53,10 @@ module.exports = async ({ spaceId, accessToken, host, syncToken }) => { | |
| 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. | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the optimal user workflow is here. I've opted for forcing the user to acknowledge a loss of connectivity rather than trying to auto serve cache if network is down.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opt-in require user to know about this feature in first place and if they don't have connectivity they can't actually look for solution for this online. There are 2 ways to solve it:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, responded to your comment without reading your code change. I think this is better than other solution as this will cause less confusion that possibly stale data is used and requiring user action means user will acknowledge use of cached data.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool 👍 |
||
| console.log( | ||
| `Try running setting GATSBY_CONTENTFUL_OFFLINE=true to see if we can serve from cache.` | ||
| ) | ||
|
|
||
| process.exit(1) | ||
| } | ||
|
|
||
|
|
@@ -84,12 +109,15 @@ module.exports = async ({ spaceId, accessToken, host, syncToken }) => { | |
| return null | ||
| }) | ||
|
|
||
| return { | ||
| const result = { | ||
| currentSyncData, | ||
| contentTypeItems, | ||
| defaultLocale, | ||
| locales: space.locales, | ||
| } | ||
|
|
||
| writeCacheFile(cacheDir, result) | ||
| return result | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ exports.sourceNodes = async ( | |
| spaceId, | ||
| accessToken, | ||
| host, | ||
| cacheDir: `${store.getState().program.directory}/.cache`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As should this |
||
| }) | ||
|
|
||
| const entryList = normalize.buildEntryList({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use
path.join.