Skip to content

Commit 5f276f2

Browse files
committed
fix(perf): add cache to directory empty check
1 parent b724ca7 commit 5f276f2

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

packages/workshop-utils/src/apps.server.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
playgroundAppCache,
1919
problemAppCache,
2020
solutionAppCache,
21+
directoryEmptyCache,
2122
} from './cache.server.js'
2223
import { compileMdx } from './compile-mdx.server.js'
2324
import { getAppConfig, getStackBlitzUrl } from './config.server.js'
@@ -222,19 +223,24 @@ function exists(file: string) {
222223
}
223224

224225
async function isDirectoryEmpty(dirPath: string): Promise<boolean> {
225-
try {
226-
const files = await fs.promises.readdir(dirPath)
227-
if (files.length === 0) return true
228-
229-
// Check if all files are gitignored
230-
const isIgnored = await isGitIgnored({ cwd: dirPath })
231-
const nonIgnoredFiles = files.filter((file) => !isIgnored(file))
232-
233-
return nonIgnoredFiles.length === 0
234-
} catch {
235-
// If we can't read the directory, consider it empty
236-
return true
237-
}
226+
return cachified({
227+
key: dirPath,
228+
cache: directoryEmptyCache,
229+
ttl: 1000 * 60 * 5,
230+
swr: 1000 * 60 * 20,
231+
forceFresh: getForceFreshForDir(directoryEmptyCache.get(dirPath), dirPath),
232+
getFreshValue: async () => {
233+
try {
234+
const files = await fs.promises.readdir(dirPath)
235+
if (files.length === 0) return true
236+
const isIgnored = await isGitIgnored({ cwd: dirPath })
237+
const nonIgnoredFiles = files.filter((file) => !isIgnored(file))
238+
return nonIgnoredFiles.length === 0
239+
} catch {
240+
return true
241+
}
242+
},
243+
})
238244
}
239245

240246
async function firstToExist(...files: Array<string>) {

packages/workshop-utils/src/cache.server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export const checkForUpdatesCache = makeSingletonCache<{
4848
}>('CheckForUpdatesCache')
4949
export const notificationsCache =
5050
makeSingletonCache<Array<Notification>>('NotificationsCache')
51+
export const directoryEmptyCache = makeSingletonCache<boolean>(
52+
'DirectoryEmptyCache',
53+
)
5154

5255
const cacheDir = path.join(os.homedir(), '.epicshop', 'cache')
5356

0 commit comments

Comments
 (0)