diff --git a/packages/gatsby-source-drupal/src/__tests__/index.js b/packages/gatsby-source-drupal/src/__tests__/index.js index a06e84cf1f3dc..2df04931d516b 100644 --- a/packages/gatsby-source-drupal/src/__tests__/index.js +++ b/packages/gatsby-source-drupal/src/__tests__/index.js @@ -20,6 +20,15 @@ jest.mock(`gatsby-source-filesystem`, () => { } }) +function makeCache() { + const store = new Map() + return { + get: async id => store.get(id), + set: async (key, value) => store.set(key, value), + store, + } +} + const normalize = require(`../normalize`) const downloadFileSpy = jest.spyOn(normalize, `downloadFile`) @@ -75,6 +84,7 @@ describe(`gatsby-source-drupal`, () => { store, getNode: id => nodes[id], getNodes, + cache: makeCache(), } beforeAll(async () => { diff --git a/packages/gatsby-source-drupal/src/gatsby-node.js b/packages/gatsby-source-drupal/src/gatsby-node.js index 7ca4bdb25a0ac..14b961e1e9a16 100644 --- a/packages/gatsby-source-drupal/src/gatsby-node.js +++ b/packages/gatsby-source-drupal/src/gatsby-node.js @@ -12,6 +12,8 @@ const { setOptions, getOptions } = require(`./plugin-options`) const { nodeFromData, downloadFile, isFileNode } = require(`./normalize`) const { + initRefsLookups, + storeRefsLookups, handleReferences, handleWebhookUpdate, handleDeletedNode, @@ -150,6 +152,8 @@ exports.sourceNodes = async ( } = pluginOptions const { createNode, setPluginStatus, touchNode } = actions + await initRefsLookups({ cache, getNode }) + // Update the concurrency limit from the plugin options requestQueue.concurrency = concurrentAPIRequests @@ -202,6 +206,7 @@ ${JSON.stringify(webhookBody, null, 4)}` } changesActivity.end() + await storeRefsLookups({ cache }) return } @@ -232,6 +237,7 @@ ${JSON.stringify(webhookBody, null, 4)}` return } changesActivity.end() + await storeRefsLookups({ cache }) return } @@ -362,6 +368,7 @@ ${JSON.stringify(webhookBody, null, 4)}` drupalFetchIncrementalActivity.end() fastBuildsSpan.finish() + await storeRefsLookups({ cache }) return } @@ -372,6 +379,7 @@ ${JSON.stringify(webhookBody, null, 4)}` initialSourcing = false if (!requireFullRebuild) { + await storeRefsLookups({ cache }) return } } @@ -635,6 +643,7 @@ ${JSON.stringify(webhookBody, null, 4)}` initialSourcing = false createNodesSpan.finish() + await storeRefsLookups({ cache, getNodes }) return } diff --git a/packages/gatsby-source-drupal/src/utils.js b/packages/gatsby-source-drupal/src/utils.js index eda40f3437987..858d861db447e 100644 --- a/packages/gatsby-source-drupal/src/utils.js +++ b/packages/gatsby-source-drupal/src/utils.js @@ -9,8 +9,38 @@ const { const { getOptions } = require(`./plugin-options`) -const backRefsNamesLookup = new Map() -const referencedNodesLookup = new Map() +let backRefsNamesLookup = new Map() +let referencedNodesLookup = new Map() + +const initRefsLookups = async ({ cache }) => { + const backRefsNamesLookupStr = await cache.get(`backRefsNamesLookup`) + const referencedNodesLookupStr = await cache.get(`referencedNodesLookup`) + + if (backRefsNamesLookupStr) { + backRefsNamesLookup = new Map(JSON.parse(backRefsNamesLookupStr)) + } + + if (referencedNodesLookupStr) { + referencedNodesLookup = new Map(JSON.parse(referencedNodesLookupStr)) + } +} + +exports.initRefsLookups = initRefsLookups + +const storeRefsLookups = async ({ cache }) => { + await Promise.all([ + cache.set( + `backRefsNamesLookup`, + JSON.stringify(Array.from(backRefsNamesLookup.entries())) + ), + cache.set( + `referencedNodesLookup`, + JSON.stringify(Array.from(referencedNodesLookup.entries())) + ), + ]) +} + +exports.storeRefsLookups = storeRefsLookups const handleReferences = ( node, @@ -333,7 +363,9 @@ ${JSON.stringify(nodeToUpdate, null, 4)} } node.internal.contentDigest = createContentDigest(node) createNode(node) - reporter.log(`Updated Gatsby node: ${node.id}`) + reporter.log( + `Updated Gatsby node: id: ${node.id} — type: ${node.internal.type}` + ) } }