ap-ssg
Version:
A fast, modular, SEO-optimized static site generator that minifies CSS, JS, and HTML for improved performance. It also supports JSON-LD, sitemap generation, and more, making it ideal for production-ready websites.
22 lines (17 loc) • 559 B
JavaScript
function makeCanonicalUrl(inputUrl) {
try {
const url = new URL(inputUrl);
let pathname = url.pathname;
// Remove /index.html (case-insensitive)
pathname = pathname.replace(/\/index\.html$/i, "");
// Remove trailing slash if not root
if (pathname !== "/" && pathname.endsWith("/")) {
pathname = pathname.slice(0, -1);
}
// Return canonicalized full URL
return `${url.origin}${pathname}`;
} catch (e) {
throw new Error("Invalid URL provided");
}
}
module.exports = makeCanonicalUrl;