Skip to content

nickFalcone/parse-sitemap

Repository files navigation

NPM info NPM bundle

A tiny zero-dependency function to parse an XML sitemap and return an array of URL objects.

Use

Import the parseXmlSitemap module directly, or use a module bundler of your choice.

Then, in an async function, await parseXmlSitemap('./path-to/sitemap.xml') supplying the path to your sitemap file:

import parseXmlSitemap from 'https://unpkg.com/@nfalcone/parse-xmlsitemap@0.2.0/index.min.js';

(async () => {
  const ul = document.getElementById('sitemapList');
  const urls = await parseXmlSitemap('./path-to/sitemap.xml'); // despite the warning, await is needed here.
})();

In the example above, urls is an array of URL objects. Each URL object contains properties including href.

Assuming an <ul id="sitemapList"></ul>, we can add list items for our site's URLs:

import parseXmlSitemap from 'https://unpkg.com/@nfalcone/parse-xmlsitemap@0.2.0/index.min.js';

(async () => {
  const ul = document.getElementById('sitemapList');
  const urls = await parseXmlSitemap('./sitemap.xml'); // despite the warning, await is needed here.

  urls.forEach((url) => {
    const li = document.createElement('li');
    li.innerText = url.href;
    ul.appendChild(li);
  });
})();

Cross origin note

If the sitemap.xml file is hosted on a different origin, proper CORS headers should be in place.

Develop

$ git clone git@github.com:nickFalcone/parse-sitemap.git
$ cd parse-sitemap/
$ npm install
$ http-server # runs the app.js demo

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published