Update getContentItems to return total count and items

The changes involved modifying the implementation of `getContentItems` across multiple files, specifically in the CMS-related codebase. This method now returns an object containing the total count of items and the items themselves. The updates also included necessary adjustments in the code where `getContentItems` is called to accommodate the new structure of the returned result.
This commit is contained in:
giancarlo
2024-04-10 16:29:19 +08:00
parent 44373c0372
commit f94557e333
8 changed files with 89 additions and 71 deletions

View File

@@ -9,14 +9,14 @@ invariant(appConfig.url, 'No NEXT_PUBLIC_SITE_URL environment variable found');
export async function GET() {
const urls = getSiteUrls();
const client = await createCmsClient();
const contentItems = await client.getContentItems();
const items = await getAllItems();
return getServerSideSitemap([
...urls,
...contentItems.map((item) => {
...items.map((path) => {
return {
loc: new URL(item.url, appConfig.url).href,
loc: new URL(path, appConfig.url).href,
lastmod: new Date().toISOString(),
};
}),
@@ -33,3 +33,23 @@ function getSiteUrls() {
};
});
}
async function getAllItems() {
const client = await createCmsClient();
const posts = client
.getContentItems({
collection: 'posts',
})
.then((response) => response.items)
.then((posts) => posts.map((post) => `/blog/${post.slug}`));
const docs = client
.getContentItems({
collection: 'documentation',
})
.then((response) => response.items)
.then((docs) => docs.map((doc) => `/docs/${doc.slug}`));
return Promise.all([posts, docs]).then((items) => items.flat());
}