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-source-contentful): Add node manifest support for previews #33297

Merged
merged 28 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6b4fc62
call createNodeManifest before creating nodes during previews
TylerBarnes Sep 23, 2021
0e2ec01
only call createNodeManifest if it's a fn
TylerBarnes Sep 23, 2021
fac3e04
add warning about old versions of Gatsby core for unstable_createNode…
TylerBarnes Sep 27, 2021
bc95598
Update normalize.js
TylerBarnes Sep 27, 2021
6968dc8
canary log
TylerBarnes Sep 27, 2021
418eec6
check host plugin setting instead of environment
TylerBarnes Sep 28, 2021
1ff73f8
log out manifest ID
TylerBarnes Sep 28, 2021
76f7e2f
use webhook body temporarily
TylerBarnes Sep 28, 2021
575f7c1
webhooks don't always exist
TylerBarnes Sep 28, 2021
9c9e37a
create manifests if a webhook body exists
TylerBarnes Sep 28, 2021
af5b793
revert changes
TylerBarnes Sep 28, 2021
4ce2c33
more fixes
TylerBarnes Sep 28, 2021
afc0fc3
only create manifests for contentful entries updated in the last 5 mins
TylerBarnes Sep 29, 2021
0677f71
add debugging log via env var
TylerBarnes Oct 5, 2021
d9cbaf2
Update normalize.js
TylerBarnes Oct 5, 2021
ede1b68
formatting
TylerBarnes Oct 5, 2021
a6093ac
move log to 1 line for Cloud
TylerBarnes Oct 5, 2021
1d16948
arbitrary change
TylerBarnes Oct 7, 2021
7724a66
create node manifests for updates in the last 2 days
TylerBarnes Oct 8, 2021
7d92aac
comment
TylerBarnes Oct 8, 2021
3973b2b
abstract out node manifest logic
TylerBarnes Oct 8, 2021
cce7cda
Merge branch 'master' into feat/add-contentful-node-manifest
TylerBarnes Oct 13, 2021
4a9d726
Merge branch 'master' into feat/add-contentful-node-manifest
TylerBarnes Oct 13, 2021
ae771ee
cast env var to number
TylerBarnes Oct 13, 2021
0132ac8
format
TylerBarnes Oct 13, 2021
373af2b
canary
TylerBarnes Oct 13, 2021
fcc5a73
pass missing args
TylerBarnes Oct 13, 2021
2cf8f03
Revert "canary"
TylerBarnes Oct 13, 2021
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
88 changes: 88 additions & 0 deletions packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,89 @@ function prepareJSONNode(id, node, key, content) {
return JSONNode
}

let numberOfContentSyncDebugLogs = 0
const maxContentSyncDebugLogTimes = 50

/**
* This fn creates node manifests which are used for Gatsby Cloud Previews via the Content Sync API/feature.
* Content Sync routes a user from Contentful to a page created from the entry data they're interested in previewing.
*/
function contentfulCreateNodeManifest({
pluginConfig,
syncToken,
entryItem,
entryNode,
space,
unstable_createNodeManifest,
}) {
const isPreview = pluginConfig.get(`host`) === `preview.contentful.com`

const createNodeManifestIsSupported =
typeof unstable_createNodeManifest === `function`

const cacheExists = !!syncToken

const shouldCreateNodeManifest =
isPreview &&
createNodeManifestIsSupported &&
// and this is a delta update
(cacheExists ||
// or this entry/node was updated in the last 2 days.
// we don't want older nodes because we only want to create
// node manifests for recently updated/created content.
(entryItem.sys.updatedAt &&
Date.now() - new Date(entryItem.sys.updatedAt).getTime() <=
// milliseconds
1000 *
// seconds
60 *
// minutes
60 *
// hours
(Number(
process.env.CONTENT_SYNC_CONTENTFUL_HOURS_SINCE_ENTRY_UPDATE
) || 48)))

const manifestId = `${space.sys.id}-${entryItem.sys.id}-${entryItem.sys.updatedAt}`

if (
process.env.CONTENTFUL_DEBUG_NODE_MANIFEST === `true` &&
numberOfContentSyncDebugLogs <= maxContentSyncDebugLogTimes
) {
numberOfContentSyncDebugLogs++

console.info(
JSON.stringify({
cacheExists,
isPreview,
createNodeManifestIsSupported,
shouldCreateNodeManifest,
manifestId,
entryItemSysUpdatedAt: entryItem.sys.updatedAt,
})
)
}

if (shouldCreateNodeManifest) {
console.info(`Contentful: Creating node manifest with id ${manifestId}`)

unstable_createNodeManifest({
manifestId,
node: entryNode,
})
} else if (isPreview && !createNodeManifestIsSupported) {
console.warn(
`Contentful: Your version of Gatsby core doesn't support Content Sync (via the unstable_createNodeManifest action). Please upgrade to the latest version to use Content Sync in your site.`
)
}
}

export const createNodesForContentType = ({
contentTypeItem,
restrictedNodeFields,
conflictFieldPrefix,
entries,
unstable_createNodeManifest,
createNode,
createNodeId,
getNode,
Expand All @@ -234,6 +312,7 @@ export const createNodesForContentType = ({
locales,
space,
useNameForId,
syncToken,
pluginConfig,
}) => {
// Establish identifier for content type
Expand Down Expand Up @@ -419,6 +498,15 @@ export const createNodesForContentType = ({
},
}

contentfulCreateNodeManifest({
pluginConfig,
syncToken,
entryItem,
entryNode,
space,
unstable_createNodeManifest,
})

// Revision applies to entries, assets, and content types
if (entryItem.sys.revision) {
entryNode.sys.revision = entryItem.sys.revision
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-source-contentful/src/source-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export async function sourceNodes(
},
pluginOptions
) {
const { createNode, touchNode, deleteNode } = actions
const { createNode, touchNode, deleteNode, unstable_createNodeManifest } =
actions
const online = await isOnline()

if (
Expand Down Expand Up @@ -427,6 +428,8 @@ export async function sourceNodes(
space,
useNameForId: pluginConfig.get(`useNameForId`),
pluginConfig,
syncToken,
unstable_createNodeManifest,
})
)
}
Expand Down