Files
myeasycms-v2/apps/web/app/server-sitemap.xml/route.ts
giancarlo f94557e333 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.
2024-04-10 16:29:19 +08:00

56 lines
1.3 KiB
TypeScript

import { invariant } from '@epic-web/invariant';
import { getServerSideSitemap } from 'next-sitemap';
import { createCmsClient } from '@kit/cms';
import appConfig from '~/config/app.config';
invariant(appConfig.url, 'No NEXT_PUBLIC_SITE_URL environment variable found');
export async function GET() {
const urls = getSiteUrls();
const items = await getAllItems();
return getServerSideSitemap([
...urls,
...items.map((path) => {
return {
loc: new URL(path, appConfig.url).href,
lastmod: new Date().toISOString(),
};
}),
]);
}
function getSiteUrls() {
const urls = ['/', 'faq', 'pricing'];
return urls.map((url) => {
return {
loc: new URL(url, appConfig.url).href,
lastmod: new Date().toISOString(),
};
});
}
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());
}