Skip to content
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

[gatsby-source-wordpress] dont request remote file if we already have cached up to date version of it #4872

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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