From c409352d39ca13ee5a543b354b0b5476a06421a3 Mon Sep 17 00:00:00 2001 From: Tyler Barnes Date: Mon, 23 Aug 2021 15:12:29 -0700 Subject: [PATCH] make sure remoteUrl exists before using it (#32885) --- .../src/steps/preview/index.ts | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/packages/gatsby-source-wordpress/src/steps/preview/index.ts b/packages/gatsby-source-wordpress/src/steps/preview/index.ts index ee62c392ce890..92ed072b3eef2 100644 --- a/packages/gatsby-source-wordpress/src/steps/preview/index.ts +++ b/packages/gatsby-source-wordpress/src/steps/preview/index.ts @@ -322,36 +322,41 @@ export const sourcePreviews = async (helpers: GatsbyHelpers): Promise => { 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 =