Skip to content

Commit

Permalink
Fix generation for routes defined using getStaticPaths (#7029)
Browse files Browse the repository at this point in the history
* Fix static site dynamic routes for sitemap integration

* Add changeset

* Update pnpm-lock

* Remove console.log
  • Loading branch information
TheOtterlord authored May 8, 2023
1 parent e54dcd5 commit 1b90a7a
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-spies-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Fix generation for static dynamic routes

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
*/
Expand Down Expand Up @@ -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.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/sitemap-static",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/sitemap": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -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>
22 changes: 22 additions & 0 deletions packages/integrations/sitemap/test/staticPaths.test.js
Original file line number Diff line number Diff line change
@@ -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/');
});
});
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b90a7a

Please sign in to comment.