Skip to content

Commit

Permalink
Merge pull request #341 from zackproser/generate-sitemap
Browse files Browse the repository at this point in the history
Generate sitemap
  • Loading branch information
zackproser authored Feb 12, 2024
2 parents 02bc531 + 2b87fcf commit c335283
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/app/sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from 'fs';
import path from 'path';

const baseUrl = process.env.SITE_URL || 'https://zackproser.com';
const baseDir = 'src/app';
const dynamicDirs = ['blog', 'videos']
const excludeDirs = ['api'];

function getRoutes() {
const fullPath = path.join(process.cwd(), baseDir);
const entries = fs.readdirSync(fullPath, { withFileTypes: true });
let routes = [];

entries.forEach(entry => {
if (entry.isDirectory() && !excludeDirs.includes(entry.name)) {
// Handle 'static' routes at the baseDir
routes.push(`/${entry.name}`);

// Handle dynamic routes
if (dynamicDirs.includes(entry.name)) {
const subDir = path.join(fullPath, entry.name);
const subEntries = fs.readdirSync(subDir, { withFileTypes: true });

subEntries.forEach(subEntry => {
if (subEntry.isDirectory()) {
routes.push(`/${entry.name}/${subEntry.name}`);
}
});
}
}
});

return routes.map(route => ({
url: `${baseUrl}${route}`,
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 1.0,
}));
}

function sitemap() {
return getRoutes();
}

export default sitemap;

0 comments on commit c335283

Please sign in to comment.