1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-sitemap-and-robots-express-server/server/sitemapAndRobots.js
Kelly Burke 3f6834dfec sitemap.xml and robots.txt example (#4163)
This example app shows you how to set up sitemap.xml and robots.txt files for proper indexing by search engine bots.
2018-05-25 14:01:32 +02:00

51 lines
1,000 B
JavaScript

const sm = require('sitemap')
const path = require('path')
const posts = require('./posts')
const sitemap = sm.createSitemap({
hostname: 'https://sitemap-robots.now.sh',
cacheTime: 600000 // 600 sec - cache purge period
})
const setup = ({ server }) => {
const Posts = posts()
for (let i = 0; i < Posts.length; i += 1) {
const post = Posts[i]
sitemap.add({
url: `/posts/${post.slug}`,
changefreq: 'daily',
priority: 0.9
})
}
sitemap.add({
url: '/a',
changefreq: 'daily',
priority: 1
})
sitemap.add({
url: '/b',
changefreq: 'daily',
priority: 1
})
server.get('/sitemap.xml', (req, res) => {
sitemap.toXML((err, xml) => {
if (err) {
res.status(500).end()
return
}
res.header('Content-Type', 'application/xml')
res.send(xml)
})
})
server.get('/robots.txt', (req, res) => {
res.sendFile(path.join(__dirname, '../static', 'robots.txt'))
})
}
module.exports = setup