Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemap should only include page routes #7656

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});