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

perf(content-list): cache contents list during generation and per-request #2527

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Changes from 1 commit
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
45 changes: 36 additions & 9 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const cacheStorage: Storage = prefixStorage(useStorage(), 'cache:content'
export const cacheParsedStorage: Storage = prefixStorage(useStorage(), 'cache:content:parsed')

const isProduction = process.env.NODE_ENV === 'production'
const isPrerendering = process.argv?.includes('generate')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use import.meta.prerender


const contentConfig = useRuntimeConfig().content

Expand Down Expand Up @@ -109,19 +110,45 @@ export function* chunksFromArray<T> (arr: T[], n: number) : Generator<T[], void>
}
}

export const getContentsList = async (event: H3Event, prefix?: string) => {
const keys = await getContentsIds(event, prefix)
export const getContentsList = (() => {
let cachedContents: ParsedContent[] = []
let pendingContentsListPromise: Promise<ParsedContent[]> | null = null

const keyChunks = [...chunksFromArray(keys, 10)]
const _getContentsList = async (event: H3Event, prefix?: string) => {
const keys = await getContentsIds(event, prefix)

const contents: ParsedContent[] = []
for (const chunk of keyChunks) {
const result = await Promise.all(chunk.map(key => getContent(event, key)))
contents.push(...result)
const keyChunks = [...chunksFromArray(keys, 10)]

const contents: ParsedContent[] = []
for (const chunk of keyChunks) {
const result = await Promise.all(chunk.map(key => getContent(event, key)))
contents.push(...result)
}
return contents
}

return contents
}
return (event: H3Event, prefix?: string) => {
if (event.context.__contentList) {
return event.context.__contentList
}
if (isPrerendering && cachedContents.length) {
return cachedContents
}

if (!pendingContentsListPromise) {
pendingContentsListPromise = _getContentsList(event, prefix)
pendingContentsListPromise.then((result) => {
if (isPrerendering) {
cachedContents = result as ParsedContent[]
}
event.context.__contentList = result as ParsedContent[]
pendingContentsListPromise = null
})
}
return pendingContentsListPromise
}
})()

const pendingPromises: Record<string, Promise<ParsedContent>> = {}
export const getContent = async (event: H3Event, id: string): Promise<ParsedContent> => {
const contentId = id
Expand Down