Skip to content

Commit

Permalink
fix: clear tracked queries when deleting stale page-data files (#29431)
Browse files Browse the repository at this point in the history
* fix: clear tracked queries when deleting stale page-data files

* add comment about hot-fix nature of added action
  • Loading branch information
pieh committed Feb 10, 2021
1 parent e567aa8 commit 478cf68
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/gatsby/src/redux/reducers/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export type ActionsUnion =
| IDisableTypeInferenceAction
| ISetProgramAction
| ISetProgramExtensions
| IDeletedStalePageDataFiles

export interface IApiFinishedAction {
type: `API_FINISHED`
Expand Down Expand Up @@ -804,3 +805,10 @@ interface ISetProgramExtensions {
type: `SET_PROGRAM_EXTENSIONS`
payload: Array<string>
}

interface IDeletedStalePageDataFiles {
type: `DELETED_STALE_PAGE_DATA_FILES`
payload: {
pagePathsToClear: Set<string>
}
}
12 changes: 11 additions & 1 deletion packages/gatsby/src/utils/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,20 @@ export async function handleStalePageData(): Promise<void> {
})

const deletionPromises: Array<Promise<void>> = []
pageDataFilesFromPreviousBuilds.forEach(pageDataFilePath => {
const pagePathsToClear = new Set<string>()
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)
Expand Down

0 comments on commit 478cf68

Please sign in to comment.