Skip to content

Commit cd87ada

Browse files
committed
chore: made sitemap compatible
1 parent a9a4092 commit cd87ada

File tree

8 files changed

+652
-233
lines changed

8 files changed

+652
-233
lines changed

lib/generateSitemap.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// generateSitemap.ts
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import { SitemapStream, streamToPromise } from 'sitemap';
5+
6+
const generateSitemap = async () => {
7+
const sitemap = new SitemapStream({
8+
hostname: 'https://www.projectmate.net',
9+
});
10+
11+
// Use the correct path to the 'pages' directory
12+
const pagesDir = path.resolve(__dirname, '../pages');
13+
14+
// Add your static pages to the sitemap
15+
sitemap.write({ url: '/' });
16+
17+
// Add dynamic pages if needed
18+
// Example: add dynamic pages from a directory
19+
const dynamicPages = fs.readdirSync(pagesDir).filter((file) => {
20+
return file.endsWith('.tsx') && file !== 'index.tsx';
21+
});
22+
23+
dynamicPages.forEach((file) => {
24+
const pagePath = `/${file.replace(/\.tsx$/, '')}`;
25+
sitemap.write({ url: pagePath });
26+
});
27+
28+
sitemap.end();
29+
30+
// Convert the stream to a promise and save the sitemap to a file
31+
const sitemapXML = await streamToPromise(sitemap)
32+
.then((data) => data.toString())
33+
.catch((error) => {
34+
console.error('Error converting stream to promise:', error);
35+
return '';
36+
});
37+
38+
fs.writeFileSync('./public/sitemap.xml', sitemapXML);
39+
40+
console.log('Sitemap created successfully!');
41+
};
42+
43+
generateSitemap();

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"lint": "next lint",
1111
"prepare": "husky install",
1212
"seed": "npx prisma db seed",
13-
"prettier-fix": "prettier --write '**/*.{js,jsx,ts,tsx,css,md,json,html}'"
13+
"prettier-fix": "prettier --write '**/*.{js,jsx,ts,tsx,css,md,json,html}'",
14+
"generate-sitemap": "ts-node --project tsconfig.sitemap.json lib/generateSitemap.ts"
1415
},
1516
"dependencies": {
1617
"@headlessui/react": "^1.7.11",
@@ -49,6 +50,8 @@
4950
"react-icons": "^4.7.1",
5051
"react-markdown": "^8.0.5",
5152
"react-share": "^4.4.1",
53+
"sitemap": "^7.1.1",
54+
"stream-to-promise": "^3.0.0",
5255
"swr": "^2.0.3",
5356
"tailwind-merge": "^2.0.0",
5457
"tailwindcss-animate": "^1.0.7",
@@ -59,7 +62,7 @@
5962
"devDependencies": {
6063
"@tailwindcss/line-clamp": "^0.4.2",
6164
"@types/next": "^9.0.0",
62-
"@types/node": "18.14.1",
65+
"@types/node": "^18.14.1",
6366
"@types/react": "18.0.28",
6467
"@types/react-dom": "18.0.11",
6568
"@types/react-tagsinput": "^3.19.9",
@@ -78,6 +81,6 @@
7881
"prettier-plugin-tailwindcss": "^0.2.3",
7982
"raw-loader": "^4.0.2",
8083
"tailwindcss": "^3.2.7",
81-
"typescript": "^5.0.4"
84+
"typescript": "^5.3.3"
8285
}
8386
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

public/sitemap.xml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"><url><loc>https://www.projectmate.net/</loc></url><url><loc>https://www.projectmate.net/404</loc></url><url><loc>https://www.projectmate.net/_app</loc></url><url><loc>https://www.projectmate.net/_document</loc></url><url><loc>https://www.projectmate.net/mates</loc></url><url><loc>https://www.projectmate.net/posts</loc></url><url><loc>https://www.projectmate.net/projects</loc></url></urlset>

tsconfig.sitemap.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"module": "CommonJS",
4+
"target": "esnext",
5+
"esModuleInterop": true
6+
}
7+
}

0 commit comments

Comments
 (0)