-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateRobotsTxt.js
More file actions
34 lines (27 loc) · 880 Bytes
/
generateRobotsTxt.js
File metadata and controls
34 lines (27 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { promises as fs } from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Determine the environment
const environment = process.env.NODE_ENV;
let robotsContent;
// I allow robots for both dev and production
if (environment === "production") {
robotsContent = `User-agent: *
Disallow:
Sitemap: http://localhost:5173/sitemap.xml`;
} else {
robotsContent = `User-agent: *
Disallow:
Sitemap: http://localhost:5173/sitemap.xml`;
}
// Path to write the robots.txt file
const robotsPath = path.join(__dirname, "public", "robots.txt");
// Write the robots.txt file
try {
await fs.writeFile(robotsPath, robotsContent);
console.log(`robots.txt generated for ${environment} environment`);
} catch (err) {
console.error("Error writing robots.txt:", err);
}