Please enable JS

Generating a sitemap.xml.gz every night.

There are a lot of NPM packages that help you throughout your Node journey. We'll use two of them to generate a sitemap.xml every night for searchengines to pickup in this simple example;

We'll use sitemap and cron amongst more known packages in the next snippets.

In the below example we'll generate a sitemap.xml on the filesystem every night;

var sm = require('sitemap');
var _ = require('lodash');
var models = require('../models');
var fs = require('fs');
var CronJob = require('cron').CronJob;
var logger = require('./winston_logger');

var job = new CronJob('00 00 00 * * *', function() {
        /*
         * Runs every weekday (Monday through Sunday)
         * at 00:00:00 AM. Midnight...
         */
        generateSitemap();

    }, function () {
        logger.error('Cronjob for sitemap stopped');
    },
    true, /* Start the job right now */
    'Europe/Amsterdam' /* Time zone of this job. */
);

function generateSitemap() {

    logger.info('Writing sitemap.xml');

    var urls = [
        { url: '/blog'      , changefreq: 'weekly'  , priority: 0.3 },
        { url: '/contact/'  , changefreq: 'monthly' , priority: 0.9 }
    ];

    models.Page.findAll().then(function(pages){
        _.each(pages, function(n, key) {
            urls.push({ url: '/'+ n.slug, changefreq: 'weekly', priority: 0.4 });
        });

        var sitemap = sm.createSitemap ({
            hostname: 'https://thedutchlab.com',
            cacheTime: 600000,  // 600 sec cache period
            urls: urls
        });

        fs.writeFileSync(require('path').resolve(__dirname,'../public/sitemap.xml'), sitemap.toString());
    });

}

Next, in Express (yet another package...) we can serve the xml sitemap like this:

var express = require('express');
var router = express.Router();
var zlib = require('zlib');
var fs = require('fs');

/* GET sitemap */
router.get('/sitemap.xml', function(req, res) {
    res.header('Content-Type', 'application/xml');
    res.send( fs.readFileSync(require('path').resolve(__dirname,'../public/sitemap.xml'), "utf8") );
});

/* GET gzipped sitemap */
router.get('/sitemap.xml.gz', function(req, res) {
    res.header('Content-Type: application/x-gzip');
    res.header('Content-Encoding: gzip');
    res.header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
    zlib.gzip(new Buffer(fs.readFileSync(require('path').resolve(__dirname,'../public/sitemap.xml'), "utf8"), 'utf8'), function(error, data) {
        res.send(data);
    });
});

That concludes the simple example on how to generate a sitemap.xml every night.



Interessant

Een interessant onderwerp voorbij zien komen en er meer over te weten komen? Neem contact op.