mirror of
https://github.com/KevinMidboe/planetposen-frontend.git
synced 2025-12-29 04:51:00 +00:00
All planetposen routes at version 0.1
This commit is contained in:
87
src/routes/sitemap.xml/+server.ts
Normal file
87
src/routes/sitemap.xml/+server.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { dev } from '$app/environment';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import type { IProductResponse } from '$lib/interfaces/ApiResponse';
|
||||
|
||||
const domain = 'planet.schleppe.cloud';
|
||||
const pages: Array<ISitemapPage> = [
|
||||
{
|
||||
name: 'home',
|
||||
modified: '2022-11-16T14:00:00.000Z'
|
||||
},
|
||||
{
|
||||
name: 'shop',
|
||||
modified: '2022-11-16T14:00:00.000Z'
|
||||
},
|
||||
{
|
||||
name: 'privacy-policy',
|
||||
modified: '2022-11-16T14:00:00.000Z'
|
||||
},
|
||||
{
|
||||
name: 'cookies',
|
||||
modified: '2022-11-16T14:00:00.000Z'
|
||||
},
|
||||
{
|
||||
name: 'terms-and-condition',
|
||||
modified: '2022-11-16T14:00:00.000Z'
|
||||
}
|
||||
];
|
||||
|
||||
interface ISitemapPage {
|
||||
name: string;
|
||||
modified: string;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const body = await buildSitemap();
|
||||
const headers = {
|
||||
'Content-Type': 'application/xml',
|
||||
'Cache-Control': `max-age=0, s-max-age=${3600}`
|
||||
};
|
||||
|
||||
return new Response(body, { headers });
|
||||
}
|
||||
|
||||
function buildSitemapUrl(address: string, modified: string, frequency: string) {
|
||||
return `<url>
|
||||
<loc>https://${address}</loc>
|
||||
<lastmod>${modified}</lastmod>
|
||||
<changefreq>${frequency}</changefreq>
|
||||
</url>`;
|
||||
}
|
||||
|
||||
function sitemapPages(): string {
|
||||
return pages
|
||||
.map((page) => buildSitemapUrl(`https://${domain}/${page.name}`, page.modified, 'yearly'))
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
async function sitemapShopPages(): Promise<string> {
|
||||
let url = `/api/products`;
|
||||
if (dev || env.API_HOST) {
|
||||
url = (env.API_HOST || 'http://localhost:30010').concat(url);
|
||||
}
|
||||
|
||||
const res = await fetch(url);
|
||||
const products: IProductResponse = await res.json();
|
||||
|
||||
return products?.products
|
||||
?.map((product) =>
|
||||
buildSitemapUrl(
|
||||
`https://${domain}/shop/${product.product_no}`,
|
||||
String(product.updated),
|
||||
'daily'
|
||||
)
|
||||
)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
async function buildSitemap(): Promise<string> {
|
||||
const generalPages = sitemapPages();
|
||||
const shopPages = await sitemapShopPages();
|
||||
|
||||
return `<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
${generalPages}
|
||||
${shopPages}
|
||||
</urlset>`.trim();
|
||||
}
|
||||
Reference in New Issue
Block a user