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

Fix generation for routes defined using getStaticPaths #7029

Merged
merged 5 commits into from
May 8, 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/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.