2023-07-08 10:36:02 +02:00
|
|
|
import type { Nitro } from "nitropack";
|
2023-07-10 19:54:13 +02:00
|
|
|
//import { useRuntimeConfig } from "nuxt/app";
|
2023-07-08 10:36:02 +02:00
|
|
|
import { SitemapStream, streamToPromise, SitemapItem } from 'sitemap';
|
|
|
|
import { Readable } from 'stream';
|
|
|
|
import { writeFileSync } from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
export default async function genSitemap(nitro: Nitro) {
|
|
|
|
if (!nitro._prerenderedRoutes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-13 18:10:25 +02:00
|
|
|
const domain = nitro.options.runtimeConfig.public.baseUrl;
|
2023-07-08 10:36:02 +02:00
|
|
|
const publicDir = nitro.options.output.publicDir;
|
|
|
|
|
|
|
|
const routes = nitro._prerenderedRoutes?.map((e) => e.fileName || null).filter((e, i, a) => e && a.indexOf(e) === i && e.endsWith("index.html")).map((e) => {
|
|
|
|
return {
|
|
|
|
url: e?.replace(/index\.html$/, ''),
|
|
|
|
|
|
|
|
changefreq: 'weekly',
|
|
|
|
priority: .7,
|
|
|
|
} as SitemapItem;
|
|
|
|
});
|
|
|
|
const smStream = new SitemapStream({ hostname: domain });
|
|
|
|
Readable.from(routes).pipe(smStream);
|
|
|
|
|
|
|
|
const data = await streamToPromise(smStream);
|
|
|
|
|
|
|
|
writeFileSync(path.join(publicDir, 'sitemap.xml'), data.toString());
|
|
|
|
}
|