From e2b2a0b412c351c4a7f4cb058e8e1a12004e956e Mon Sep 17 00:00:00 2001 From: prototypa Date: Fri, 11 Aug 2023 23:48:29 -0400 Subject: [PATCH] Auto add sitemap url in robots.txt --- astro.config.mjs | 3 +++ src/utils/tasks.mjs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/utils/tasks.mjs diff --git a/astro.config.mjs b/astro.config.mjs index 7e3db13..8b45de6 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -9,6 +9,7 @@ import image from '@astrojs/image'; import mdx from '@astrojs/mdx'; import icon from 'astro-icon'; import partytown from '@astrojs/partytown'; +import tasks from "./src/utils/tasks" import { readingTimeRemarkPlugin } from './src/utils/frontmatter.mjs'; @@ -63,6 +64,8 @@ export default defineConfig({ config: { forward: ['dataLayer.push'] }, }) ), + + tasks() ], markdown: { diff --git a/src/utils/tasks.mjs b/src/utils/tasks.mjs new file mode 100644 index 0000000..25ca768 --- /dev/null +++ b/src/utils/tasks.mjs @@ -0,0 +1,44 @@ +import fs from 'node:fs'; +import os from "node:os"; + +const tasksIntegration = () => { + let config; + return { + name: 'AstroWind:tasks', + + hooks: { + 'astro:config:done': async ({ config: cfg }) => { + config = cfg; + console.log(config); + }, + + 'astro:build:done': async () => { + try { + const outDir = config.outDir; + const publicDir = config.publicDir; + const sitemapName = "sitemap-index.xml"; + const sitemapFile = new URL(sitemapName, outDir); + const robotsTxtFile = new URL('robots.txt', publicDir); + const robotsTxtFileInOut = new URL('robots.txt', outDir); + + const hasIntegration = + Array.isArray(config?.integrations) && + config.integrations?.find((e) => e?.name === '@astrojs/sitemap') !== undefined; + const sitemapExists = fs.existsSync(sitemapFile); + + if (hasIntegration && sitemapExists) { + const robotsTxt = fs.readFileSync(robotsTxtFile, { encoding: 'utf8', flags: 'a+' }); + + if (!robotsTxt.includes("Sitemap:")) { + const sitemapUrl = new URL(sitemapName, String(new URL(config.base, config.site))); + const content = `${os.EOL}${os.EOL}Sitemap: ${sitemapUrl}` + fs.appendFileSync(robotsTxtFileInOut, content, { encoding: 'utf8', flags: 'w' }) + } + } + } catch (err) { /* empty */ } + }, + }, + }; +}; + +export default tasksIntegration;