Skip to content

Commit

Permalink
fix: load the tags manifest asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
imranolas committed Apr 21, 2024
1 parent f09650b commit e87f1b4
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,34 @@ export default class FileSystemCache implements CacheHandler {
'fetch-cache',
'tags-manifest.json'
)
this.loadTagsManifest()

this.loadTagsManifestSync()
}
}

public resetRequestCache(): void {}

private loadTagsManifest() {
/**
* Load the tags manifest from the file system
*/
private async loadTagsManifest() {
if (!this.tagsManifestPath || !this.fs || tagsManifest) return
try {
tagsManifest = JSON.parse(
await this.fs.readFile(this.tagsManifestPath, 'utf8')
)
} catch (err: any) {
tagsManifest = { version: 1, items: {} }
}
if (this.debug) console.log('loadTagsManifest', tagsManifest)
}

/**
* As above, but synchronous for use in the constructor. This is to
* preserve the existing behaviour when instantiating the cache handler. Although it's
* not ideal to block the main thread it's only called once during startup.
*/
private loadTagsManifestSync() {
if (!this.tagsManifestPath || !this.fs || tagsManifest) return
try {
tagsManifest = JSON.parse(
Expand All @@ -113,7 +134,7 @@ export default class FileSystemCache implements CacheHandler {
// we need to ensure the tagsManifest is refreshed
// since separate workers can be updating it at the same
// time and we can't flush out of sync data
this.loadTagsManifest()
await this.loadTagsManifest()
if (!tagsManifest || !this.tagsManifestPath) {
return
}
Expand Down Expand Up @@ -271,7 +292,7 @@ export default class FileSystemCache implements CacheHandler {
}

if (cacheTags?.length) {
this.loadTagsManifest()
await this.loadTagsManifest()

const isStale = cacheTags.some((tag) => {
return (
Expand All @@ -291,7 +312,7 @@ export default class FileSystemCache implements CacheHandler {
}

if (data && data?.value?.kind === 'FETCH') {
this.loadTagsManifest()
await this.loadTagsManifest()

const combinedTags = [...(tags || []), ...(softTags || [])]

Expand Down

0 comments on commit e87f1b4

Please sign in to comment.