Skip to content

Commit

Permalink
fix(gatsby): replace checks for .cache/json with checks for .cache/ca…
Browse files Browse the repository at this point in the history
…ches-lmdb (#33431)
  • Loading branch information
raresportan authored Oct 7, 2021
1 parent 20eb2e0 commit 751cc43
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/gatsby/src/services/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export async function initialize({
// enable loading indicator
process.env.GATSBY_QUERY_ON_DEMAND_LOADING_INDICATOR = `true`
}
detectLmdbStore()
const lmdbStoreIsUsed = detectLmdbStore()

if (config && config.polyfill) {
reporter.warn(
Expand Down Expand Up @@ -209,20 +209,29 @@ export async function initialize({
await apiRunnerNode(`onPreInit`, { parentSpan: activity.span })
activity.end()

const lmdbCacheDirectoryName = `caches-lmdb`

const cacheDirectory = `${program.directory}/.cache`
const publicDirectory = `${program.directory}/public`
const workerCacheDirectory = `${program.directory}/.cache/worker`
const lmdbCacheDirectory = `${program.directory}/.cache/${lmdbCacheDirectoryName}`

const cacheJsonDirExists = fs.existsSync(`${cacheDirectory}/json`)
const publicDirExists = fs.existsSync(publicDirectory)
const workerCacheDirExists = fs.existsSync(workerCacheDirectory)
const lmdbCacheDirExists = fs.existsSync(lmdbCacheDirectory)

// check the cache file that is used by the current configuration
const cacheDirExists = lmdbStoreIsUsed
? lmdbCacheDirExists
: cacheJsonDirExists

// For builds in case public dir exists, but cache doesn't, we need to clean up potentially stale
// artifacts from previous builds (due to cache not being available, we can't rely on tracking of artifacts)
if (
process.env.NODE_ENV === `production` &&
publicDirExists &&
!cacheJsonDirExists
!cacheDirExists
) {
activity = reporter.activityTimer(
`delete html and css files from previous builds`,
Expand Down Expand Up @@ -297,9 +306,8 @@ export async function initialize({
}

// .cache directory exists in develop at this point
// so checking for .cache/json as a heuristic (could be any expected file)
const cacheIsCorrupt = cacheJsonDirExists && !publicDirExists

// so checking for .cache/json or .cache/caches-lmdb as a heuristic (could be any expected file)
const cacheIsCorrupt = cacheDirExists && !publicDirExists
if (cacheIsCorrupt) {
reporter.info(reporter.stripIndent`
We've detected that the Gatsby cache is incomplete (the .cache directory exists
Expand Down Expand Up @@ -448,7 +456,11 @@ export async function initialize({
try {
await fs.copy(srcDir, siteDir)
await fs.copy(tryRequire, `${siteDir}/test-require-error.js`)
await fs.ensureDirSync(`${cacheDirectory}/json`)
if (lmdbStoreIsUsed) {
await fs.ensureDirSync(`${cacheDirectory}/${lmdbCacheDirectoryName}`)
} else {
await fs.ensureDirSync(`${cacheDirectory}/json`)
}

// Ensure .cache/fragments exists and is empty. We want fragments to be
// added on every run in response to data as fragments can only be added if
Expand Down

0 comments on commit 751cc43

Please sign in to comment.