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:
parent
6ad4672ef1
commit
dd931a7806
7 changed files with 54 additions and 2 deletions
5
.changeset/fluffy-experts-tie.md
Normal file
5
.changeset/fluffy-experts-tie.md
Normal 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.
|
5
.changeset/giant-tomatoes-dream.md
Normal file
5
.changeset/giant-tomatoes-dream.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/sitemap': patch
|
||||
---
|
||||
|
||||
Ensure trailing slash is only added to page routes
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -4,4 +4,10 @@ import sitemap from '@astrojs/sitemap';
|
|||
export default defineConfig({
|
||||
integrations: [sitemap()],
|
||||
site: 'http://example.com',
|
||||
redirects: {
|
||||
'/redirect': '/'
|
||||
},
|
||||
experimental: {
|
||||
redirects: true
|
||||
}
|
||||
})
|
||||
|
|
8
packages/integrations/sitemap/test/fixtures/static/src/pages/endpoint.json.ts
vendored
Normal file
8
packages/integrations/sitemap/test/fixtures/static/src/pages/endpoint.json.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
export async function get({}) {
|
||||
return {
|
||||
body: JSON.stringify({
|
||||
name: 'Astro',
|
||||
url: 'https://astro.build/',
|
||||
}),
|
||||
};
|
||||
}
|
26
packages/integrations/sitemap/test/routes.test.js
Normal file
26
packages/integrations/sitemap/test/routes.test.js
Normal 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');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue