Skip to content

Commit

Permalink
fix(gatsby): reset jobs cache when its outdated (#33617) (#33660)
Browse files Browse the repository at this point in the history
Co-authored-by: Ward Peeters <ward@coding-tech.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
  • Loading branch information
3 people authored Oct 25, 2021
1 parent 8c99000 commit 42358ee
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/gatsby/src/redux/reducers/jobsv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ export const jobsV2Reducer = (
| IDeleteCacheAction
): IGatsbyState["jobsV2"] => {
switch (action.type) {
case `DELETE_CACHE`:
return (action as IDeleteCacheAction).cacheIsCorrupt
? initialState()
: state
case `DELETE_CACHE`: {
// Wipe the cache if state shape doesn't match the initial shape
// It is possible when the old cache is loaded for the new version of this reducer
const cleanState = initialState()
const cleanStateKeys = Object.keys(cleanState)

const isOutdatedJobsState =
cleanStateKeys.length !== Object.keys(state).length ||
cleanStateKeys.some(
key => !Object.prototype.hasOwnProperty.call(state, key)
)

return action.cacheIsCorrupt || isOutdatedJobsState ? cleanState : state
}

case `CREATE_JOB_V2`: {
const { job } = action.payload
Expand Down Expand Up @@ -78,12 +88,6 @@ export const jobsV2Reducer = (
case `SET_JOB_V2_CONTEXT`: {
const { requestId, job } = action.payload

// A workaround for a stale cache bug:
// in some edge case redux cache is not cleared (even after gatsby-config and package.json changes).
// TODO: figure out the root cause and remove this workaround (see also CLEAR_JOB_V2_CONTEXT)
if (!state.jobsByRequest) {
state.jobsByRequest = new Map()
}
let jobs = state.jobsByRequest.get(requestId)
if (!jobs) {
jobs = new Set<string>()
Expand All @@ -96,9 +100,6 @@ export const jobsV2Reducer = (

case `CLEAR_JOB_V2_CONTEXT`: {
const { requestId } = action.payload
if (!state.jobsByRequest) {
state.jobsByRequest = new Map()
}
state.jobsByRequest.delete(requestId)
}
}
Expand Down

0 comments on commit 42358ee

Please sign in to comment.