Files
myeasycms-v2/apps/web/app/server-sitemap.xml/route.ts
giancarlo 69942ec243 Refactor language switcher and enhance site routing
Renamed 'LanguageDropdownSwitcher' to 'LanguageSelector' for better representation of the component's functionality. Removed unnecessary dependencies and optimized function declarations. Updated site routing to include new pages like 'contact', 'terms-of-service', and 'privacy-policy'. Also made adjustments for multi-language support, providing better user experience.
2024-04-10 21:23:41 +08:00

63 lines
1.4 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',
'/contact',
'/terms-of-service',
'/privacy-policy',
];
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());
}