Fix generation for routes defined using getStaticPaths (#7029)

* Fix static site dynamic routes for sitemap integration

* Add changeset

* Update pnpm-lock

* Remove console.log
This commit is contained in:
Reuben Tier 2023-05-08 21:12:41 +01:00 committed by GitHub
parent e54dcd5943
commit 1b90a7a5d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 356 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---
Fix generation for static dynamic routes

File diff suppressed because one or more lines are too long

View file

@ -61,7 +61,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
config = cfg;
},
'astro:build:done': async ({ dir, routes }) => {
'astro:build:done': async ({ dir, routes, pages }) => {
try {
if (!config.site) {
logger.warn(
@ -85,7 +85,14 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return;
}
let pageUrls = routes.reduce<string[]>((urls, r) => {
let pageUrls = pages.map((p) => {
if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/'))
finalSiteUrl.pathname += '/';
const path = finalSiteUrl.pathname + p.pathname;
return new URL(path, finalSiteUrl).href;
});
let routeUrls = routes.reduce<string[]>((urls, r) => {
/**
* Dynamic URLs have entries with `undefined` pathnames
*/
@ -119,9 +126,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return;
}
if (customPages) {
pageUrls = Array.from(new Set([...pageUrls, ...customPages]));
}
pageUrls = Array.from(new Set([...pageUrls, ...routeUrls, ...(customPages ?? [])]));
if (pageUrls.length === 0) {
logger.warn(`No pages found!\n\`${OUTFILE}\` not created.`);

View file

@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com',
})

View file

@ -0,0 +1,9 @@
{
"name": "@test/sitemap-static",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/sitemap": "workspace:*"
}
}

View file

@ -0,0 +1,17 @@
---
export function getStaticPaths() {
return [
{ params: { slug: 'one' }, props: { title: 'One' } },
{ params: { slug: 'two' }, props: { title: 'Two' } },
]
}
---
<html>
<head>
<title>{Astro.props.title}</title>
</head>
<body>
<h1>{Astro.props.title}</h1>
</body>
</html>

View file

@ -0,0 +1,22 @@
import { loadFixture, readXML } from './test-utils.js';
import { expect } from 'chai';
describe('getStaticPaths support', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
});
await fixture.build();
});
it('getStaticPath pages require zero config', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls[0].loc[0]).to.equal('http://example.com/one/');
expect(urls[1].loc[0]).to.equal('http://example.com/two/');
});
});

View file

@ -4575,6 +4575,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/sitemap/test/fixtures/static:
dependencies:
'@astrojs/sitemap':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/sitemap/test/fixtures/trailing-slash:
dependencies:
'@astrojs/sitemap':