Sitemap should only include page routes (#7656)

* fix(#7080): sitemap should only add trailing slash to pages

* fix(sitemap): only include pages in sitemap

* chore: add test

* chore: remove unused import

* docs: update readme
This commit is contained in:
Nate Moore 2023-07-17 15:29:56 -05:00 committed by GitHub
parent 6ad4672ef1
commit dd931a7806
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/sitemap': major
---
Sitemap only includes `page` routes (generated by `.astro` files) rather than all routes (pages, endpoints, or redirects). This behavior matches our existing documentation, but is a breaking change nonetheless.

View file

@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---
Ensure trailing slash is only added to page routes

View file

@ -1,6 +1,6 @@
# @astrojs/sitemap 🗺
This **[Astro integration][astro-integration]** generates a sitemap based on your routes when you build your Astro project.
This **[Astro integration][astro-integration]** generates a sitemap based on your pages when you build your Astro project.
- <strong>[Why Astro Sitemap](#why-astro-sitemap)</strong>
- <strong>[Installation](#installation)</strong>
@ -337,7 +337,6 @@ The resulting sitemap looks like this:
## Examples
- The official Astro website uses Astro Sitemap to generate [its sitemap](https://astro.build/sitemap-index.xml).
- The [integrations playground template](https://github.com/withastro/astro/tree/latest/examples/integrations-playground?on=github) comes with Astro Sitemap installed. Try adding a route and building the project!
- [Browse projects with Astro Sitemap on GitHub](https://github.com/search?q=%22@astrojs/sitemap%22+filename:package.json&type=Code) for more examples!
## Troubleshooting

View file

@ -96,6 +96,9 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
});
let routeUrls = routes.reduce<string[]>((urls, r) => {
// Only expose pages, not endpoints or redirects
if (r.type !== 'page') return urls;
/**
* Dynamic URLs have entries with `undefined` pathnames
*/

View file

@ -4,4 +4,10 @@ import sitemap from '@astrojs/sitemap';
export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com',
redirects: {
'/redirect': '/'
},
experimental: {
redirects: true
}
})

View file

@ -0,0 +1,8 @@
export async function get({}) {
return {
body: JSON.stringify({
name: 'Astro',
url: 'https://astro.build/',
}),
};
}

View file

@ -0,0 +1,26 @@
import { loadFixture, readXML } from './test-utils.js';
import { expect } from 'chai';
describe('routes', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
/** @type {string[]} */
let urls;
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
});
await fixture.build();
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
urls = data.urlset.url.map(url => url.loc[0]);
});
it('does not include endpoints', async () => {
expect(urls).to.not.include('http://example.com/endpoint.json');
});
it('does not include redirects', async () => {
expect(urls).to.not.include('http://example.com/redirect');
});
});