Skip to content

Commit

Permalink
[gatsby-source-filesystem] don't try to process same remote file mult…
Browse files Browse the repository at this point in the history
…iple times, cache promises and return them on subsequent calls
  • Loading branch information
pieh committed Feb 4, 2018
1 parent 928914e commit 0152be0
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ const { isWebUri } = require(`valid-url`)
const { createFileNode } = require(`./create-file-node`)
const cacheId = url => `create-remote-file-node-${url}`

module.exports = ({ url, store, cache, createNode, auth = {} }) =>
new Promise(async (resolve, reject) => {
/**
* Index of promises resolving to File node from remote url
*/
const processingCache = {}

module.exports = ({ url, store, cache, createNode, auth = {} }) => {
// Check if we already requested node for this remote file
// and return stored promise if we did.
if (processingCache[url]) {
return processingCache[url]
}

return (processingCache[url] = new Promise(async (resolve, reject) => {
if (!url || isWebUri(url) === undefined) {
resolve()
return
Expand Down Expand Up @@ -99,4 +110,5 @@ module.exports = ({ url, store, cache, createNode, auth = {} }) =>
resolve(fileNode)
})
})
})
}))
}

0 comments on commit 0152be0

Please sign in to comment.