Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): Check for files before delete action when using GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES #23219

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/gatsby/src/commands/build-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
import { remove as removePageDataFile, fixedPagePath } from "../utils/page-data"
import { IGatsbyState } from "../redux/types"

const checkFolderIsEmpty = (path: string): boolean =>
fs.existsSync(path) && !fs.readdirSync(path).length

export const getChangedPageDataKeys = (
state: IGatsbyState,
cachedPageData: Map<string, string>
Expand Down Expand Up @@ -55,11 +58,11 @@ const checkAndRemoveEmptyDir = (publicDir: string, pagePath: string): void => {
`page-data`,
fixedPagePath(pagePath)
)
const hasFiles = fs.readdirSync(pageHtmlDirectory)

// if page's html folder is empty also remove matching page-data folder
if (!hasFiles.length) {
// if page's folder is empty also remove matching page-data folder
if (checkFolderIsEmpty(pageHtmlDirectory)) {
fs.removeSync(pageHtmlDirectory)
}
if (checkFolderIsEmpty(pageDataDirectory)) {
fs.removeSync(pageDataDirectory)
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby/src/utils/page-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const read = async ({ publicDir }, pagePath) => {

const remove = async ({ publicDir }, pagePath) => {
const filePath = getFilePath({ publicDir }, pagePath)
return fs.remove(filePath)
if (fs.existsSync(filePath)) {
return await fs.remove(filePath)
}
return Promise.resolve()
}

const write = async ({ publicDir }, page, result) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby/src/utils/page-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const remove = async (
pagePath: string
): Promise<void> => {
const filePath = getPageHtmlFilePath(publicDir, pagePath)

return await fs.remove(filePath)
if (fs.existsSync(filePath)) {
return await fs.remove(filePath)
}
return Promise.resolve()
}