From 6b77de2263f7eb48222b1aeac330230361dfe5c3 Mon Sep 17 00:00:00 2001 From: Atila Fassina Date: Mon, 13 Mar 2023 20:09:29 +0100 Subject: [PATCH] refactor: move logic to `astro:build:done` --- packages/integrations/sitemap/package.json | 1 - packages/integrations/sitemap/src/index.ts | 81 +++++-------------- .../sitemap/src/utils/is-route-prerendered.ts | 8 -- pnpm-lock.yaml | 17 ++-- 4 files changed, 30 insertions(+), 77 deletions(-) delete mode 100644 packages/integrations/sitemap/src/utils/is-route-prerendered.ts diff --git a/packages/integrations/sitemap/package.json b/packages/integrations/sitemap/package.json index 4962033388459..7d8c93d277660 100644 --- a/packages/integrations/sitemap/package.json +++ b/packages/integrations/sitemap/package.json @@ -33,7 +33,6 @@ "test": "mocha --timeout 20000" }, "dependencies": { - "fast-glob": "^3.2.11", "sitemap": "^7.1.1", "zod": "^3.17.3" }, diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 2cc5c9dd3fe57..7e0ef4fa6ad45 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -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}`; @@ -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; } @@ -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((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) { @@ -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) { diff --git a/packages/integrations/sitemap/src/utils/is-route-prerendered.ts b/packages/integrations/sitemap/src/utils/is-route-prerendered.ts deleted file mode 100644 index 468e09ba8489e..0000000000000 --- a/packages/integrations/sitemap/src/utils/is-route-prerendered.ts +++ /dev/null @@ -1,8 +0,0 @@ -const REGEX_PRERENDER = /const prerender = true/g; -import { readFile } from 'fs/promises'; - -export async function isRoutePrerendered(filePath: string) { - const contents = await readFile(filePath, 'utf-8'); - - return REGEX_PRERENDER.test(contents); -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 017cf1b746d85..d503a0c4d2186 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3507,13 +3507,11 @@ importers: astro: workspace:* astro-scripts: workspace:* chai: ^4.3.6 - fast-glob: ^3.2.11 mocha: ^9.2.2 sitemap: ^7.1.1 xml2js: 0.4.23 zod: ^3.17.3 dependencies: - fast-glob: 3.2.12 sitemap: 7.1.1 zod: 3.20.6 devDependencies: @@ -4196,14 +4194,15 @@ packages: - react dev: false - /@astrojs/markdown-remark/2.0.1_astro@packages+astro: - resolution: {integrity: sha512-xQF1rXGJN18m+zZucwRRtmNehuhPMMhZhi6HWKrtpEAKnHSPk8lqf1GXgKH7/Sypglu8ivdECZ+EGs6kOYVasQ==} + /@astrojs/markdown-remark/2.1.0_astro@packages+astro: + resolution: {integrity: sha512-w9T5o3UWQIfMcCkM2nLWrlfVQazh/7mw+2N/85QGcSUkZy6oNJoyy8Xz/ZkDhHLx8HPO0RT9fABR0B/H+aDaEw==} peerDependencies: astro: '*' dependencies: - '@astrojs/prism': 2.0.0 + '@astrojs/prism': 2.1.0 astro: link:packages/astro github-slugger: 1.5.0 + image-size: 1.0.2 import-meta-resolve: 2.2.1 rehype-raw: 6.1.1 rehype-stringify: 9.0.3 @@ -4223,8 +4222,8 @@ packages: resolution: {integrity: sha512-mol57cw1jJMcQgKMRGn7p6cewajq6JTNtqj5aAZgROWam/phVDSOCbXj/WU3O9+3qFnyKtpczoufQKwJTQltAw==} engines: {node: '>=16.12.0'} dependencies: - '@astrojs/markdown-remark': 2.0.1_astro@packages+astro - '@astrojs/prism': 2.0.0 + '@astrojs/markdown-remark': 2.1.0_astro@packages+astro + '@astrojs/prism': 2.1.0 '@mdx-js/mdx': 2.3.0 '@mdx-js/rollup': 2.3.0 acorn: 8.8.2 @@ -4268,8 +4267,8 @@ packages: - supports-color dev: false - /@astrojs/prism/2.0.0: - resolution: {integrity: sha512-YgeoeEPqsxaEpg0rwe/bUq3653LqSQnMjrLlpYwrbQQMQQqz6Y5yXN+RX3SfLJ6ppNb4+Fu2+Z49EXjk48Ihjw==} + /@astrojs/prism/2.1.0: + resolution: {integrity: sha512-+II6nfIFGZ7iH0FunhRGcj/J1mCxjcHl85cZRuFePKLoIhFHJT3nC3myQnUw386hUaIn2W20McxxtAVf4leeRQ==} engines: {node: '>=16.12.0'} dependencies: prismjs: 1.29.0