A URL from the sitemap (absolute).
The decoded slug, or null when the URL isn't a root-level product.
slugFromUrl('https://sciencelab.com/isopropyl-alcohol-70-v-v/'); // "isopropyl-alcohol-70-v-v"
slugFromUrl('https://sciencelab.com/brands/acme/'); // null
export function slugFromUrl(url: string): string | null {
let path: string;
try {
path = new URL(url).pathname;
} catch {
return null;
}
const slug = path.replace(/^\/+|\/+$/g, '');
return slug && !slug.includes('/') ? decodeURIComponent(slug) : null;
}
Pulls the single-segment product slug out of a ScienceLab URL.
Product pages live at the site root (
https://sciencelab.com/<slug>/); anything nested (a category or CMS page) is rejected so the sitemap's non-product entries are skipped.