Skip to content

Commit

Permalink
Sitemap should only include page routes (#7656)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
natemoo-re authored Jul 17, 2023
1 parent 6ad4672 commit dd931a7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-experts-tie.md
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions .changeset/giant-tomatoes-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Ensure trailing slash is only added to page routes
3 changes: 1 addition & 2 deletions packages/integrations/sitemap/README.md
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ import sitemap from '@astrojs/sitemap';
export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com',
redirects: {
'/redirect': '/'
},
experimental: {
redirects: true
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function get({}) {
return {
body: JSON.stringify({
name: 'Astro',
url: 'https://astro.build/',
}),
};
}
26 changes: 26 additions & 0 deletions packages/integrations/sitemap/test/routes.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit dd931a7

Please sign in to comment.