-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to prefix sitemap (#9846)
* Add option to prefix sitemap * Fix call resolve twice * let to const * Apply suggestions from code review Co-authored-by: Emanuele Stoppa <[email protected]> * change changeset patch to minor * use node:test * Update changeset * Add regex validation for prefix * Update .changeset/eighty-falcons-tease.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update prefix regex in SitemapOptionsSchema --------- Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
3c73441
commit 9b78c99
Showing
4 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
"@astrojs/sitemap": minor | ||
--- | ||
|
||
Adds a new configuration option `prefix` that allows you to change the default `sitemap-*.xml` file name. | ||
|
||
By default, running `astro build` creates both `sitemap-index.xml` and `sitemap-0.xml` in your output directory. | ||
|
||
To change the names of these files (e.g. to `astrosite-index.xml` and `astrosite-0.xml`), set the `prefix` option in your `sitemap` integration configuration: | ||
|
||
``` | ||
import { defineConfig } from 'astro/config'; | ||
import sitemap from '@astrojs/sitemap'; | ||
export default defineConfig({ | ||
site: 'https://example.com', | ||
integrations: [ | ||
sitemap({ | ||
prefix: 'astrosite-', | ||
}), | ||
], | ||
}); | ||
``` | ||
|
||
This option is useful when Google Search Console is unable to fetch your default sitemap files, but can read renamed files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { loadFixture, readXML } from './test-utils.js'; | ||
import { sitemap } from './fixtures/static/deps.mjs'; | ||
import assert from 'node:assert/strict'; | ||
import { before, describe, it } from 'node:test'; | ||
|
||
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`); | ||
}); | ||
}); | ||
}); |