Skip to content

Commit c07336e

Browse files
committed
fix(fs-watcher): add timeout to deal with race-condition
1 parent b774751 commit c07336e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/utils/dev.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ export function watchContents(nuxt: Nuxt, options: ModuleOptions, manifest: Mani
134134
const keyInCollection = join(collection.name, source?.prefix || '', filePath)
135135
const fullPath = join(cwd, path)
136136

137-
const content = await readFile(fullPath, 'utf8')
137+
let content = await readFile(fullPath, 'utf8')
138+
if (content === '') {
139+
// If users edit the file very quickly, in some race-condition, the file content might be read as empty.
140+
// To deal with this scenario, we wait for 50ms if the file is empty and try again.
141+
content = await new Promise<string>(resolve => setTimeout(resolve, 50))
142+
.then(() => readFile(fullPath, 'utf8'))
143+
}
138144
const checksum = getContentChecksum(content)
139145
const localCache = await db.fetchDevelopmentCacheForKey(keyInCollection)
140146

0 commit comments

Comments
 (0)