Skip to content

Commit

Permalink
fix(gatsby-source-wordpress): Enable preview mode in PINC builds (#30601
Browse files Browse the repository at this point in the history
) (#30627)

* clean up preview mode logic and check for new INCREMENTAL_PREVIEWS runner type

(cherry picked from commit d97b009)

Co-authored-by: Tyler Barnes <[email protected]>
  • Loading branch information
GatsbyJS Bot and TylerBarnes authored Apr 1, 2021
1 parent a4f8a14 commit 63d3716
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
11 changes: 3 additions & 8 deletions packages/gatsby-source-wordpress/src/models/develop.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { inPreviewMode } from "~/steps/preview"
export interface IDevelopState {
refreshPollingIsPaused: boolean
}
Expand All @@ -19,20 +20,14 @@ const developStore: IPreviewStore = {

reducers: {
pauseRefreshPolling(state) {
if (
process.env.NODE_ENV === `development` &&
!process.env.ENABLE_GATSBY_REFRESH_ENDPOINT
) {
if (!inPreviewMode()) {
state.refreshPollingIsPaused = true
}

return state
},
resumeRefreshPolling(state) {
if (
process.env.NODE_ENV === `development` &&
!process.env.ENABLE_GATSBY_REFRESH_ENDPOINT
) {
if (!inPreviewMode()) {
state.refreshPollingIsPaused = false
}

Expand Down
9 changes: 2 additions & 7 deletions packages/gatsby-source-wordpress/src/models/gatsby-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import merge from "lodash/merge"
import { createRemoteMediaItemNode } from "~/steps/source-nodes/create-nodes/create-remote-media-item-node"
import { menuBeforeChangeNode } from "~/steps/source-nodes/before-change-node/menu"
import { cloneDeep } from "lodash"
import { inPreviewMode } from "~/steps/preview"

export interface IPluginOptionsPreset {
presetName: string
Expand All @@ -13,15 +14,9 @@ export interface IPluginOptionsPreset {
options: IPluginOptions
}

const inDevelopPreview =
process.env.NODE_ENV === `development` &&
!!process.env.ENABLE_GATSBY_REFRESH_ENDPOINT

const inPreviewRunner = process.env.RUNNER_TYPE === `PREVIEW`

export const previewOptimizationPreset: IPluginOptionsPreset = {
presetName: `PREVIEW_OPTIMIZATION`,
useIf: (): boolean => inDevelopPreview || inPreviewRunner,
useIf: inPreviewMode,
options: {
html: {
useGatsbyImage: false,
Expand Down
8 changes: 0 additions & 8 deletions packages/gatsby-source-wordpress/src/models/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface IStoredPage {
}

export interface IPreviewState {
inPreviewMode: boolean
nodePageCreatedCallbacks: {
[nodeId: string]: OnPageCreatedCallback
}
Expand Down Expand Up @@ -56,19 +55,12 @@ export interface IPreviewStore {

const previewStore: IPreviewStore = {
state: {
inPreviewMode: false,
nodePageCreatedCallbacks: {},
nodeIdsToCreatedPages: {},
pagePathToNodeDependencyId: {},
},

reducers: {
setInPreviewMode(state, inPreviewMode) {
state.inPreviewMode = inPreviewMode

return state
},

unSubscribeToPagesCreatedFromNodeById(state, { nodeId }) {
if (state.nodePageCreatedCallbacks?.[nodeId]) {
delete state.nodePageCreatedCallbacks[nodeId]
Expand Down
17 changes: 12 additions & 5 deletions packages/gatsby-source-wordpress/src/steps/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ import { touchValidNodes } from "../source-nodes/update-nodes/fetch-node-updates
import { IPluginOptions } from "~/models/gatsby-api"
import { Reporter } from "gatsby/reporter"

export const inPreviewMode = (): boolean =>
!!process.env.ENABLE_GATSBY_REFRESH_ENDPOINT &&
!!store.getState().previewStore.inPreviewMode
const inDevelopPreview =
process.env.NODE_ENV === `development` &&
!!process.env.ENABLE_GATSBY_REFRESH_ENDPOINT

const inPreviewRunner =
process.env.RUNNER_TYPE === `PREVIEW` ||
process.env.RUNNER_TYPE === `INCREMENTAL_PREVIEWS` ||
!!process.env.IS_GATSBY_PREVIEW

// this is a function simply because many places in the code expect it to be.
// it used to call store.getState() and check for some state to determine preview mode
export const inPreviewMode = (): boolean => inDevelopPreview || inPreviewRunner

export type PreviewStatusUnion =
| `PREVIEW_SUCCESS`
Expand Down Expand Up @@ -279,8 +288,6 @@ export const sourcePreview = async (
return
}

store.dispatch.previewStore.setInPreviewMode(true)

// this callback will be invoked when the page is created/updated for this node
// then it'll send a mutation to WPGraphQL so that WP knows the preview is ready
store.dispatch.previewStore.subscribeToPagesCreatedFromNodeById({
Expand Down

0 comments on commit 63d3716

Please sign in to comment.