Skip to content

Commit

Permalink
feat(gatsby-source-contentful): Add node manifest support for previews (
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBarnes authored Oct 13, 2021
1 parent 89cad45 commit 3a385a5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
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

0 comments on commit 3a385a5

Please sign in to comment.