From 7e1cfdb457a2f7316542897e8a2bca2e39451dfd Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Thu, 25 May 2023 08:46:10 -0400 Subject: [PATCH] fix(gatsby-source-wordpress): allow using engines when using wordpress auth (#38103) (#38112) Co-authored-by: Lennart (cherry picked from commit 7adb331a113cb8541124fd15e2943ccd580b6399) Co-authored-by: Michal Piechowiak --- .../src/gatsby-node.ts | 2 ++ .../src/steps/auth-handling.ts | 28 +++++++++++++++++++ .../src/steps/index.ts | 5 ++++ .../process-and-validate-plugin-options.ts | 3 -- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 packages/gatsby-source-wordpress/src/steps/auth-handling.ts diff --git a/packages/gatsby-source-wordpress/src/gatsby-node.ts b/packages/gatsby-source-wordpress/src/gatsby-node.ts index c9ace78fa169b..abed4d9b1155d 100644 --- a/packages/gatsby-source-wordpress/src/gatsby-node.ts +++ b/packages/gatsby-source-wordpress/src/gatsby-node.ts @@ -8,7 +8,9 @@ module.exports = runApisInSteps({ steps.setErrorMap, steps.tempPreventMultipleInstances, steps.setRequestHeaders, + steps.hideAuthPluginOptions, ], + onPreBootstrap: [steps.restoreAuthPluginOptions], pluginOptionsSchema: steps.pluginOptionsSchema, diff --git a/packages/gatsby-source-wordpress/src/steps/auth-handling.ts b/packages/gatsby-source-wordpress/src/steps/auth-handling.ts new file mode 100644 index 0000000000000..af45942ae4990 --- /dev/null +++ b/packages/gatsby-source-wordpress/src/steps/auth-handling.ts @@ -0,0 +1,28 @@ +import type { GatsbyNodeApiHelpers } from "~/utils/gatsby-types" +import type { Step } from "./../utils/run-steps" +import type { IPluginOptions } from "~/models/gatsby-api" + +let storedAuthSettings: IPluginOptions["auth"] | undefined + +export const hideAuthPluginOptions: Step = async ( + _helpers: GatsbyNodeApiHelpers, + pluginOptions: IPluginOptions +): Promise => { + // store auth settings so we can restore them later + storedAuthSettings = pluginOptions.auth + + // remove auth from pluginOptions before we write out browser plugin options module, + // so we don't leak into the browser + delete pluginOptions.auth +} + +export const restoreAuthPluginOptions: Step = async ( + _helpers: GatsbyNodeApiHelpers, + pluginOptions: IPluginOptions +): Promise => { + if (storedAuthSettings) { + // if we stored auth settings, restore them now after we've written out browser plugin options module + // so engines can use them + pluginOptions.auth = storedAuthSettings + } +} diff --git a/packages/gatsby-source-wordpress/src/steps/index.ts b/packages/gatsby-source-wordpress/src/steps/index.ts index d4c880185c816..1bc05f0d92dd1 100644 --- a/packages/gatsby-source-wordpress/src/steps/index.ts +++ b/packages/gatsby-source-wordpress/src/steps/index.ts @@ -22,3 +22,8 @@ export { logPostBuildWarnings } from "~/steps/log-post-build-warnings" export { imageRoutes } from "~/steps/image-routes" export { setRequestHeaders } from "./set-request-headers" + +export { + hideAuthPluginOptions, + restoreAuthPluginOptions, +} from "./auth-handling" diff --git a/packages/gatsby-source-wordpress/src/steps/process-and-validate-plugin-options.ts b/packages/gatsby-source-wordpress/src/steps/process-and-validate-plugin-options.ts index 275ba84c594e0..e13f7e09fc0c5 100644 --- a/packages/gatsby-source-wordpress/src/steps/process-and-validate-plugin-options.ts +++ b/packages/gatsby-source-wordpress/src/steps/process-and-validate-plugin-options.ts @@ -167,8 +167,5 @@ export const processAndValidatePluginOptions = ( } }) - // remove auth from pluginOptions so we don't leak into the browser - delete pluginOptions.auth - return userPluginOptions }