nuxt-community / sitemap-module

Sitemap Module for Nuxt 2
https://sitemap.nuxtjs.org
MIT License
690 stars 127 forks source link

Nuxt Fatal Error. Error: Invalid character in string #266

Open kovan opened 2 years ago

kovan commented 2 years ago

Somehow the description of a <video> entry in the sitemap has weird characters which seem not to be in the range allowed by the XML specification.

Here is the stack trace:

at XMLStringifier.assertLegalChar (node_modules/xmlbuilder/lib/XMLStringifier.js:208:19) at XMLStringifier.cdata (node_modules/xmlbuilder/lib/XMLStringifier.js:61:21) at new XMLCData (node_modules/xmlbuilder/lib/XMLCData.js:21:35) at XMLElement.cdata (node_modules/xmlbuilder/lib/XMLNode.js:318:17) at SitemapItem.buildVideoElement (node_modules/sitemap/dist/lib/sitemap-item.js:79:47) at Array.forEach () at SitemapItem.buildXML (node_modules/sitemap/dist/lib/sitemap-item.js:173:28) at Sitemap.toString (node_modules/sitemap/dist/lib/sitemap.js:280:62) at Sitemap.toXML (node_modules/sitemap/dist/lib/sitemap.js:147:21) at generateSitemap (node_modules/@nuxtjs/sitemap/lib/generator.js:58:42) at async generateSitemaps (node_modules/@nuxtjs/sitemap/lib/generator.js:32:5) at async Promise.all (index 0) at async node_modules/@nuxtjs/sitemap/lib/module.js:44:5 at async Nuxt.callHook (node_modules/hable/dist/hable.js:1:959) at async Generator.generate (node_modules/@nuxt/generator/dist/generator.js:91:5) at async generate$1 (node_modules/@nuxt/cli/dist/cli-generate.js:46:22)

Weirdly , the error always ends says the conflicting character is at the last position of the string, tested with different strings. So I don't know which character is causing problems.

I have tried removing conflicting characters (using [this]() code), but the error persists.

And this is the code that generates the entry, which has been working fine for quite a long time until this issue appeared. Nothing really special about it I think:

function videoToSitemapEntry(video) {
    // Reference:
    // https://developers.google.com/search/docs/advanced/sitemaps/video-sitemaps
    let json = {
        url: `/video/${video.videosdb.slug}`,
        video: [
            {
                thumbnail_loc: video.snippet.thumbnails.medium.url,
                title: video.snippet.title,
                description: video.snippet.description
                    ? removeXMLInvalidChars(video.snippet.description, true).substring(0, 2040)
                    : video.snippet.title,
                duration: video.videosdb.durationSeconds,
                publication_date: dateToISO(video.snippet.publishedAt)
            },
        ],
        priority: 1.0,
    }
    return json
}

Help appreciated.

Thank you for your time.