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

feat(gatsby, gatsby-source-contentful): add public action to disable stale node checks #37782

Merged
merged 44 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
73fc998
add touch nodes optout action
TylerBarnes Mar 20, 2023
3a90c4e
cleanup
TylerBarnes Mar 24, 2023
bc10de5
revert formatting
TylerBarnes Mar 27, 2023
b7856b1
remove unused variable
TylerBarnes Mar 27, 2023
e3b5356
remove unused var
TylerBarnes Mar 27, 2023
f4de579
switch from opting out types from stale nodes to opting out entire pl…
TylerBarnes Mar 29, 2023
05bb75b
add no nodes warning
TylerBarnes Mar 29, 2023
0eae24b
fix bug where double bound actions are ignored when there are no args
TylerBarnes Mar 29, 2023
711e5f1
update progress mock
TylerBarnes Mar 29, 2023
006781d
get owner from plugin
TylerBarnes Mar 29, 2023
ed37dbe
rename type
TylerBarnes Mar 29, 2023
34afa79
rename types
TylerBarnes Mar 29, 2023
d643f22
use redux types instead of pulling from lmdb
TylerBarnes Mar 29, 2023
9edecac
remove unused line
TylerBarnes Mar 29, 2023
ba945e9
Update source-nodes.ts
TylerBarnes Mar 29, 2023
7db6014
use CREATE_NODE action instead of adding a new type owner action
TylerBarnes Mar 29, 2023
8000e0f
Merge branch 'master' into feat/disable-node-gc-by-type
TylerBarnes Mar 29, 2023
a0914e0
add typeowners test
TylerBarnes Mar 29, 2023
bc48550
test touchNodes and enableStatefulSourceNodes()
TylerBarnes Mar 30, 2023
fa7b00c
fix contentful tests
TylerBarnes Mar 30, 2023
4422dce
snapshot updates
TylerBarnes Mar 30, 2023
d5db06b
chore(changelogs): update changelogs (#37808)
ViCo0TeCH Mar 30, 2023
c2bb5d5
fix(deps): update starters and examples - gatsby to ^5.8.1 (#37806)
renovate[bot] Mar 30, 2023
b55761d
fix(gatsby): Validate sub plugins options (#37804)
Talaxy009 Mar 30, 2023
95a2944
fix(create-gatsby): Use correct name in summary message (#37809)
LekoArts Mar 30, 2023
2fbc24d
chore(release): Publish next
pieh Mar 30, 2023
8be2932
Merge branch 'master' into feat/disable-node-gc-by-type
TylerBarnes Mar 30, 2023
70e77ff
remove testing timeout
TylerBarnes Mar 30, 2023
de0a235
minimal docs
TylerBarnes Mar 31, 2023
bdcc992
reword
TylerBarnes Mar 31, 2023
a89bb13
add comment
TylerBarnes Mar 31, 2023
b83d888
reportOnce instead of throwing an error
TylerBarnes Mar 31, 2023
e859aa8
consolidate typeOwners
TylerBarnes Mar 31, 2023
6ebba79
use new typesToPlugins Map keys instead of pluginsToTypes Map values
TylerBarnes Mar 31, 2023
13df0c2
consolidate remaining typeOwners object checks into new typeOwners re…
TylerBarnes Apr 3, 2023
c7195ef
fix missing owner error
TylerBarnes Apr 3, 2023
acb13d9
fix type errors and incorrect plugin object reference
TylerBarnes Apr 3, 2023
236f29f
maybe fix unit tests
pieh Apr 4, 2023
1ddaa40
make SitePage nodes owned by internal-data-bridge
pieh Apr 4, 2023
1405646
add missing fields on some TOUCH_NODE actions
pieh Apr 4, 2023
b121925
skip owner checks when deleting child nodes
pieh Apr 4, 2023
7760b01
sp
TylerBarnes Apr 4, 2023
11ff633
Merge branch 'master' into feat/disable-node-gc-by-type
TylerBarnes Apr 4, 2023
f86ca48
Update yarn.lock
TylerBarnes Apr 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { getGatsbyVersion } from "gatsby-core-utils"
import { lt, prerelease } from "semver"

const typePrefix = `Contentful`
const makeTypeName = type => _.upperFirst(_.camelCase(`${typePrefix} ${type}`))
export const makeTypeName = type =>
_.upperFirst(_.camelCase(`${typePrefix} ${type}`))

const GATSBY_VERSION_MANIFEST_V2 = `4.3.0`
const gatsbyVersion =
Expand Down
44 changes: 39 additions & 5 deletions packages/gatsby-source-contentful/src/source-nodes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
import { hasFeature } from "gatsby-plugin-utils/has-feature"
import isOnline from "is-online"
import _ from "lodash"

Expand All @@ -11,6 +12,7 @@ import {
createAssetNodes,
createNodesForContentType,
makeId,
makeTypeName,
} from "./normalize"
import { createPluginConfig } from "./plugin-options"
import { CODES } from "./report"
Expand Down Expand Up @@ -40,7 +42,7 @@ const CONTENT_DIGEST_COUNTER_SEPARATOR = `_COUNT_`
* or the fallback field or the default field.
*/

let isFirstSource = true
let isFirstSourceNodesCallOfCurrentNodeProcess = true
export async function sourceNodes(
{
actions,
Expand All @@ -55,14 +57,22 @@ export async function sourceNodes(
},
pluginOptions
) {
const { createNode, touchNode, deleteNode, unstable_createNodeManifest } =
actions
const canDisableStaleNodeChecks = hasFeature(`disable-stale-node-type-checks`)
const needToTouchNodes = !canDisableStaleNodeChecks

const {
createNode: originalCreateNode,
touchNode,
deleteNode,
unstable_createNodeManifest,
disableStaleNodeTypeCheck,
} = actions
const online = await isOnline()

// Gatsby only checks if a node has been touched on the first sourcing.
// As iterating and touching nodes can grow quite expensive on larger sites with
// 1000s of nodes, we'll skip doing this on subsequent sources.
if (isFirstSource) {
if (isFirstSourceNodesCallOfCurrentNodeProcess && needToTouchNodes) {
getNodes().forEach(node => {
if (node.internal.owner !== `gatsby-source-contentful`) {
return
Expand All @@ -73,9 +83,10 @@ export async function sourceNodes(
touchNode(getNode(node.fields.localFile))
}
})
isFirstSource = false
}

isFirstSourceNodesCallOfCurrentNodeProcess = false

if (
!online &&
process.env.GATSBY_CONTENTFUL_OFFLINE === `true` &&
Expand Down Expand Up @@ -115,6 +126,7 @@ export async function sourceNodes(

fetchActivity.start()

const CREATED_TYPENAMES = `contentful-created-typenames-${sourceId}`
const CACHE_SYNC_TOKEN = `contentful-sync-token-${sourceId}`
const CACHE_CONTENT_TYPES = `contentful-content-types-${sourceId}`
const CACHE_FOREIGN_REFERENCE_MAP_STATE = `contentful-foreign-reference-map-state-${sourceId}`
Expand Down Expand Up @@ -300,6 +312,20 @@ export async function sourceNodes(
})
}

const createdTypeNames = canDisableStaleNodeChecks
? new Set((await cache.get(CREATED_TYPENAMES)) || [])
: null

// wrap createNode so we can track the typenames of nodes we create
// and call disableStaleNodeTypeCheck for them
const createNode = node => {
if (createdTypeNames && node?.internal?.type) {
createdTypeNames.add(node.internal.type)
}

return originalCreateNode(node)
}

if (deletedEntries.length || deletedAssets.length) {
const deletionActivity = reporter.activityTimer(
`Contentful: Deleting nodes and assets`,
Expand Down Expand Up @@ -540,4 +566,12 @@ export async function sourceNodes(
assetDownloadWorkers: pluginConfig.get(`assetDownloadWorkers`),
})
}

if (canDisableStaleNodeChecks && createdTypeNames) {
reporter.verbose(
`Contentful disabling garbage collection for ${createdTypeNames.size} node types`
)
createdTypeNames.forEach(disableStaleNodeTypeCheck)
await cache.set(CREATED_TYPENAMES, [...createdTypeNames])
}
}