Home / File/ write-sitemap.ts — astro Source File

write-sitemap.ts — astro Source File

Architecture documentation for write-sitemap.ts, a typescript file in the astro codebase. 9 imports, 0 dependents.

File typescript CoreAstro RoutingSystem 9 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd["write-sitemap.ts"]
  3adbf330_e1f3_6c24_255f_e8c25442ac9f["./index.js"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> 3adbf330_e1f3_6c24_255f_e8c25442ac9f
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  8f34f3e8_2f0f_1f0d_91dc_c25ece8c8169["node:stream"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> 8f34f3e8_2f0f_1f0d_91dc_c25ece8c8169
  b4a76fc8_3591_85b4_7b57_55ab21d1030d["node:util"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> b4a76fc8_3591_85b4_7b57_55ab21d1030d
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> f16d8c76_2866_6150_bd14_0347b59abfe9
  4b2a278b_7519_80c4_73d6_387d11edd840["sitemap"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> 4b2a278b_7519_80c4_73d6_387d11edd840
  d737c47c_6343_2a32_1a89_76870c6eadf4["stream-replace-string"]
  b5bc5d6f_1305_f171_dbec_6c8fd4472fcd --> d737c47c_6343_2a32_1a89_76870c6eadf4
  style b5bc5d6f_1305_f171_dbec_6c8fd4472fcd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { createWriteStream, type WriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { normalize, resolve } from 'node:path';
import { pipeline, Readable } from 'node:stream';
import { promisify } from 'node:util';
import type { AstroConfig } from 'astro';
import { SitemapAndIndexStream, SitemapIndexStream, SitemapStream } from 'sitemap';
import replace from 'stream-replace-string';
import type { SitemapItem } from './index.js';

type WriteSitemapConfig = {
	filenameBase: string;
	hostname: string;
	sitemapHostname?: string;
	customSitemaps?: string[];
	sourceData: SitemapItem[];
	destinationDir: string;
	publicBasePath?: string;
	limit?: number;
	xslURL?: string;
	lastmod?: string;
	namespaces?: {
		news?: boolean;
		xhtml?: boolean;
		image?: boolean;
		video?: boolean;
	};
};

// adapted from sitemap.js/sitemap-simple
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

Functions

Dependencies

  • ./index.js
  • astro
  • node:fs
  • node:path
  • node:stream
  • node:util
  • promises
  • sitemap
  • stream-replace-string

Frequently Asked Questions

What does write-sitemap.ts do?
write-sitemap.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in write-sitemap.ts?
write-sitemap.ts defines 1 function(s): writeSitemap.
What does write-sitemap.ts depend on?
write-sitemap.ts imports 9 module(s): ./index.js, astro, node:fs, node:path, node:stream, node:util, promises, sitemap, and 1 more.
Where is write-sitemap.ts in the architecture?
write-sitemap.ts is located at packages/integrations/sitemap/src/write-sitemap.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/sitemap/src).

Analyze Your Own Codebase

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

Try Supermodel Free