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:
parent
e54dcd5943
commit
1b90a7a5d5
8 changed files with 356 additions and 5 deletions
5
.changeset/hungry-spies-kick.md
Normal file
5
.changeset/hungry-spies-kick.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/sitemap': patch
|
||||
---
|
||||
|
||||
Fix generation for static dynamic routes
|
277
packages/integrations/cloudflare/test/fixtures/basics/functions/[[path]].js
vendored
Normal file
277
packages/integrations/cloudflare/test/fixtures/basics/functions/[[path]].js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -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.`);
|
||||
|
|
7
packages/integrations/sitemap/test/fixtures/static/astro.config.mjs
vendored
Normal file
7
packages/integrations/sitemap/test/fixtures/static/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
import sitemap from '@astrojs/sitemap';
|
||||
|
||||
export default defineConfig({
|
||||
integrations: [sitemap()],
|
||||
site: 'http://example.com',
|
||||
})
|
9
packages/integrations/sitemap/test/fixtures/static/package.json
vendored
Normal file
9
packages/integrations/sitemap/test/fixtures/static/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "@test/sitemap-static",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*",
|
||||
"@astrojs/sitemap": "workspace:*"
|
||||
}
|
||||
}
|
17
packages/integrations/sitemap/test/fixtures/static/src/pages/[slug].astro
vendored
Normal file
17
packages/integrations/sitemap/test/fixtures/static/src/pages/[slug].astro
vendored
Normal 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>
|
22
packages/integrations/sitemap/test/staticPaths.test.js
Normal file
22
packages/integrations/sitemap/test/staticPaths.test.js
Normal 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/');
|
||||
});
|
||||
});
|
|
@ -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':
|
||||
|
|
Loading…
Reference in a new issue