Home / Function/ writeSitemap() — astro Function Reference

writeSitemap() — astro Function Reference

Architecture documentation for the writeSitemap() function in write-sitemap.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  270ede8a_ec1a_ff73_bac6_9cb931811c6f["writeSitemap()"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd["write-sitemap.ts"]
  270ede8a_ec1a_ff73_bac6_9cb931811c6f -->|defined in| b5bc5d6f_1305_f171_dbec_6c8fd4472fcd
  style 270ede8a_ec1a_ff73_bac6_9cb931811c6f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/sitemap/src/write-sitemap.ts lines 31–100

export async function writeSitemap(
	{
		filenameBase,
		hostname,
		sitemapHostname = hostname,
		sourceData,
		destinationDir,
		limit = 50000,
		customSitemaps = [],
		publicBasePath = './',
		xslURL: xslUrl,
		lastmod,
		namespaces = { news: true, xhtml: true, image: true, video: true },
	}: WriteSitemapConfig,
	astroConfig: AstroConfig,
) {
	await mkdir(destinationDir, { recursive: true });

	const sitemapAndIndexStream = new SitemapAndIndexStream({
		limit,
		xslUrl,
		getSitemapStream: (i) => {
			const sitemapStream = new SitemapStream({
				hostname,
				xslUrl,
				// Custom namespace handling
				xmlns: {
					news: namespaces?.news !== false,
					xhtml: namespaces?.xhtml !== false,
					image: namespaces?.image !== false,
					video: namespaces?.video !== false,
				},
			});
			const path = `./${filenameBase}-${i}.xml`;
			const writePath = resolve(destinationDir, path);
			if (!publicBasePath.endsWith('/')) {
				publicBasePath += '/';
			}
			const publicPath = normalize(publicBasePath + path);

			let stream: WriteStream;
			if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') {
				// workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403
				const host = hostname.endsWith('/') ? hostname.slice(0, -1) : hostname;
				const searchStr = `<loc>${host}/</loc>`;
				const replaceStr = `<loc>${host}</loc>`;
				stream = sitemapStream
					.pipe(replace(searchStr, replaceStr))
					.pipe(createWriteStream(writePath));
			} else {
				stream = sitemapStream.pipe(createWriteStream(writePath));
			}

			const url = new URL(publicPath, sitemapHostname).toString();
			return [{ url, lastmod }, sitemapStream, stream];
		},
	});

	const src = Readable.from(sourceData);
	const indexPath = resolve(destinationDir, `./${filenameBase}-index.xml`);
	for (const url of customSitemaps) {
		SitemapIndexStream.prototype._transform.call(
			sitemapAndIndexStream,
			{ url, lastmod },
			'utf8',
			() => {},
		);
	}
	return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath));
}

Domain

Subdomains

Frequently Asked Questions

What does writeSitemap() do?
writeSitemap() is a function in the astro codebase, defined in packages/integrations/sitemap/src/write-sitemap.ts.
Where is writeSitemap() defined?
writeSitemap() is defined in packages/integrations/sitemap/src/write-sitemap.ts at line 31.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free