0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

Revert "Add option to prefix sitemap" (#10179)

* Revert "Add option to prefix sitemap (#9846)"

This reverts commit 9b78c99275.

* changeset

* feedabck

* fix incorrect merging
This commit is contained in:
Emanuele Stoppa 2024-02-21 14:27:00 +00:00 committed by GitHub
parent 92b00ac97e
commit 6343f6a438
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 101 deletions

View file

@ -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.

View file

@ -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}`;
@ -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;
}
@ -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 {
@ -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) {
@ -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) {

View file

@ -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);

View file

@ -1,72 +0,0 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { sitemap } from './fixtures/static/deps.mjs';
import { loadFixture, readXML } from './test-utils.js';
describe('Prefix support', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
const prefix = 'test-';
describe('Static', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
integrations: [
sitemap(),
sitemap({
prefix,
}),
],
});
await fixture.build();
});
it('Content is same', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const prefixData = await readXML(fixture.readFile(`/${prefix}0.xml`));
assert.deepEqual(prefixData, data);
});
it('Index file load correct sitemap', async () => {
const data = await readXML(fixture.readFile('/sitemap-index.xml'));
const sitemapUrl = data.sitemapindex.sitemap[0].loc[0];
assert.strictEqual(sitemapUrl, 'http://example.com/sitemap-0.xml');
const prefixData = await readXML(fixture.readFile(`/${prefix}index.xml`));
const prefixSitemapUrl = prefixData.sitemapindex.sitemap[0].loc[0];
assert.strictEqual(prefixSitemapUrl, `http://example.com/${prefix}0.xml`);
});
});
describe('SSR', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr/',
integrations: [
sitemap(),
sitemap({
prefix,
}),
],
});
await fixture.build();
});
it('Content is same', async () => {
const data = await readXML(fixture.readFile('/client/sitemap-0.xml'));
const prefixData = await readXML(fixture.readFile(`/client/${prefix}0.xml`));
assert.deepEqual(prefixData, data);
});
it('Index file load correct sitemap', async () => {
const data = await readXML(fixture.readFile('/client/sitemap-index.xml'));
const sitemapUrl = data.sitemapindex.sitemap[0].loc[0];
assert.strictEqual(sitemapUrl, 'http://example.com/sitemap-0.xml');
const prefixData = await readXML(fixture.readFile(`/client/${prefix}index.xml`));
const prefixSitemapUrl = prefixData.sitemapindex.sitemap[0].loc[0];
assert.strictEqual(prefixSitemapUrl, `http://example.com/${prefix}0.xml`);
});
});
});