Created
August 17, 2024 08:24
-
-
Save azdanov/e1b5b0d108f30b0e24cf11f543e2b67c to your computer and use it in GitHub Desktop.
Node.js sitemap.xml link checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require('axios'); | |
| const xml2js = require('xml2js'); | |
| const { promisify } = require('util'); | |
| const parseXml = promisify(xml2js.parseString); | |
| async function checkUrl(url) { | |
| try { | |
| const response = await axios.head(url, { timeout: 5000 }); | |
| return { url, status: response.status }; | |
| } catch (error) { | |
| return { url, status: error.response ? error.response.status : 'Error' }; | |
| } | |
| } | |
| function delay(ms) { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| async function validateSitemap(sitemapUrl) { | |
| try { | |
| const response = await axios.get(sitemapUrl); | |
| const result = await parseXml(response.data); | |
| const urls = result.urlset.url.map(item => item.loc[0]); | |
| for (const url of urls) { | |
| const { status } = await checkUrl(url); | |
| if (status !== 200) { | |
| console.log(`Broken link: ${url} (Status: ${status})`); | |
| } else { | |
| console.log(`Working link: ${url}`); | |
| } | |
| await delay(500); // 500ms delay between requests | |
| } | |
| } catch (error) { | |
| console.error('Error validating sitemap:', error.message); | |
| } | |
| } | |
| // Usage | |
| const sitemapUrl = 'https://azdanov.dev/sitemap.xml'; | |
| validateSitemap(sitemapUrl); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: