React-snap: Idea: check if rendered pages have proper title and description

Created on 14 Nov 2017  路  2Comments  路  Source: stereobooster/react-snap

If user bothered to do prerendering, lets make sure one will get max benefit out of it - I mean SEO (additionally to speed). Also possible to generate sitemap.xml. Also can check budgets for file sizes.

enhancement

Most helpful comment

Hey, I wrote a script that runs after react-snap that generate a sitemap.

const builder = require('xmlbuilder');
const { readdir, writeFile } = require('fs');
const { promisify } = require('util');

const asyncReaddir = promisify(readdir), asyncWriteFile = promisify(writeFile);

const asyncForEach = async (array, callback) => {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}

let allRoots = [];

const readSite = async (dir) => {
    const directory = await asyncReaddir(dir, { withFileTypes: true });

    await asyncForEach(directory, async fileOrDirectory => {
        const { name } = fileOrDirectory;

        const root = `${dir}/${name}`;
        if (fileOrDirectory.isDirectory()) {
            await readSite(root);
        } else if (name === 'index.html') {
            allRoots = [...allRoots, root];
        }
    });
}

const blackList = ['https://whatever.com/some-page'];

(async () => {
    const baseUrl = 'https://whatever.com/';

    await readSite('build');

    const siteUrls = allRoots.map(root => root.replace('build/', baseUrl).replace('/index.html', ''));

    const urlset = builder.create('urlset', { encoding: 'UTF-8', version: '1.0' });

    urlset.attribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

    siteUrls
        .filter(url => !blackList.includes(url))
        .sort((a, b) => a.length - b.length)
        .forEach(url => {
            const u = urlset.ele('url');

            u.ele('loc', url);
            u.ele('priority', 0.5);
        });

    const sitemap = urlset.end({ pretty: true });

    await asyncWriteFile('build/sitemap.xml', sitemap);
})();

All 2 comments

Hey, I wrote a script that runs after react-snap that generate a sitemap.

const builder = require('xmlbuilder');
const { readdir, writeFile } = require('fs');
const { promisify } = require('util');

const asyncReaddir = promisify(readdir), asyncWriteFile = promisify(writeFile);

const asyncForEach = async (array, callback) => {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}

let allRoots = [];

const readSite = async (dir) => {
    const directory = await asyncReaddir(dir, { withFileTypes: true });

    await asyncForEach(directory, async fileOrDirectory => {
        const { name } = fileOrDirectory;

        const root = `${dir}/${name}`;
        if (fileOrDirectory.isDirectory()) {
            await readSite(root);
        } else if (name === 'index.html') {
            allRoots = [...allRoots, root];
        }
    });
}

const blackList = ['https://whatever.com/some-page'];

(async () => {
    const baseUrl = 'https://whatever.com/';

    await readSite('build');

    const siteUrls = allRoots.map(root => root.replace('build/', baseUrl).replace('/index.html', ''));

    const urlset = builder.create('urlset', { encoding: 'UTF-8', version: '1.0' });

    urlset.attribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

    siteUrls
        .filter(url => !blackList.includes(url))
        .sort((a, b) => a.length - b.length)
        .forEach(url => {
            const u = urlset.ele('url');

            u.ele('loc', url);
            u.ele('priority', 0.5);
        });

    const sitemap = urlset.end({ pretty: true });

    await asyncWriteFile('build/sitemap.xml', sitemap);
})();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jessevdp picture jessevdp  路  8Comments

lewisdonovan picture lewisdonovan  路  7Comments

aheissenberger picture aheissenberger  路  7Comments

loganpowell picture loganpowell  路  9Comments

philipeatela picture philipeatela  路  3Comments