Skip to content

Commit

Permalink
[gatsby-source-wordpress] take advantage of modified field in media…
Browse files Browse the repository at this point in the history
… rest endpoint to not request file if we already have it (#4872)
  • Loading branch information
pieh authored and KyleAMathews committed Apr 6, 2018
1 parent 362f78a commit f86576e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby-source-wordpress/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.sourceNodes = async (
excludedRoutes = [],
}
) => {
const { createNode } = boundActionCreators
const { createNode, touchNode } = boundActionCreators
_verbose = verboseOutput
_siteURL = `${protocol}://${baseUrl}`
_useACF = useACF
Expand Down Expand Up @@ -99,6 +99,7 @@ exports.sourceNodes = async (
store,
cache,
createNode,
touchNode,
_auth,
})

Expand Down
49 changes: 36 additions & 13 deletions packages/gatsby-source-wordpress/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,27 +393,50 @@ exports.downloadMediaFiles = async ({
store,
cache,
createNode,
touchNode,
_auth,
}) =>
Promise.all(
entities.map(async e => {
let fileNode
let fileNodeID
if (e.__type === `wordpress__wp_media`) {
try {
fileNode = await createRemoteFileNode({
url: e.source_url,
store,
cache,
createNode,
auth: _auth,
})
} catch (e) {
// Ignore
const mediaDataCacheKey = `wordpress-media-${e.wordpress_id}`
const cacheMediaData = await cache.get(mediaDataCacheKey)

// If we have cached media data and it wasn't modified, reuse
// previously created file node to not try to redownload
if (cacheMediaData && e.modified === cacheMediaData.modified) {
fileNodeID = cacheMediaData.fileNodeID
touchNode(cacheMediaData.fileNodeID)
}

// If we don't have cached data, download the file
if (!fileNodeID) {
try {
const fileNode = await createRemoteFileNode({
url: e.source_url,
store,
cache,
createNode,
auth: _auth,
})

if (fileNode) {
fileNodeID = fileNode.id

await cache.set(mediaDataCacheKey, {
fileNodeID,
modified: e.modified,
})
}
} catch (e) {
// Ignore
}
}
}

if (fileNode) {
e.localFile___NODE = fileNode.id
if (fileNodeID) {
e.localFile___NODE = fileNodeID
delete e.media_details.sizes
}

Expand Down

0 comments on commit f86576e

Please sign in to comment.