Home / Function/ writeSitemapChunk() — astro Function Reference

writeSitemapChunk() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  6092065c_60d0_4c34_eb98_9ad6f0d51af9["writeSitemapChunk()"]
  d93872ee_352c_b6ca_8205_2268f8108f4e["write-sitemap-chunk.ts"]
  6092065c_60d0_4c34_eb98_9ad6f0d51af9 -->|defined in| d93872ee_352c_b6ca_8205_2268f8108f4e
  style 6092065c_60d0_4c34_eb98_9ad6f0d51af9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

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

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

	// Normalize publicBasePath
	let normalizedPublicBasePath = publicBasePath;
	if (!normalizedPublicBasePath.endsWith('/')) {
		normalizedPublicBasePath += '/';
	}

	// Array to collect all sitemap URLs for the index
	const sitemapUrls: Array<{ url: string; lastmod?: string }> = [];

	// Process each chunk separately
	for (const [chunkName, items] of Object.entries(sourceData)) {
		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}-${chunkName}-${i}.xml`;
				const writePath = resolve(destinationDir, path);
				const publicPath = normalize(normalizedPublicBasePath + path);

				let stream: WriteStream;
				if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') {
					// workaround for trailing slash issue in sitemap.js
					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();

				// Collect this sitemap URL for the index
				sitemapUrls.push({ url, lastmod });

				return [{ url, lastmod }, sitemapStream, stream];
			},
		});

		// Create a readable stream from this chunk's items
		const dataStream = Readable.from(items);

		// Write this chunk's sitemap(s)
		await promisify(pipeline)(dataStream, sitemapAndIndexStream);
	}

	// Now create the sitemap index with all the generated sitemaps
	const indexStream = new SitemapIndexStream({ xslUrl });
	const indexPath = resolve(destinationDir, `./${filenameBase}-index.xml`);

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free