|
| 1 | +// @ts-check |
| 2 | +import { hasFeature } from "gatsby-plugin-utils/index" |
| 3 | +import { getDataStore } from "gatsby/dist/datastore" |
| 4 | +import { untilNextEventLoopTick } from "./utils" |
| 5 | + |
| 6 | +// Array of all existing Contentful nodes. Make it global and incrementally update it because it's hella slow to recreate this on every data update for large sites. |
| 7 | +export const existingNodes = new Map() |
| 8 | + |
| 9 | +let allNodesLoopCount = 0 |
| 10 | + |
| 11 | +// "is" === object so it can be overridden by tests |
| 12 | +export const is = { |
| 13 | + firstSourceNodesCallOfCurrentNodeProcess: true, |
| 14 | +} |
| 15 | + |
| 16 | +export async function getExistingCachedNodes({ |
| 17 | + actions, |
| 18 | + getNode, |
| 19 | + pluginConfig, |
| 20 | +}) { |
| 21 | + const { touchNode } = actions |
| 22 | + |
| 23 | + const needToTouchNodes = |
| 24 | + !hasFeature(`stateful-source-nodes`) && |
| 25 | + is.firstSourceNodesCallOfCurrentNodeProcess |
| 26 | + |
| 27 | + if (existingNodes.size === 0) { |
| 28 | + memoryNodeCountsBySysType.Asset = 0 |
| 29 | + memoryNodeCountsBySysType.Entry = 0 |
| 30 | + |
| 31 | + const dataStore = getDataStore() |
| 32 | + const allNodeTypeNames = Array.from(dataStore.getTypes()) |
| 33 | + |
| 34 | + for (const typeName of allNodeTypeNames) { |
| 35 | + const typeNodes = dataStore.iterateNodesByType(typeName) |
| 36 | + |
| 37 | + const firstNodeOfType = Array.from(typeNodes.slice(0, 1))[0] |
| 38 | + |
| 39 | + if ( |
| 40 | + !firstNodeOfType || |
| 41 | + firstNodeOfType.internal.owner !== `gatsby-source-contentful` |
| 42 | + ) { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + for (const node of typeNodes) { |
| 47 | + if (needToTouchNodes) { |
| 48 | + touchNode(node) |
| 49 | + |
| 50 | + if (node?.fields?.includes(`localFile`)) { |
| 51 | + // Prevent GraphQL type inference from crashing on this property |
| 52 | + const fullNode = getNode(node.id) |
| 53 | + const localFileNode = getNode(fullNode.fields.localFile) |
| 54 | + touchNode(localFileNode) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (++allNodesLoopCount % 5000 === 0) { |
| 59 | + // dont block the event loop |
| 60 | + await untilNextEventLoopTick() |
| 61 | + } |
| 62 | + |
| 63 | + addNodeToExistingNodesCache(node) |
| 64 | + } |
| 65 | + |
| 66 | + // dont block the event loop |
| 67 | + await untilNextEventLoopTick() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + is.firstSourceNodesCallOfCurrentNodeProcess = false |
| 72 | + |
| 73 | + return { |
| 74 | + existingNodes, |
| 75 | + memoryNodeCountsBySysType, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const memoryNodeCountsBySysType = { |
| 80 | + Asset: 0, |
| 81 | + Entry: 0, |
| 82 | +} |
| 83 | + |
| 84 | +// store only the fields we need to compare to reduce memory usage. if a node is updated we'll use getNode to grab the whole node before updating it |
| 85 | +export function addNodeToExistingNodesCache(node) { |
| 86 | + if (node.internal.type === `ContentfulTag`) { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + if ( |
| 91 | + node.sys.type in memoryNodeCountsBySysType && |
| 92 | + !existingNodes.has(node.id) |
| 93 | + ) { |
| 94 | + memoryNodeCountsBySysType[node.sys.type] ||= 0 |
| 95 | + memoryNodeCountsBySysType[node.sys.type]++ |
| 96 | + } |
| 97 | + |
| 98 | + const cacheNode = { |
| 99 | + id: node.id, |
| 100 | + contentful_id: node.contentful_id, |
| 101 | + sys: { |
| 102 | + type: node.sys.type, |
| 103 | + }, |
| 104 | + node_locale: node.node_locale, |
| 105 | + children: node.children, |
| 106 | + internal: { |
| 107 | + owner: node.internal.owner, |
| 108 | + }, |
| 109 | + __memcache: true, |
| 110 | + } |
| 111 | + |
| 112 | + for (const key of Object.keys(node)) { |
| 113 | + if (key.endsWith(`___NODE`)) { |
| 114 | + cacheNode[key] = node[key] |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + existingNodes.set(node.id, cacheNode) |
| 119 | +} |
| 120 | + |
| 121 | +export function removeNodeFromExistingNodesCache(node) { |
| 122 | + if (node.internal.type === `ContentfulTag`) { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + if ( |
| 127 | + node.sys.type in memoryNodeCountsBySysType && |
| 128 | + existingNodes.has(node.id) |
| 129 | + ) { |
| 130 | + memoryNodeCountsBySysType[node.sys.type] ||= 0 |
| 131 | + memoryNodeCountsBySysType[node.sys.type]-- |
| 132 | + |
| 133 | + if (memoryNodeCountsBySysType[node.sys.type] < 0) { |
| 134 | + memoryNodeCountsBySysType[node.sys.type] = 0 |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + existingNodes.delete(node.id) |
| 139 | +} |
0 commit comments