Skip to content

Commit

Permalink
feat(gatsby-source-wordpress): Log when nodes are duplicates (#29941)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBarnes committed Mar 3, 2021
1 parent 528a459 commit 7ee63ea
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fetchGraphql from "~/utils/fetch-graphql"
import store from "~/store"
import { formatLogMessage } from "../../../utils/format-log-message"

export const normalizeNode = ({ node, nodeTypeName }) => {
const normalizedNodeTypeName = node.__typename || nodeTypeName
Expand All @@ -11,6 +12,13 @@ export const normalizeNode = ({ node, nodeTypeName }) => {
return node
}

// this is used to determine wether we've already seen an id
// for cold builds.
// the reason we want to do this is to detect if we create 2 nodes with the same id
// if we do there will be less nodes in Gatsby than in WP and users will be confused.
const idSet = new Set()
let hasLoggedDuplicateMessage = false

/**
* paginatedWpNodeFetch
*
Expand Down Expand Up @@ -85,6 +93,30 @@ const paginatedWpNodeFetch = async ({

if (nodes && nodes.length) {
nodes.forEach(node => {
const existingId = idSet.has(node.id)

if (existingId) {
const existingNode = allContentNodes.find(
innerNode => innerNode.id === node.id
)

if (!hasLoggedDuplicateMessage) {
hasLoggedDuplicateMessage = true
helpers.reporter.warn(
formatLogMessage(
`Found a duplicate ID in WordPress - this means you will have fewer nodes in Gatsby than in WordPress. This will need to be resolved in WP by identifying and fixing the underlying bug with your WP plugins or custom code.`
)
)
}
helpers.reporter.info(
formatLogMessage(
`#${node.databaseId} (${node?.uri}) is a duplicate of ${existingNode.databaseId} (${existingNode?.uri})`
)
)
} else {
idSet.add(node.id)
}

node = normalizeNode({ node, nodeTypeName })
allContentNodes.push(node)
})
Expand Down

0 comments on commit 7ee63ea

Please sign in to comment.