Skip to content

Commit

Permalink
fix: prevent duplicate parses
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 27, 2023
1 parent 061192c commit a208567
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/runtime/server/storage.ts
Expand Up @@ -160,24 +160,26 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
return cached.parsed as ParsedContent
}

// eslint-disable-next-line no-async-promise-executor
const promise = pendingPromises[hash] || new Promise(async (resolve) => {
const body = await sourceStorage.getItem(id)
if (!pendingPromises[hash]) {
// eslint-disable-next-line no-async-promise-executor
pendingPromises[hash] = new Promise(async (resolve) => {
const body = await sourceStorage.getItem(id)

if (body === null) {
return resolve({ _id: contentId, body: null } as unknown as ParsedContent)
}
if (body === null) {
return resolve({ _id: contentId, body: null } as unknown as ParsedContent)
}

const parsed = await parseContent(contentId, body) as ParsedContent
const parsed = await parseContent(contentId, body) as ParsedContent

await cacheParsedStorage.setItem(id, { parsed, hash }).catch(() => {})
await cacheParsedStorage.setItem(id, { parsed, hash }).catch(() => {})

resolve(parsed)
resolve(parsed)

delete pendingPromises[hash]
})
delete pendingPromises[hash]
})
}

return promise
return pendingPromises[hash]
}

/**
Expand Down

0 comments on commit a208567

Please sign in to comment.