-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update examples/with-sitemap/scripts/generate-sitemap.js
Co-authored-by: Luis Alvarez D. <[email protected]>
- Loading branch information
1 parent
e311207
commit 34626f2
Showing
1 changed file
with
16 additions
and
23 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 |
---|---|---|
@@ -1,35 +1,28 @@ | ||
const fs = require('fs') | ||
|
||
const globby = require('globby') | ||
|
||
const WEBSITE_URL = 'http://localhost:3000' | ||
function addPage(page) { | ||
const path = page.replace('pages', '').replace('.js', '').replace('.mdx', '') | ||
const route = path === '/index' ? '' : path | ||
|
||
return ` <url> | ||
<loc>${`${process.env.WEBSITE_URL}${route}`}</loc> | ||
<changefreq>hourly</changefreq> | ||
</url>` | ||
} | ||
|
||
;(async () => { | ||
async function generateSitemap() { | ||
// Ignore Next.js specific files (e.g., _app.js) and API routes. | ||
const pages = await globby([ | ||
'pages/**/*{.js,.mdx}', | ||
'!pages/_*.js', | ||
'!pages/api', | ||
]) | ||
const sitemap = ` | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
${pages | ||
.map((page) => { | ||
const path = page | ||
.replace('pages', '') | ||
.replace('.js', '') | ||
.replace('.mdx', '') | ||
const route = path === '/index' ? '' : path | ||
return ` | ||
<url> | ||
<loc>${`${WEBSITE_URL}${route}`}</loc> | ||
</url> | ||
` | ||
}) | ||
.join('')} | ||
</urlset> | ||
` | ||
const sitemap = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
${pages.map(addPage).join('\n')} | ||
</urlset>` | ||
|
||
fs.writeFileSync('public/sitemap.xml', sitemap) | ||
})() | ||
} | ||
|
||
generateSitemap() |