-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #9 from codin-gg/3-sitemap
adds www/sitemap.xml build
- Loading branch information
Showing
4 changed files
with
35 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
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ node_modules | |
www/api/*/ | ||
!www/api/*.* | ||
www/api/.DS_Store | ||
www/sitemap.xml | ||
|
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,32 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
|
||
async function * getAllFiles (dir, { join } = path, { readdir, stat } = fs) { | ||
const files = await readdir(dir) | ||
for (const file of files) { | ||
const path = join(dir, file) | ||
const stats = await stat(path) | ||
if (stats.isDirectory()) { | ||
yield * getAllFiles(path) | ||
} else if (stats.isFile()) { | ||
yield path | ||
} | ||
} | ||
} | ||
|
||
async function generateSitemap (dir, { relative } = path, { stat } = fs) { | ||
let sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' | ||
for await (const file of getAllFiles(dir)) { | ||
if (['www/sitemap.xml'].includes(file)) continue | ||
sitemap += `<url><loc>/${relative(dir, file).replace(/\\/g, '/')}</loc><lastmod>${(await stat(file)).mtime.toISOString().split('T')[0]}</lastmod></url>\n` | ||
} | ||
return sitemap + '</urlset>' | ||
} | ||
|
||
console.time('[bin/build-sitemap] created: www/sitemap.xml time') | ||
await fs.writeFile('www/sitemap.xml', await generateSitemap('www')) | ||
console.timeEnd('[bin/build-sitemap] created: www/sitemap.xml time') | ||
|
||
// For when this file will become too big: | ||
// - https://webmasters.stackexchange.com/a/93807 | ||
// - https://chat.openai.com/c/ebb14320-2827-423d-8ddd-1dd016c25497 (first thread) |
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