How to use the sitemap.SitemapStream function in sitemap

To help you get started, we’ve selected a few sitemap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cswbrian / district-councils-dashboard / ssr / server / lib / sitemap.js View on Github external
server.get('/sitemap.xml', (req, res) => {
    res.header('Content-Type', 'application/xml')
    res.header('Content-Encoding', 'gzip')

    try {
      const smStream = new SitemapStream({
        hostname: process.env.PUBLIC_URL,
      })
      const pipeline = smStream.pipe(createGzip())

      getDistricts().then(urls => {
        // Add URLs
        urls.forEach(url => {
          smStream.write(url)
        })
        smStream.end()

        // cache the response
        streamToPromise(pipeline).then(sm => sitemap = sm)
        // stream the response
        pipeline.pipe(res).on('error', (err) => { throw err })
      })