Skip to content

Commit

Permalink
Add generate-sitemap script to package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
efazulkarim committed Mar 3, 2024
1 parent 57b4950 commit febadc6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
42 changes: 42 additions & 0 deletions generate-sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs');
const path = require('path');
const globby = require('globby'); // Make sure to install globby first

(async () => {
// Paths under the pages directory that should be in the sitemap
const pages = await globby([
'pages/*.js', // All JS files in the pages directory
'!pages/_*.js', // Exclude Next.js special files
'!pages/api', // Exclude API routes
]);

// Path to the posts directory containing Markdown files
const posts = await globby('posts/**/*.md');

// Combine and convert both lists to URL paths
const sitemapUrls = [...pages, ...posts].map((file) => {
const filePath = file
.replace('pages', '')
.replace('posts', '/posts')
.replace(/\.js$/, '')
.replace(/\.md$/, '')
.replace(/index$/, ''); // Remove index to avoid duplicate home page entry
return `https://glamaura.me${filePath}`;
});

// Create the sitemap.xml content
const sitemapContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemapUrls
.map(
(url) => `
<url>
<loc>${url}</loc>
</url>`
)
.join('')}
</urlset>`;

// Write the sitemap.xml to the public directory
fs.writeFileSync(path.join(__dirname, 'public', 'sitemap.xml'), sitemapContent);
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"generate-sitemap": "node generate-sitemap.js"
},
"dependencies": {
"@datadog/browser-rum": "^5.10.0",
Expand Down
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
User-agent: *
Allow: /
Sitemap: https://glamaura.me/sitemap.xml
40 changes: 40 additions & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

<url>
<loc>https://glamaura.me/posts/10-most-essential-pet-accessories-that-every-new-cat-owner-needs</loc>
</url>
<url>
<loc>https://glamaura.me/posts/achieve-flawless-skin</loc>
</url>
<url>
<loc>https://glamaura.me/posts/best-anti-aging-skincare-products-of-2024</loc>
</url>
<url>
<loc>https://glamaura.me/posts/dress-to-impress-womens-apparel-guide-for-formal-places-in-2024</loc>
</url>
<url>
<loc>https://glamaura.me/posts/flutter-the-good-the-bad-and-the-ugly</loc>
</url>
<url>
<loc>https://glamaura.me/posts/guide-to-hair-type</loc>
</url>
<url>
<loc>https://glamaura.me/posts/mixing-patterns-cloth</loc>
</url>
<url>
<loc>https://glamaura.me/posts/the-ultimate-guide-to-hydrating-your-skin</loc>
</url>
<url>
<loc>https://glamaura.me/posts/top-5-must-have-pet-accessories-for-your-furry-friend</loc>
</url>
<url>
<loc>https://glamaura.me/posts/top-picks-for-every-type-of-boyfriend</loc>
</url>
<url>
<loc>https://glamaura.me/posts/top-womens-perfumes-of-2024-your-ultimate-buying-guide</loc>
</url>
<url>
<loc>https://glamaura.me/posts/your-everyday-step-by-step-skincare-guide</loc>
</url>
</urlset>

0 comments on commit febadc6

Please sign in to comment.