Skip to content
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/icy-pigs-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Updates how routes are retrieved to avoid relying on a deprecated API
13 changes: 9 additions & 4 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { AstroConfig, AstroIntegration } from 'astro';
import type { AstroConfig, AstroIntegration, IntegrationResolvedRoute } from 'astro';
import type { EnumChangefreq, LinkItem as LinkItemBase, SitemapItemLoose } from 'sitemap';
import { ZodError } from 'zod';

Expand Down Expand Up @@ -76,17 +76,22 @@ const isStatusCodePage = (locales: string[]) => {
};
};
const createPlugin = (options?: SitemapOptions): AstroIntegration => {
let _routes: Array<IntegrationResolvedRoute>;
let config: AstroConfig;

return {
name: PKG_NAME,

hooks: {
'astro:routes:resolved': ({ routes }) => {
_routes = routes;
},

'astro:config:done': async ({ config: cfg }) => {
config = cfg;
},

'astro:build:done': async ({ dir, routes, pages, logger }) => {
'astro:build:done': async ({ dir, pages, logger }) => {
try {
if (!config.site) {
logger.warn(
Expand All @@ -112,15 +117,15 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return new URL(fullPath, finalSiteUrl).href;
});

const routeUrls = routes.reduce<string[]>((urls, r) => {
const 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
*/
if (r.pathname) {
if (shouldIgnoreStatus(r.pathname ?? r.route)) return urls;
if (shouldIgnoreStatus(r.pathname ?? r.pattern)) return urls;

// `finalSiteUrl` may end with a trailing slash
// or not because of base paths.
Expand Down