diff --git a/packages/gatsby/src/redux/__tests__/index.js b/packages/gatsby/src/redux/__tests__/index.js index b8327549acf1b..bc026633a0ded 100644 --- a/packages/gatsby/src/redux/__tests__/index.js +++ b/packages/gatsby/src/redux/__tests__/index.js @@ -149,4 +149,23 @@ describe(`redux db`, () => { expect(mockWrittenContent.has(legacyLocation)).toBe(false) }) + + describe(`GATSBY_DISABLE_CACHE_PERSISTENCE`, () => { + beforeAll(() => { + process.env.GATSBY_DISABLE_CACHE_PERSISTENCE = `truthy` + }) + + afterAll(() => { + delete process.env.GATSBY_DISABLE_CACHE_PERSISTENCE + }) + it(`shouldn't write redux cache to disk when GATSBY_DISABLE_CACHE_PERSISTENCE env var is used`, async () => { + expect(initialComponentsState).toEqual(new Map()) + + store.getState().nodes = getFakeNodes() + + await saveState() + + expect(writeToCache).not.toBeCalled() + }) + }) }) diff --git a/packages/gatsby/src/redux/index.ts b/packages/gatsby/src/redux/index.ts index 397f1996c4747..74e752948586f 100644 --- a/packages/gatsby/src/redux/index.ts +++ b/packages/gatsby/src/redux/index.ts @@ -86,6 +86,13 @@ export const store: GatsbyReduxStore = configureStore(readState()) // Persist state. export const saveState = (): void => { + if (process.env.GATSBY_DISABLE_CACHE_PERSISTENCE) { + // do not persist cache if above env var is set. + // this is to temporarily unblock builds that hit the v8.serialize related + // Node.js buffer size exceeding kMaxLength fatal crashes + return undefined + } + const state = store.getState() return writeToCache({ diff --git a/packages/gatsby/src/services/initialize.ts b/packages/gatsby/src/services/initialize.ts index 579657ccc7547..a00c15b0d16cf 100644 --- a/packages/gatsby/src/services/initialize.ts +++ b/packages/gatsby/src/services/initialize.ts @@ -74,6 +74,12 @@ export async function initialize({ store: Store workerPool: JestWorker }> { + if (process.env.GATSBY_DISABLE_CACHE_PERSISTENCE) { + reporter.info( + `GATSBY_DISABLE_CACHE_PERSISTENCE is enabled. Cache won't be persisted. Next builds will not be able to reuse any work done by current session.` + ) + telemetry.trackFeatureIsUsed(`DisableCachePersistence`) + } if (!args) { reporter.panic(`Missing program args`) }