Skip to content

Commit 478cf68

Browse files
authored
fix: clear tracked queries when deleting stale page-data files (#29431)
* fix: clear tracked queries when deleting stale page-data files * add comment about hot-fix nature of added action
1 parent e567aa8 commit 478cf68

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

packages/gatsby/src/redux/reducers/queries.ts

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ export function queriesReducer(
9090
state.deletedQueries.add(action.payload.path)
9191
return state
9292
}
93+
case `DELETED_STALE_PAGE_DATA_FILES`: {
94+
// this action is a hack/hot fix
95+
// it should be removed/reverted when we start persisting pages state
96+
for (const queryId of action.payload.pagePathsToClear) {
97+
for (const component of state.trackedComponents.values()) {
98+
component.pages.delete(queryId)
99+
}
100+
state = clearNodeDependencies(state, queryId)
101+
state = clearConnectionDependencies(state, queryId)
102+
state.trackedQueries.delete(queryId)
103+
}
104+
105+
return state
106+
}
93107
case `API_FINISHED`: {
94108
if (action.payload.apiName !== `createPages`) {
95109
return state

packages/gatsby/src/redux/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ export type ActionsUnion =
352352
| IDisableTypeInferenceAction
353353
| ISetProgramAction
354354
| ISetProgramExtensions
355+
| IDeletedStalePageDataFiles
355356

356357
export interface IApiFinishedAction {
357358
type: `API_FINISHED`
@@ -804,3 +805,10 @@ interface ISetProgramExtensions {
804805
type: `SET_PROGRAM_EXTENSIONS`
805806
payload: Array<string>
806807
}
808+
809+
interface IDeletedStalePageDataFiles {
810+
type: `DELETED_STALE_PAGE_DATA_FILES`
811+
payload: {
812+
pagePathsToClear: Set<string>
813+
}
814+
}

packages/gatsby/src/utils/page-data.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,20 @@ export async function handleStalePageData(): Promise<void> {
256256
})
257257

258258
const deletionPromises: Array<Promise<void>> = []
259-
pageDataFilesFromPreviousBuilds.forEach(pageDataFilePath => {
259+
const pagePathsToClear = new Set<string>()
260+
for (const pageDataFilePath of pageDataFilesFromPreviousBuilds) {
260261
if (!expectedPageDataFiles.has(pageDataFilePath)) {
262+
const stalePageDataContent = await fs.readJson(pageDataFilePath)
263+
pagePathsToClear.add(stalePageDataContent.path)
261264
deletionPromises.push(fs.remove(pageDataFilePath))
262265
}
266+
}
267+
268+
store.dispatch({
269+
type: `DELETED_STALE_PAGE_DATA_FILES`,
270+
payload: {
271+
pagePathsToClear,
272+
},
263273
})
264274

265275
await Promise.all(deletionPromises)

0 commit comments

Comments
 (0)