Created
July 1, 2020 17:50
-
-
Save teasherm/b9bbe1bcf6f0b8224f9e64c926910445 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const ProgressBar = require(`progress`); | |
const { | |
createRemoteFileNode | |
} = require(`gatsby-source-filesystem`); | |
const bar = new ProgressBar(`Downloading Contentful Assets [:bar] :current/:total :elapsed secs :percent`, { | |
total: 0, | |
width: 30 | |
}); | |
let totalJobs = 0; | |
/** | |
* @name downloadContentfulAssets | |
* @description Downloads Contentful assets to the local filesystem. | |
* The asset files will be downloaded and cached. Use `localFile` to link to them | |
* @param gatsbyFunctions - Gatsby's internal helper functions | |
*/ | |
const downloadContentfulAssets = async gatsbyFunctions => { | |
const { | |
actions: { | |
createNode, | |
touchNode | |
}, | |
createNodeId, | |
store, | |
cache, | |
getNodes, | |
reporter | |
} = gatsbyFunctions; // Any ContentfulAsset nodes will be downloaded, cached and copied to public/static | |
// regardless of if you use `localFile` to link an asset or not. | |
const contentfulAssetNodes = getNodes().filter(n => n.internal.owner === `gatsby-source-contentful` && n.internal.type === `ContentfulAsset`); | |
totalJobs = contentfulAssetNodes.length; | |
bar.total = totalJobs; | |
for (const node of contentfulAssetNodes) { | |
let fileNodeID; | |
const { | |
contentful_id: id, | |
node_locale: locale | |
} = node; | |
const remoteDataCacheKey = `contentful-asset-${id}-${locale}`; | |
const cacheRemoteData = await cache.get(remoteDataCacheKey); | |
if (!node.file) { | |
reporter.warn(`The asset with id: ${id}, contains no file.`); | |
continue; | |
} | |
if (!node.file.url) { | |
reporter.warn(`The asset with id: ${id} has a file but the file contains no url.`); | |
continue; | |
} | |
const url = `http://${node.file.url.slice(2)}`; // Avoid downloading the asset again if it's been cached | |
// Note: Contentful Assets do not provide useful metadata | |
// to compare a modified asset to a cached version? | |
if (cacheRemoteData) { | |
fileNodeID = cacheRemoteData.fileNodeID; // eslint-disable-line prefer-destructuring | |
touchNode({ | |
nodeId: cacheRemoteData.fileNodeID | |
}); | |
} // If we don't have cached data, download the file | |
if (!fileNodeID) { | |
try { | |
const fileNode = await createRemoteFileNode({ | |
url, | |
store, | |
cache, | |
createNode, | |
createNodeId, | |
reporter | |
}); | |
if (fileNode) { | |
bar.tick(); | |
fileNodeID = fileNode.id; | |
await cache.set(remoteDataCacheKey, { | |
fileNodeID | |
}); | |
} | |
} catch (err) {// Ignore | |
console.log(err); | |
continue; | |
} | |
} | |
if (fileNodeID) { | |
node.localFile___NODE = fileNodeID; | |
} | |
continue; | |
} | |
}; | |
exports.downloadContentfulAssets = downloadContentfulAssets; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For contentful builds that hang, often happens on mac