IlusionDev / nextjs-sitemap-generator

Generate sitemap.xml from nextjs pages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

apply config with wildcard in pagesConfig

duruld opened this issue · comments

commented

It would be great to do a wildcard configuration for child pages under a path, like:

pagesConfig: { '/blog/*': { } }

@duruld This is a great idea, I take a note

I put a PR up for this, it's a preliminary work, just to get some thoughts

https://github.com/IlusionDev/nextjs-sitemap-generator/pull/70/files

Until the PR will be accepted, I would like to suggest the dirty hack, that inserts configuration data into resulting sitemap.xml

sitemap_generator.js code:

const sitemap = require('nextjs-sitemap-generator');

const SITEMAP_DIR = ".next/static/";
const BASE_URL = "https://example.com";

sitemap({
    baseUrl: BASE_URL,
    pagesDirectory: __dirname + "/.next/server/pages",
    targetDirectory: SITEMAP_DIR,
    ignoredExtensions: ["js", "map"],
    ignoredPaths: ["[projectId]", "[itemId]"],
});


// Kludge :c
const fs = require('fs');
fs.readFile(`${SITEMAP_DIR}/sitemap.xml`, 'utf8', function (err, data) {
    if (err) {
        return console.log(err);
    }

    // key -- regexp path, value -- addition string data to put into the result XML
    const ADDITIONAL_DATA = {
        '.+': '<priority>0.5</priority>\n' +
            '<changefreq>daily</changefreq>',
    }

    let result = data;
    for (let [path, value] of Object.entries(ADDITIONAL_DATA)) {
        const regexp = new RegExp(`(<loc>${BASE_URL}\/${path}<\/loc>)`, 'g');
        result = data.replace(regexp, `$1\n${value}`);
    }


    fs.writeFile(`${SITEMAP_DIR}/sitemap.xml`, result, 'utf8', function (err) {
        if (err) return console.log(err);
    });
});

Added in #70