mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
feat: cache getCollection()
calls in production (#6503)
* feat: cache getCollection() in prod * chore: changeset
This commit is contained in:
parent
c44aa15534
commit
f6eddffa04
2 changed files with 34 additions and 20 deletions
5
.changeset/nasty-cougars-double.md
Normal file
5
.changeset/nasty-cougars-double.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add caching to `getCollection()` queries for faster SSG production builds
|
|
@ -38,6 +38,7 @@ export function createCollectionToGlobResultMap({
|
||||||
return collectionToGlobResultMap;
|
return collectionToGlobResultMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cacheEntriesByCollection = new Map<string, any[]>();
|
||||||
export function createGetCollection({
|
export function createGetCollection({
|
||||||
collectionToEntryMap,
|
collectionToEntryMap,
|
||||||
collectionToRenderEntryMap,
|
collectionToRenderEntryMap,
|
||||||
|
@ -47,27 +48,35 @@ export function createGetCollection({
|
||||||
}) {
|
}) {
|
||||||
return async function getCollection(collection: string, filter?: (entry: any) => unknown) {
|
return async function getCollection(collection: string, filter?: (entry: any) => unknown) {
|
||||||
const lazyImports = Object.values(collectionToEntryMap[collection] ?? {});
|
const lazyImports = Object.values(collectionToEntryMap[collection] ?? {});
|
||||||
const entries = Promise.all(
|
let entries: any[] = [];
|
||||||
lazyImports.map(async (lazyImport) => {
|
// Cache `getCollection()` calls in production only
|
||||||
const entry = await lazyImport();
|
// prevents stale cache in development
|
||||||
return {
|
if (import.meta.env.PROD && cacheEntriesByCollection.has(collection)) {
|
||||||
id: entry.id,
|
entries = cacheEntriesByCollection.get(collection)!;
|
||||||
slug: entry.slug,
|
} else {
|
||||||
body: entry.body,
|
entries = await Promise.all(
|
||||||
collection: entry.collection,
|
lazyImports.map(async (lazyImport) => {
|
||||||
data: entry.data,
|
const entry = await lazyImport();
|
||||||
async render() {
|
return {
|
||||||
return render({
|
id: entry.id,
|
||||||
collection: entry.collection,
|
slug: entry.slug,
|
||||||
id: entry.id,
|
body: entry.body,
|
||||||
collectionToRenderEntryMap,
|
collection: entry.collection,
|
||||||
});
|
data: entry.data,
|
||||||
},
|
async render() {
|
||||||
};
|
return render({
|
||||||
})
|
collection: entry.collection,
|
||||||
);
|
id: entry.id,
|
||||||
|
collectionToRenderEntryMap,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
cacheEntriesByCollection.set(collection, entries);
|
||||||
|
}
|
||||||
if (typeof filter === 'function') {
|
if (typeof filter === 'function') {
|
||||||
return (await entries).filter(filter);
|
return entries.filter(filter);
|
||||||
} else {
|
} else {
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue