Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: sitemap tool

Blue Mouse7a86561058a7
diff --git a/web/app/routes/tools.sitemap-validator.tsx b/web/app/routes/tools.sitemap-validator.tsx
index 087d34444..3dd300523 100644
--- a/web/app/routes/tools.sitemap-validator.tsx
+++ b/web/app/routes/tools.sitemap-validator.tsx
@@ -8,6 +8,7 @@ import {
XCircleIcon,
InformationCircleIcon,
} from '@heroicons/react/24/solid'
+import { XMLParser, XMLValidator } from 'fast-xml-parser'
import { useState } from 'react'
import { redirect, useFetcher } from 'react-router'
import type { SitemapFunction } from 'remix-sitemap'
@@ -93,22 +94,41 @@ function isValidUrl(url: string): boolean {
}
}
-function parseXml(xmlString: string): Document | null {
- try {
- const parser = new DOMParser()
- const doc = parser.parseFromString(xmlString, 'application/xml')
+function parseXml(xmlString: string): Record<string, unknown> | null {
+ const validationResult = XMLValidator.validate(xmlString)
+ if (validationResult !== true) {
+ return null
+ }
- const parseError = doc.querySelector('parsererror')
- if (parseError) {
- return null
- }
+ try {
+ const parser = new XMLParser({
+ ignoreAttributes: true,
+ ignoreDeclaration: true,
+ trimValues: true,
+ })
- return doc
+ return parser.parse(xmlString)
} catch {
return null
}
}
+function normalizeArray<T>(value: T | T[] | undefined): T[] {
+ if (!value) return []
+ return Array.isArray(value) ? value : [value]
+}
+
+function getText(value: unknown): string | undefined {
+ if (typeof value === 'string') return value.trim()
+ if (typeof value === 'number') return String(value)
+ if (value && typeof value === 'object' && '#text' in value) {
+ const textValue = (value as { '#text'?: unknown })['#text']
+ if (typeof textValue === 'string') return textValue.trim()
+ if (typeof textValue === 'number') return String(textValue)
+ }
+ return undefined
+}
+
export async function action({ request }: { request: Request }) {
const formData = await request.formData()
const sitemapUrl = formData.get('url') as string | null
@@ -184,9 +204,9 @@ export async function action({ request }: { request: Request }) {
})
}
- const doc = parseXml(xmlContent)
+ const parsedXml = parseXml(xmlContent)
- if (!doc) {
+ if (!parsedXml) {
issues.push({
type: 'error',
message: 'Invalid XML structure',
@@ -211,8 +231,9 @@ export async function action({ request }: { request: Request }) {
}
}
- const sitemapIndex = doc.querySelector('sitemapindex')
- const urlset = doc.querySelector('urlset')
+ const sitemapIndex =
+ (parsedXml as { sitemapindex?: unknown }).sitemapindex ?? null
+ const urlset = (parsedXml as { urlset?: unknown }).urlset ?? null
if (!sitemapIndex && !urlset) {
issues.push({
@@ -242,9 +263,15 @@ export async function action({ request }: { request: Request }) {
let urls: SitemapUrl[] = []
let sitemaps: string[] = []
+ let urlCount = 0
if (sitemapIndex) {
- const sitemapElements = doc.querySelectorAll('sitemap')
+ const sitemapElements = normalizeArray(
+ (sitemapIndex as { sitemap?: unknown }).sitemap as
+ | Record<string, unknown>
+ | Record<string, unknown>[]
+ | undefined,
+ )
if (sitemapElements.length === 0) {
issues.push({
@@ -256,7 +283,9 @@ export async function action({ request }: { request: Request }) {
}
sitemapElements.forEach((sitemap, index) => {
- const loc = sitemap.querySelector('loc')?.textContent?.trim()
+ const loc = getText(
+ (sitemap as { loc?: unknown; '#text'?: unknown }).loc,
+ )
if (!loc) {
issues.push({
@@ -294,8 +323,14 @@ export async function action({ request }: { request: Request }) {
}
if (urlset) {
- const urlElements = doc.querySelectorAll('url')
+ const urlElements = normalizeArray(
+ (urlset as { url?: unknown }).url as
+ | Record<string, unknown>
+ | Record<string, unknown>[]
+ | undefined,
+ )
const seenUrls = new Set<string>()
+ urlCount = urlElements.length
if (urlElements.length === 0) {
issues.push({
@@ -314,14 +349,14 @@ export async function action({ request }: { request: Request }) {
}
urlElements.forEach((urlElement, index) => {
- const loc = urlElement.querySelector('loc')?.textContent?.trim()
- const lastmod = urlElement.querySelector('lastmod')?.textContent?.trim()
- const changefreq = urlElement
- .querySelector('changefreq')
- ?.textContent?.trim()
- const priority = urlElement
- .querySelector('priority')
- ?.textContent?.trim()
+ const loc = getText((urlElement as { loc?: unknown }).loc)
+ const lastmod = getText((urlElement as { lastmod?: unknown }).lastmod)
+ const changefreq = getText(
+ (urlElement as { changefreq?: unknown }).changefreq,
+ )
+ const priority = getText(
+ (urlElement as { priority?: unknown }).priority,
+ )
if (!loc) {
issues.push({
@@ -433,7 +468,7 @@ export async function action({ request }: { request: Request }) {
isValid: !hasErrors,
url: normalizedUrl,
encoding,
- urlCount: urlset ? doc.querySelectorAll('url').length : 0,
+ urlCount,
issues,
urls,
sitemapIndex: !!sitemapIndex,
@@ -737,11 +772,12 @@ export default function SitemapValidator() {
>
<IssueIcon type={issue.type} />
<div className='min-w-0 flex-1'>
- <Text weight='medium' className='text-sm'>
+ <Text as='p' weight='medium' className='text-sm'>
{issue.message}
</Text>
{issue.details && (
<Text
+ as='p'
size='sm'
colour='muted'
className='mt-0.5 break-all'
diff --git a/web/package-lock.json b/web/package-lock.json
index 70342473c..32594113b 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -4,7 +4,6 @@
"requires": true,
"packages": {
"": {
- "name": "web",
"license": "AGPL-3.0",
"dependencies": {
"@headlessui/react": "^2.2.9",
@@ -25,6 +24,7 @@
"d3": "^7.9.0",
"d3-scale": "^4.0.2",
"dayjs": "^1.11.19",
+ "fast-xml-parser": "^5.3.3",
"file-saver": "^2.0.5",
"framer-motion": "^12.29.0",
"geojson": "^0.5.0",
@@ -5070,9 +5070,9 @@
}
},
"node_modules/fast-xml-parser": {
- "version": "4.5.3",
- "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.5.3.tgz",
- "integrity": "sha512-RKihhV+SHsIUGXObeVy9AXiBbFwkVk7Syp8XgwN5U3JV416+Gwp/GO9i0JYKmikykgz/UHRrrV4ROuZEo/T0ig==",
+ "version": "5.3.3",
+ "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-5.3.3.tgz",
+ "integrity": "sha512-2O3dkPAAC6JavuMm8+4+pgTk+5hoAs+CjZ+sWcQLkX9+/tHRuTkQh/Oaifr8qDmZ8iEHb771Ea6G8CdwkrgvYA==",
"funding": [
{
"type": "github",
@@ -5081,7 +5081,7 @@
],
"license": "MIT",
"dependencies": {
- "strnum": "^1.1.1"
+ "strnum": "^2.1.0"
},
"bin": {
"fxparser": "src/cli/cli.js"
@@ -7014,6 +7014,36 @@
"@remix-run/server-runtime": "^2.0.0"
}
},
+ "node_modules/remix-sitemap/node_modules/fast-xml-parser": {
+ "version": "4.5.3",
+ "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.5.3.tgz",
+ "integrity": "sha512-RKihhV+SHsIUGXObeVy9AXiBbFwkVk7Syp8XgwN5U3JV416+Gwp/GO9i0JYKmikykgz/UHRrrV4ROuZEo/T0ig==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/NaturalIntelligence"
+ }
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "strnum": "^1.1.1"
+ },
+ "bin": {
+ "fxparser": "src/cli/cli.js"
+ }
+ },
+ "node_modules/remix-sitemap/node_modules/strnum": {
+ "version": "1.1.2",
+ "resolved": "https://registry.npmjs.org/strnum/-/strnum-1.1.2.tgz",
+ "integrity": "sha512-vrN+B7DBIoTTZjnPNewwhx6cBA/H+IS7rfW68n7XxC1y7uoiGQBxaKzqucGUgavX15dJgiGztLJ8vxuEzwqBdA==",
+ "funding": [
+ {
+ "type": "github",
+ "url": "https://github.com/sponsors/NaturalIntelligence"
+ }
+ ],
+ "license": "MIT"
+ },
"node_modules/remix-utils": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/remix-utils/-/remix-utils-9.0.0.tgz",
@@ -7499,9 +7529,9 @@
}
},
"node_modules/strnum": {
- "version": "1.1.2",
- "resolved": "https://registry.npmjs.org/strnum/-/strnum-1.1.2.tgz",
- "integrity": "sha512-vrN+B7DBIoTTZjnPNewwhx6cBA/H+IS7rfW68n7XxC1y7uoiGQBxaKzqucGUgavX15dJgiGztLJ8vxuEzwqBdA==",
+ "version": "2.1.2",
+ "resolved": "https://registry.npmjs.org/strnum/-/strnum-2.1.2.tgz",
+ "integrity": "sha512-l63NF9y/cLROq/yqKXSLtcMeeyOfnSQlfMSlzFt/K73oIaD8DGaQWd7Z34X9GPiKqP5rbSh84Hl4bOlLcjiSrQ==",
"funding": [
{
"type": "github",
diff --git a/web/package.json b/web/package.json
index c59dbe7ad..cc57017b3 100644
--- a/web/package.json
+++ b/web/package.json
@@ -36,6 +36,7 @@
"d3": "^7.9.0",
"d3-scale": "^4.0.2",
"dayjs": "^1.11.19",
+ "fast-xml-parser": "^5.3.3",
"file-saver": "^2.0.5",
"framer-motion": "^12.29.0",
"geojson": "^0.5.0",