Skip to content

Commit

Permalink
make sure remoteUrl exists before using it (#32885)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBarnes committed Aug 23, 2021
1 parent 114e3d3 commit c409352
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions packages/gatsby-source-wordpress/src/steps/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,36 +322,41 @@ export const sourcePreviews = async (helpers: GatsbyHelpers): Promise<void> => {
url,
} = getPluginOptions()

const { hostname: settingsHostname } = urlUtil.parse(url)
const { hostname: remoteHostname } = urlUtil.parse(webhookBody.remoteUrl)

if (settingsHostname !== remoteHostname) {
const sendPreviewStatus = createPreviewStatusCallback({
previewData: webhookBody,
reporter,
})
// some versions of WPGatsby don't send a remoteUrl on every webhook.
// if we check this for every webhookBody errors will occur!
if (webhookBody.remoteUrl) {
// check if we're receiving preview data fromt the right WP backend
const { hostname: settingsHostname } = urlUtil.parse(url)
const { hostname: remoteHostname } = urlUtil.parse(webhookBody.remoteUrl)

if (settingsHostname !== remoteHostname) {
const sendPreviewStatus = createPreviewStatusCallback({
previewData: webhookBody,
reporter,
})

await sendPreviewStatus({
status: `RECEIVED_PREVIEW_DATA_FROM_WRONG_URL`,
context: `check that the preview data came from the right URL.`,
passedNode: {
modified: webhookBody.modified,
databaseId: webhookBody.parentDatabaseId,
},
graphqlEndpoint: webhookBody.remoteUrl,
})
await sendPreviewStatus({
status: `RECEIVED_PREVIEW_DATA_FROM_WRONG_URL`,
context: `check that the preview data came from the right URL.`,
passedNode: {
modified: webhookBody.modified,
databaseId: webhookBody.parentDatabaseId,
},
graphqlEndpoint: webhookBody.remoteUrl,
})

reporter.warn(
formatLogMessage(
`Received preview data from a different remote URL than the one specified in plugin options. Preview will not work. Please send preview requests from the WP instance configured in gatsby-config.js.\n\n ${chalk.bold(
`Remote URL:`
)} ${webhookBody.remoteUrl}\n ${chalk.bold(
`Plugin options URL:`
)} ${url}\n\n`
reporter.warn(
formatLogMessage(
`Received preview data from a different remote URL than the one specified in plugin options. Preview will not work. Please send preview requests from the WP instance configured in gatsby-config.js.\n\n ${chalk.bold(
`Remote URL:`
)} ${webhookBody.remoteUrl}\n ${chalk.bold(
`Plugin options URL:`
)} ${url}\n\n`
)
)
)

return
return
}
}

const inPreviewDebugMode =
Expand Down

0 comments on commit c409352

Please sign in to comment.