Skip to content

Commit

Permalink
refactor: move logic to astro:build:done
Browse files Browse the repository at this point in the history
  • Loading branch information
atilafassina committed Mar 13, 2023
1 parent 5939555 commit 6b77de2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 77 deletions.
1 change: 0 additions & 1 deletion packages/integrations/sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"test": "mocha --timeout 20000"
},
"dependencies": {
"fast-glob": "^3.2.11",
"sitemap": "^7.1.1",
"zod": "^3.17.3"
},
Expand Down
81 changes: 22 additions & 59 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import {
simpleSitemapAndIndex,
SitemapItemLoose,
} from 'sitemap';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import fg from 'fast-glob';
import { ZodError } from 'zod';

import { generateSitemap } from './generate-sitemap.js';
import { Logger } from './utils/logger.js';
import { isRoutePrerendered } from './utils/is-route-prerendered.js';
import { validateOptions } from './validate-options.js';

export type ChangeFreq = `${EnumChangefreq}`;
Expand Down Expand Up @@ -61,61 +58,15 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {

hooks: {
'astro:config:done': async ({ config: cfg }) => {
if (cfg.site) {
config = cfg;
} else {
// eslint-disable-next-line no-console
console.warn(
'The Sitemap integration requires the `site` astro.config option. Skipping.'
);
return;
}
},

'astro:build:start': async () => {
if (config.output !== 'server' || !config.site) {
return;
}

const srcPath = fileURLToPath(config.srcDir);
const pagesPath = resolve(srcPath, 'pages');

const pageFiles = await fg(`${pagesPath}/**/*.{astro,ts,js}`);

const routes = (
await Promise.all(
pageFiles.map(async (filePath) => {
const isPrerendered = await isRoutePrerendered(filePath);
const index = filePath.indexOf('pages/') + 6;
const routeSegment = filePath
.substring(index)
.replace(/\.(astro|ts|js)/, '')
.replace(/index$/, '');

/**
* @TODO
* figure out how to run `getStaticPaths` here.
*/
const isDynamicRoute = routeSegment.endsWith(']');
const shouldIndex = !isDynamicRoute && !isPrerendered;

return shouldIndex ? `${config.site}/${routeSegment}` : undefined;
})
)
).filter((route): route is string => Boolean(route));
const opts = validateOptions(config.site, options);

opts.customPages = opts.customPages
? Array.from(new Set([...routes, ...opts.customPages]))
: routes;
options = opts;

logger.info(`build is starting + ${JSON.stringify(opts.customPages, null, 2)}`);
config = cfg;
},

'astro:build:done': async ({ dir, pages }) => {
'astro:build:done': async ({ dir, routes }) => {
try {
if (!config.site) {
logger.warn(
'The Sitemap integration requires the `site` astro.config option. Skipping.'
);
return;
}

Expand All @@ -134,10 +85,22 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return;
}

let pageUrls = pages.map((p) => {
const path = finalSiteUrl.pathname + p.pathname;
return new URL(path, finalSiteUrl).href;
});
let pageUrls = routes.reduce<string[]>((urls, r) => {
/**
* Dynamic URLs have entries with `undefined` pathnames
*/
if (r.pathname) {
/**
* remove the initial slash from relative pathname
* because `finalSiteUrl` always has trailing slash
*/
const path = finalSiteUrl.pathname + r.pathname.substring(1);
const newUrl = new URL(path, finalSiteUrl).href;
urls.push(newUrl);
}

return urls;
}, []);

try {
if (filter) {
Expand All @@ -149,7 +112,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
}

if (customPages) {
pageUrls = [...pageUrls, ...customPages];
pageUrls = Array.from(new Set([...pageUrls, ...customPages]));
}

if (pageUrls.length === 0) {
Expand Down

This file was deleted.

17 changes: 8 additions & 9 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 6b77de2

Please sign in to comment.