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(storage): prevent duplicate parsing #2326

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const getContentsList = async (event: H3Event, prefix?: string) => {

return contents
}

const pendingPromises: Record<string, Promise<ParsedContent>> = {}
export const getContent = async (event: H3Event, id: string): Promise<ParsedContent> => {
const contentId = id
// Handle ignored id
Expand Down Expand Up @@ -160,17 +160,24 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
return cached.parsed as ParsedContent
}

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

if (body === null) {
return { _id: contentId, body: null }
}
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)

delete pendingPromises[hash]
})

return parsed
return promise
}

/**
Expand Down