Skip to content

image:/video: fields are never populated; empty <urlset> returns UnknownStateError; /g exclusions alternate #236

Open

Description

@trackoor

Sitemapper.fetch() never populates any of the documented image:* or video:* fields, reports a valid empty <urlset> (or <sitemapindex>) as UnknownStateError, and alternates matches when an exclusion RegExp has the g or y flag.

Save as repro.mjs and run npm install sitemapper@4.1.6 && node repro.mjs:

import http from "node:http";
import Sitemapper from "sitemapper";

const xml = {
  "/img.xml": `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://e.com/i</loc>
    <image:image>
      <image:loc>http://e.com/1.png</image:loc>
      <image:title>Photo</image:title>
    </image:image>
  </url>
</urlset>`,
  "/empty.xml": `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>`,
  "/ex.xml": `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url><loc>http://e.com/foo/1</loc></url>
  <url><loc>http://e.com/foo/2</loc></url>
  <url><loc>http://e.com/foo/3</loc></url>
  <url><loc>http://e.com/foo/4</loc></url>
  <url><loc>http://e.com/bar</loc></url>
</urlset>`,
};

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "application/xml" });
  res.end(xml[req.url]);
});
await new Promise((r) => server.listen(0, "127.0.0.1", r));
const base = `http://127.0.0.1:${server.address().port}`;

console.log("1:", (await new Sitemapper({
  url: `${base}/img.xml`,
  fields: { loc: true, "image:loc": true, "image:title": true },
}).fetch()).sites);

console.log("2:", await new Sitemapper({ url: `${base}/empty.xml` }).fetch());

console.log("3:", (await new Sitemapper({
  url: `${base}/ex.xml`,
  exclusions: [/foo/g],
}).fetch()).sites);

server.close();

Actual output:

1: [ { loc: 'http://e.com/i' } ]
2: {
  url: 'http://127.0.0.1:33777/empty.xml',
  sites: [],
  errors: [
    {
      url: 'http://127.0.0.1:33777/empty.xml',
      type: 'UnknownStateError',
      message: 'An unknown error occurred.',
      retries: 0
    }
  ]
}
3: [ 'http://e.com/foo/2', 'http://e.com/foo/4', 'http://e.com/bar' ]

Expected output:

  1. [{ loc: 'http://e.com/i', 'image:loc': 'http://e.com/1.png', 'image:title': 'Photo' }] (or arrays when a <url> has multiple <image:image> elements), and likewise for image:caption, video:title, video:description, and video:thumbnail_loc listed in README.md and sitemapper.d.ts.
  2. { url: 'http://127.0.0.1:33777/empty.xml', sites: [], errors: [] } (issue Handling an empty sitemap file #23 previously fixed empty sitemaps, and additional-coverage.test.ts expects sites: []).
  3. [ 'http://e.com/bar' ].

In src/assets/sitemapper.js, XMLParser runs with removeNSPrefix: true so image/video entries land at site.image.{loc,title,caption} and site.video.{title,description,thumbnail_loc} while crawl() checks site[field]; fast-xml-parser parses an empty <urlset> to urlset: "" which fails data.urlset && data.urlset.url (and retries retries times); and isExcluded() calls pattern.test(url) directly without resetting lastIndex.

Versions: sitemapper@4.1.6 (latest on npm) and master (76eac0b), Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions