Skip to content

Commit

Permalink
Update examples/with-sitemap/scripts/generate-sitemap.js
Browse files Browse the repository at this point in the history
Co-authored-by: Luis Alvarez D. <[email protected]>
  • Loading branch information
khattakdev and lfades authored Jul 23, 2020
1 parent e311207 commit 34626f2
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions examples/with-sitemap/scripts/generate-sitemap.js
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()

0 comments on commit 34626f2

Please sign in to comment.