Skip to content

Commit

Permalink
fix(gatsby): notify when Gatsby cache is incomplete (#27549)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar authored Oct 19, 2020
1 parent ecc0ad3 commit 2d6a153
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const {
copy,
mkdirp,
move,
readdir,
readFile,
remove,
Expand Down Expand Up @@ -41,7 +40,7 @@ describe(`long term caching`, () => {

const createPublic0 = async () => {
execFileSync(`yarn`, [`build`], { cwd: basePath })
return move(`${basePath}/public`, `${basePath}/public-0`)
return copy(`${basePath}/public`, `${basePath}/public-0`)
}

const createPublic1 = async () => {
Expand All @@ -53,7 +52,7 @@ describe(`long term caching`, () => {
await writeFile(`${pagesPath}/index.js`, modifiedData)

execFileSync(`yarn`, [`build`], { cwd: basePath })
return move(`${basePath}/public`, `${basePath}/public-1`)
return copy(`${basePath}/public`, `${basePath}/public-1`)
}

const createPublic2 = async () => {
Expand All @@ -66,7 +65,7 @@ describe(`long term caching`, () => {
await writeFile(`${pagesPath}/index.js`, modifiedData)

execFileSync(`yarn`, [`build`], { cwd: basePath })
return move(`${basePath}/public`, `${basePath}/public-2`)
return copy(`${basePath}/public`, `${basePath}/public-2`)
}

const createPublic3 = async () => {
Expand All @@ -79,7 +78,7 @@ describe(`long term caching`, () => {
await writeFile(`${pagesPath}/index.js`, modifiedData)

execFileSync(`yarn`, [`build`], { cwd: basePath })
return move(`${basePath}/public`, `${basePath}/public-3`)
return copy(`${basePath}/public`, `${basePath}/public-3`)
}

const createPublic4 = async () => {
Expand All @@ -93,7 +92,7 @@ describe(`long term caching`, () => {
await writeFile(`${pagesPath}/index.js`, modifiedData)

execFileSync(`yarn`, [`build`], { cwd: basePath })
return move(`${basePath}/public`, `${basePath}/public-4`)
return copy(`${basePath}/public`, `${basePath}/public-4`)
}

const createPublic5 = async () => {
Expand All @@ -105,7 +104,7 @@ describe(`long term caching`, () => {
await writeFile(`${srcPath}/async-2.js`, modifiedData)

execFileSync(`yarn`, [`build`], { cwd: basePath })
return move(`${basePath}/public`, `${basePath}/public-5`)
return copy(`${basePath}/public`, `${basePath}/public-5`)
}

beforeAll(async () => {
Expand Down
16 changes: 14 additions & 2 deletions packages/gatsby/src/services/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,19 @@ export async function initialize({
`)
}
const cacheDirectory = `${program.directory}/.cache`
if (!oldPluginsHash || pluginsHash !== oldPluginsHash) {
const publicDirectory = `${program.directory}/public`
const cacheIsCorrupt =
fs.existsSync(cacheDirectory) && !fs.existsSync(publicDirectory)

if (cacheIsCorrupt) {
reporter.info(reporter.stripIndent`
We've detected that the Gatsby cache is incomplete (the .cache directory exists
but the public directory does not). As a precaution, we're deleting your site's
cache to ensure there's no stale data.
`)
}

if (!oldPluginsHash || pluginsHash !== oldPluginsHash || cacheIsCorrupt) {
try {
// Attempt to empty dir if remove fails,
// like when directory is mount point
Expand Down Expand Up @@ -286,7 +298,7 @@ export async function initialize({
await fs.ensureDir(cacheDirectory)

// Ensure the public/static directory
await fs.ensureDir(`${program.directory}/public/static`)
await fs.ensureDir(`${publicDirectory}/static`)

activity.end()

Expand Down

0 comments on commit 2d6a153

Please sign in to comment.