File tree Expand file tree Collapse file tree 1 file changed +37
-6
lines changed
packages/gatsby-source-contentful/src Expand file tree Collapse file tree 1 file changed +37
-6
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,27 @@ const restrictedNodeFields = [
1616 `parent` ,
1717]
1818
19+ const cmsCacheFilename = `contentful-result.json`
20+
21+ async function writeFetchCache ( programDirectory , fetchResult ) {
22+ try {
23+ await fs . writeJson ( `${ programDirectory } /.cache/${ cmsCacheFilename } ` , fetchResult )
24+ } catch ( err ) {
25+ console . error ( err )
26+ }
27+ }
28+
29+ async function readFetchCache ( programDirectory ) {
30+ try {
31+ console . time ( `Fetch Contentful data` )
32+ console . log ( `Using Contentful Offline cache ⚠️` )
33+ return await fs . readJson ( `${ programDirectory } /.cache/${ cmsCacheFilename } ` )
34+ } catch ( err ) {
35+ console . error ( err )
36+ }
37+ return null
38+ }
39+
1940exports . setFieldsOnGraphQLNodeType = require ( `./extend-node-type` ) . extendNodeType
2041
2142/***
@@ -53,17 +74,27 @@ exports.sourceNodes = async (
5374 ]
5475 }
5576
77+ const programDir = store . getState ( ) . program . directory
78+ let fetchResult
79+
80+ if ( process . env . GATSBY_CONTENTFUL_OFFLINE === `true` ) {
81+ fetchResult = await readFetchCache ( programDir )
82+ } else {
83+ fetchResult = await fetchData ( {
84+ syncToken,
85+ spaceId,
86+ accessToken,
87+ host,
88+ } )
89+ writeFetchCache ( programDir , fetchResult )
90+ }
91+
5692 const {
5793 currentSyncData,
5894 contentTypeItems,
5995 defaultLocale,
6096 locales,
61- } = await fetchData ( {
62- syncToken,
63- spaceId,
64- accessToken,
65- host,
66- } )
97+ } = fetchResult
6798
6899 const entryList = normalize . buildEntryList ( {
69100 currentSyncData,
You can’t perform that action at this time.
0 commit comments