Auto add sitemap url in robots.txt
This commit is contained in:
@ -9,6 +9,7 @@ import image from '@astrojs/image';
|
|||||||
import mdx from '@astrojs/mdx';
|
import mdx from '@astrojs/mdx';
|
||||||
import icon from 'astro-icon';
|
import icon from 'astro-icon';
|
||||||
import partytown from '@astrojs/partytown';
|
import partytown from '@astrojs/partytown';
|
||||||
|
import tasks from "./src/utils/tasks"
|
||||||
|
|
||||||
import { readingTimeRemarkPlugin } from './src/utils/frontmatter.mjs';
|
import { readingTimeRemarkPlugin } from './src/utils/frontmatter.mjs';
|
||||||
|
|
||||||
@ -63,6 +64,8 @@ export default defineConfig({
|
|||||||
config: { forward: ['dataLayer.push'] },
|
config: { forward: ['dataLayer.push'] },
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
|
|
||||||
|
tasks()
|
||||||
],
|
],
|
||||||
|
|
||||||
markdown: {
|
markdown: {
|
||||||
|
44
src/utils/tasks.mjs
Normal file
44
src/utils/tasks.mjs
Normal file
@ -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;
|
Reference in New Issue
Block a user