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] prevent duplicate nodes for remote media files #3842

Closed
Closed
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
74 changes: 51 additions & 23 deletions packages/gatsby-source-wordpress/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,32 +385,60 @@ exports.downloadMediaFiles = async ({
cache,
createNode,
_auth,
}) =>
Promise.all(
entities.map(async e => {
let fileNode
if (e.__type === `wordpress__wp_media`) {
try {
fileNode = await createRemoteFileNode({
url: e.source_url,
store,
cache,
createNode,
auth: _auth,
})
} catch (e) {
// Ignore
}) => {
const remoteCache = {}
const cacheId = url => `gatsby-source-wordpress-remote-file-node-id-${url}`

const createMediaFileNodes = async () =>
Promise.all(
entities.map(async e => {
if (e.source_url && remoteCache[e.source_url]) {
return e
}
}

if (fileNode) {
e.localFile___NODE = fileNode.id
delete e.media_details.sizes
}
let fileNode
if (e.__type === `wordpress__wp_media`) {
try {
remoteCache[e.source_url] = true
fileNode = await createRemoteFileNode({
url: e.source_url,
store,
cache,
createNode,
auth: _auth,
})
} catch (e) {
// Ignore
}
}
if (fileNode) {
await cache.set(cacheId(e.source_url), fileNode.id)
}

return e
})
)
return e
})
)

const mapLocalFileNodes = async () =>
Promise.all(
entities.map(async e => {
if (e.__type === `wordpress__wp_media`) {
const cachedNodeId = await cache.get(cacheId(e.source_url))
if (cachedNodeId) {
e.localFile___NODE = cachedNodeId
delete e.media_details.sizes
}
}

return e
})
)

entities = await createMediaFileNodes()
entities = await mapLocalFileNodes()

return entities
}

const createACFChildNodes = (
obj,
Expand Down