Skip to content

Commit a7e8720

Browse files
committed
Make dynamic sitemap and robots txt
1 parent 3da01b7 commit a7e8720

24 files changed

+351
-20
lines changed

package-lock.json

+169
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7-
"build": "next build && next export",
7+
88
"analyze:build": "cross-env ANALYZE=true npm run build",
99
"analyze:dev": "cross-env ANALYZE=true npm run dev",
10-
"script:gen-articles-index": "node ./scripts/generateArticlesIndex.js"
10+
11+
"script:gen-articles-index": "node ./scripts/generateArticlesIndex.js",
12+
"script:gen-sitemap": "node ./scripts/generateSitemap.js",
13+
"script:gen-robots": "node ./scripts/generateRobots.js",
14+
15+
"build": "next build && next export",
16+
"postbuild": "npm run script:gen-sitemap && npm run script:gen-robots"
1117
},
1218
"dependencies": {
1319
"@headlessui/react": "^1.7.13",
@@ -38,6 +44,8 @@
3844
"autoprefixer": "^10.4.14",
3945
"cross-env": "^7.0.3",
4046
"cssnano": "^5.1.15",
47+
"dotenv": "^16.0.3",
48+
"glob": "^9.3.2",
4149
"next-bundle-analyzer": "^0.6.7",
4250
"next-compose-plugins": "^2.2.1",
4351
"postcss": "^8.4.21",

public/robots.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
User-Agent: *
2+
Allow: *
3+
4+
Sitemap: /sitemap.xml

scripts/generateRobots.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const path = require("path");
2+
const fs = require("fs/promises");
3+
4+
const dotenv = require("dotenv");
5+
dotenv.config({ path: path.resolve(__dirname, "../.env") });
6+
dotenv.config({ path: path.resolve(__dirname, "../.env.local"), override: true });
7+
8+
const run = async () => {
9+
const outDir = path.resolve(__dirname, "../out");
10+
11+
let content = "";
12+
13+
content += "User-agent: *\n";
14+
content += "Allow: *\n\n";
15+
content += `Sitemap: ${process.env.NEXT_PUBLIC_FRONTEND_URL}/sitemap.xml`;
16+
17+
fs.writeFile(path.join(outDir, "robots.txt"), content);
18+
};
19+
20+
run();

0 commit comments

Comments
 (0)