diff --git a/packages/gatsby/src/redux/reducers/queries.ts b/packages/gatsby/src/redux/reducers/queries.ts index dd4a67e1a311d..53434afb24088 100644 --- a/packages/gatsby/src/redux/reducers/queries.ts +++ b/packages/gatsby/src/redux/reducers/queries.ts @@ -90,6 +90,20 @@ export function queriesReducer( state.deletedQueries.add(action.payload.path) return state } + case `DELETED_STALE_PAGE_DATA_FILES`: { + // this action is a hack/hot fix + // it should be removed/reverted when we start persisting pages state + for (const queryId of action.payload.pagePathsToClear) { + for (const component of state.trackedComponents.values()) { + component.pages.delete(queryId) + } + state = clearNodeDependencies(state, queryId) + state = clearConnectionDependencies(state, queryId) + state.trackedQueries.delete(queryId) + } + + return state + } case `API_FINISHED`: { if (action.payload.apiName !== `createPages`) { return state diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index eb8974686b936..2693b96f73511 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -352,6 +352,7 @@ export type ActionsUnion = | IDisableTypeInferenceAction | ISetProgramAction | ISetProgramExtensions + | IDeletedStalePageDataFiles export interface IApiFinishedAction { type: `API_FINISHED` @@ -804,3 +805,10 @@ interface ISetProgramExtensions { type: `SET_PROGRAM_EXTENSIONS` payload: Array } + +interface IDeletedStalePageDataFiles { + type: `DELETED_STALE_PAGE_DATA_FILES` + payload: { + pagePathsToClear: Set + } +} diff --git a/packages/gatsby/src/utils/page-data.ts b/packages/gatsby/src/utils/page-data.ts index 0a3a1c6bd068a..e69b5c1952c1d 100644 --- a/packages/gatsby/src/utils/page-data.ts +++ b/packages/gatsby/src/utils/page-data.ts @@ -256,10 +256,20 @@ export async function handleStalePageData(): Promise { }) const deletionPromises: Array> = [] - pageDataFilesFromPreviousBuilds.forEach(pageDataFilePath => { + const pagePathsToClear = new Set() + for (const pageDataFilePath of pageDataFilesFromPreviousBuilds) { if (!expectedPageDataFiles.has(pageDataFilePath)) { + const stalePageDataContent = await fs.readJson(pageDataFilePath) + pagePathsToClear.add(stalePageDataContent.path) deletionPromises.push(fs.remove(pageDataFilePath)) } + } + + store.dispatch({ + type: `DELETED_STALE_PAGE_DATA_FILES`, + payload: { + pagePathsToClear, + }, }) await Promise.all(deletionPromises)