-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from zackproser/generate-sitemap
Generate sitemap
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|