Skip to content

Commit 0e9feeb

Browse files
TylerBarnespieh
andauthored
feat(gatsby-source-wordpress): opt out of stale node checks (#37920)
* opt out of stale node checks * update error message --------- Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent e258553 commit 0e9feeb

File tree

8 files changed

+69
-42
lines changed

8 files changed

+69
-42
lines changed

packages/gatsby-source-wordpress/src/steps/persist-cached-images.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Step } from "./../utils/run-steps"
22
import store from "~/store"
33
import { getGatsbyApi } from "~/utils/get-gatsby-api"
44
import { getPersistentCache } from "~/utils/cache"
5+
import { needToTouchNodes } from "~/utils/gatsby-features"
56

67
const persistPreviouslyCachedImages: Step = async (): Promise<void> => {
78
const { helpers, pluginOptions } = getGatsbyApi()
@@ -11,9 +12,11 @@ const persistPreviouslyCachedImages: Step = async (): Promise<void> => {
1112
`${pluginOptions.schema.typePrefix}MediaItem`
1213
)
1314

14-
// and touch them so they aren't garbage collected.
15-
// we will remove them as needed when receiving DELETE events from WP
16-
mediaItemNodes.forEach(node => helpers.actions.touchNode(node))
15+
if (needToTouchNodes) {
16+
// and if needed touch them so they aren't garbage collected.
17+
// we will remove them as needed when receiving DELETE events from WP
18+
mediaItemNodes.forEach(node => helpers.actions.touchNode(node))
19+
}
1720

1821
const imageNodeMetaByUrl = await getPersistentCache({
1922
key: `image-node-meta-by-url`,

packages/gatsby-source-wordpress/src/steps/source-nodes/create-nodes/fetch-and-create-non-node-root-fields.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createNodeWithSideEffects } from "./create-nodes"
55
import fetchReferencedMediaItemsAndCreateNodes from "../fetch-nodes/fetch-referenced-media-items"
66
import { CREATED_NODE_IDS } from "~/constants"
77
import { getPersistentCache, setPersistentCache } from "~/utils/cache"
8+
import { needToTouchNodes } from "../../../utils/gatsby-features"
89

910
const fetchAndCreateNonNodeRootFields = async () => {
1011
const state = store.getState()
@@ -69,20 +70,22 @@ const fetchAndCreateNonNodeRootFields = async () => {
6970
referencedMediaItemNodeIds: newMediaItemIds,
7071
})
7172

72-
const previouslyCachedNodeIds = await getPersistentCache({
73-
key: CREATED_NODE_IDS,
74-
})
75-
76-
const createdNodeIds = [
77-
...new Set([
78-
...(previouslyCachedNodeIds || []),
79-
...referencedMediaItemNodeIdsArray,
80-
]),
81-
]
82-
83-
// save the node id's so we can touch them on the next build
84-
// so that we don't have to refetch all nodes
85-
await setPersistentCache({ key: CREATED_NODE_IDS, value: createdNodeIds })
73+
if (needToTouchNodes) {
74+
const previouslyCachedNodeIds = await getPersistentCache({
75+
key: CREATED_NODE_IDS,
76+
})
77+
78+
const createdNodeIds = [
79+
...new Set([
80+
...(previouslyCachedNodeIds || []),
81+
...referencedMediaItemNodeIdsArray,
82+
]),
83+
]
84+
85+
// save the node id's so we can touch them on the next build
86+
// so that we don't have to refetch all nodes
87+
await setPersistentCache({ key: CREATED_NODE_IDS, value: createdNodeIds })
88+
}
8689

8790
store.dispatch.logger.stopActivityTimer({
8891
typeName: `MediaItems`,

packages/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-nodes.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
setPersistentCache,
1616
} from "~/utils/cache"
1717
import { buildTypeName } from "../../create-schema-customization/helpers"
18+
import { needToTouchNodes } from "../../../utils/gatsby-features"
1819

1920
/**
2021
* fetchWPGQLContentNodes
@@ -227,7 +228,9 @@ export const fetchAndCreateAllNodes = async () => {
227228
})
228229
}
229230

230-
// save the node id's so we can touch them on the next build
231-
// so that we don't have to refetch all nodes
232-
await setPersistentCache({ key: CREATED_NODE_IDS, value: createdNodeIds })
231+
if (needToTouchNodes) {
232+
// save the node id's so we can touch them on the next build
233+
// so that we don't have to refetch all nodes
234+
await setPersistentCache({ key: CREATED_NODE_IDS, value: createdNodeIds })
235+
}
233236
}

packages/gatsby-source-wordpress/src/steps/source-nodes/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import store from "~/store"
88
import fetchAndCreateNonNodeRootFields from "./create-nodes/fetch-and-create-non-node-root-fields"
99
import { allowFileDownloaderProgressBarToClear } from "./create-nodes/create-remote-file-node/progress-bar-promise"
1010
import { sourcePreviews } from "~/steps/preview"
11+
import { hasStatefulSourceNodes } from "~/utils/gatsby-features"
1112

1213
const sourceNodes: Step = async helpers => {
13-
const { cache, webhookBody, refetchAll } = helpers
14+
const { cache, webhookBody, refetchAll, actions } = helpers
15+
16+
if (hasStatefulSourceNodes) {
17+
actions.enableStatefulSourceNodes()
18+
}
1419

1520
// fetch non-node root fields such as settings.
1621
// For now, we're refetching them on every build

packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/fetch-node-updates.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import { fetchAndRunWpActions } from "./wp-actions"
33
import { formatLogMessage } from "~/utils/format-log-message"
44
import { getGatsbyApi } from "~/utils/get-gatsby-api"
55
import { getPersistentCache } from "~/utils/cache"
6+
import { needToTouchNodes } from "../../../utils/gatsby-features"
67

78
export const touchValidNodes = async () => {
9+
if (!needToTouchNodes) {
10+
return
11+
}
12+
813
const { helpers } = getGatsbyApi()
914
const { actions } = helpers
1015

packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/delete.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import { fetchGraphql } from "~/utils/fetch-graphql"
1010
import { getQueryInfoBySingleFieldName } from "../../helpers"
1111
import { CREATED_NODE_IDS } from "~/constants"
1212
import { setPersistentCache, getPersistentCache } from "~/utils/cache"
13+
import { needToTouchNodes } from "../../../../utils/gatsby-features"
1314

14-
const wpActionDELETE = async ({
15-
helpers,
16-
// cachedNodeIds,
17-
wpAction,
18-
}) => {
15+
const wpActionDELETE = async ({ helpers, wpAction }) => {
1916
const { reporter, actions, getNode } = helpers
2017

2118
try {
22-
const cachedNodeIds = await getPersistentCache({ key: CREATED_NODE_IDS })
19+
const cachedNodeIds = needToTouchNodes
20+
? await getPersistentCache({ key: CREATED_NODE_IDS })
21+
: null
2322

2423
// get the node ID from the WPGQL id
2524
const nodeId = wpAction.referencedNodeGlobalRelayID
@@ -60,7 +59,7 @@ const wpActionDELETE = async ({
6059
wpStore: store,
6160
})) || {}
6261

63-
if (additionalNodeIds && additionalNodeIds.length) {
62+
if (needToTouchNodes && additionalNodeIds && additionalNodeIds.length) {
6463
additionalNodeIds.forEach(id => cachedNodeIds.push(id))
6564
}
6665
}
@@ -81,12 +80,12 @@ const wpActionDELETE = async ({
8180
reporter.log(``)
8281
}
8382

84-
// Remove this from cached node id's so we don't try to touch it
85-
const validNodeIds = cachedNodeIds.filter(cachedId => cachedId !== nodeId)
83+
if (needToTouchNodes) {
84+
// Remove this from cached node id's so we don't try to touch it
85+
const validNodeIds = cachedNodeIds.filter(cachedId => cachedId !== nodeId)
8686

87-
await setPersistentCache({ key: CREATED_NODE_IDS, value: validNodeIds })
88-
89-
// return validNodeIds
87+
await setPersistentCache({ key: CREATED_NODE_IDS, value: validNodeIds })
88+
}
9089
} catch (e) {
9190
Object.entries(wpAction).forEach(([key, value]) => {
9291
reporter.warn(`${key} ${JSON.stringify(value)}`)

packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "~/steps/create-schema-customization/helpers"
2020
import { processNode } from "~/steps/source-nodes/create-nodes/process-node"
2121
import { getPersistentCache, setPersistentCache } from "~/utils/cache"
22+
import { needToTouchNodes } from "../../../../utils/gatsby-features"
2223

2324
export const fetchAndCreateSingleNode = async ({
2425
singleName,
@@ -170,7 +171,7 @@ export const createSingleNode = async ({
170171

171172
const { typeInfo } = getQueryInfoBySingleFieldName(singleName)
172173

173-
if (!cachedNodeIds) {
174+
if (!cachedNodeIds && needToTouchNodes) {
174175
cachedNodeIds = await getPersistentCache({ key: CREATED_NODE_IDS })
175176
}
176177

@@ -259,12 +260,11 @@ export const createSingleNode = async ({
259260

260261
if (remoteNode) {
261262
actions.createNode(remoteNode)
263+
}
262264

265+
if (remoteNode && needToTouchNodes) {
263266
cachedNodeIds.push(remoteNode.id)
264-
265-
if (additionalNodeIds && additionalNodeIds.length) {
266-
additionalNodeIds.forEach(id => cachedNodeIds.push(id))
267-
}
267+
additionalNodeIds?.forEach(id => cachedNodeIds.push(id))
268268

269269
await setPersistentCache({ key: CREATED_NODE_IDS, value: cachedNodeIds })
270270
}
@@ -304,13 +304,18 @@ const wpActionUPDATE = async ({ helpers, wpAction }) => {
304304
const existingNode = await getNode(nodeId)
305305

306306
if (wpAction.referencedNodeStatus !== `publish`) {
307-
// if the post status isn't publish anymore, we need to remove the node
308-
// by removing it from cached nodes so it's garbage collected by Gatsby
309-
const validNodeIds = cachedNodeIds.filter(cachedId => cachedId !== nodeId)
307+
if (needToTouchNodes) {
308+
// if the post status isn't publish anymore, we need to remove the node
309+
// by removing it from cached nodes so it's garbage collected by Gatsby
310+
const validNodeIds = cachedNodeIds.filter(cachedId => cachedId !== nodeId)
310311

311-
await setPersistentCache({ key: CREATED_NODE_IDS, value: validNodeIds })
312+
await setPersistentCache({ key: CREATED_NODE_IDS, value: validNodeIds })
313+
}
312314

313315
if (existingNode) {
316+
// calling touchNode here ensures owners is populated before we delete.
317+
// In some cases calling delete node without touching the node first throws an error. this is a bug in Gatsby that has been fixed but this code remains to be backwards compatible with earlier versions that have the bug still.
318+
// TODO remove in the next major version
314319
await actions.touchNode(existingNode)
315320
await actions.deleteNode(existingNode)
316321
reportUpdate({ setAction: `DELETE` })
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { hasFeature } from "gatsby-plugin-utils"
2+
3+
export const hasStatefulSourceNodes = hasFeature(`stateful-source-nodes`)
4+
export const needToTouchNodes = !hasStatefulSourceNodes

0 commit comments

Comments
 (0)