Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Add option to prefix sitemap" #10179

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/slow-cats-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@astrojs/sitemap": patch
---

Revert https://github.com/withastro/astro/pull/9846

The feature to customize the file name of the sitemap was reverted due to some internal issues with one of the dependencies. With an non-deterministic behaviour, the sitemap file was sometime emitted with incorrect syntax.
31 changes: 9 additions & 22 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { AstroConfig, AstroIntegration } from 'astro';
import path, { resolve } from 'node:path';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { EnumChangefreq, LinkItem as LinkItemBase, SitemapItemLoose } from 'sitemap';
import { SitemapAndIndexStream, SitemapStream, streamToPromise } from 'sitemap';
import { simpleSitemapAndIndex } from 'sitemap';
import { ZodError } from 'zod';

import { generateSitemap } from './generate-sitemap.js';
import { validateOptions } from './validate-options.js';
import { createWriteStream } from 'node:fs';
import { Readable } from 'node:stream';

export { EnumChangefreq as ChangeFreqEnum } from 'sitemap';
export type ChangeFreq = `${EnumChangefreq}`;
Expand All @@ -35,8 +33,6 @@ export type SitemapOptions =
lastmod?: Date;
priority?: number;

prefix?: string;

// called for each sitemap item just before to save them on disk, sync or async
serialize?(item: SitemapItem): SitemapItem | Promise<SitemapItem | undefined> | undefined;
}
Expand All @@ -48,6 +44,7 @@ function formatConfigErrorMessage(err: ZodError) {
}

const PKG_NAME = '@astrojs/sitemap';
const OUTFILE = 'sitemap-index.xml';
const STATUS_CODE_PAGES = new Set(['404', '500']);

function isStatusCodePage(pathname: string): boolean {
Expand Down Expand Up @@ -80,8 +77,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {

const opts = validateOptions(config.site, options);

const { filter, customPages, serialize, entryLimit, prefix = 'sitemap-' } = opts;
const OUTFILE = `${prefix}index.xml`;
const { filter, customPages, serialize, entryLimit } = opts;

let finalSiteUrl: URL;
if (config.site) {
Expand Down Expand Up @@ -171,22 +167,13 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
}
}
const destDir = fileURLToPath(dir);

const sms = new SitemapAndIndexStream({
await simpleSitemapAndIndex({
hostname: finalSiteUrl.href,
destinationDir: destDir,
sourceData: urlData,
limit: entryLimit,
getSitemapStream: (i) => {
const sitemapStream = new SitemapStream({ hostname: finalSiteUrl.href });
const fileName = `${prefix}${i}.xml`;

const ws = sitemapStream.pipe(createWriteStream(resolve(destDir + fileName)));

return [new URL(fileName, finalSiteUrl.href).toString(), sitemapStream, ws];
},
gzip: false,
});

sms.pipe(createWriteStream(resolve(destDir + OUTFILE)));
await streamToPromise(Readable.from(urlData).pipe(sms));
sms.end();
logger.info(`\`${OUTFILE}\` created at \`${path.relative(process.cwd(), destDir)}\``);
} catch (err) {
if (err instanceof ZodError) {
Expand Down
7 changes: 0 additions & 7 deletions packages/integrations/sitemap/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ export const SitemapOptionsSchema = z
changefreq: z.nativeEnum(ChangeFreq).optional(),
lastmod: z.date().optional(),
priority: z.number().min(0).max(1).optional(),

prefix: z
.string()
.regex(/^[a-zA-Z\-_]+$/gm, {
message: 'Only English alphabet symbols, hyphen and underscore allowed',
})
.optional(),
})
.strict()
.default(SITEMAP_CONFIG_DEFAULTS);
Loading