Performance: Sitemap generation
Sitemap generation affects the initial page load speed indirectly by influencing SEO crawling efficiency and server response time during sitemap requests.
import { SitemapStream, streamToPromise } from 'sitemap'; import { Readable } from 'stream'; const pages = ['/', '/about', '/contact']; let sitemapCache = null; export async function GET() { if (!sitemapCache) { const stream = new SitemapStream({ hostname: 'https://example.com' }); const xml = await streamToPromise(Readable.from(pages).pipe(stream)); sitemapCache = xml.toString(); } return new Response(sitemapCache, { headers: { 'Content-Type': 'application/xml' } }); }
import fs from 'fs'; export async function GET() { const pages = fs.readdirSync('./src/pages'); const urls = pages.map(page => `<url><loc>https://example.com/${page.replace('.astro', '')}</loc></url>`).join(''); return new Response(`<?xml version="1.0" encoding="UTF-8"?><urlset>${urls}</urlset>`, { headers: { 'Content-Type': 'application/xml' } }); }
| Pattern | Server Load | Response Time | Caching | Verdict |
|---|---|---|---|---|
| Synchronous filesystem read per request | High | Slow (50-200ms) | No | [X] Bad |
| Cached sitemap generated once | Low | Fast (<10ms) | Yes | [OK] Good |
export const get = () => {
return Sitemap({
filter: (page) => !page.includes('/drafts/'),
changefreq: 'weekly'
});
};filter function?export const get = () => {
return Sitemap({
changefreq: 'daily',
filter: page => page.startsWith('/blog')
});
};changefreq to 'monthly'. Which code snippet correctly achieves this?!!page converts page to boolean, excluding falsy values like empty strings.Pass the test and this pattern moves unknown