From 61cb387144063480fb442da5d92eabf86d943b94 Mon Sep 17 00:00:00 2001 From: paraschi Date: Thu, 22 Aug 2024 21:11:10 +0300 Subject: [PATCH] fix: same domain check with www for baseUrl --- src/canonical/handler.js | 2 +- test/audits/canonical.test.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/canonical/handler.js b/src/canonical/handler.js index 8888c735..34b75b70 100644 --- a/src/canonical/handler.js +++ b/src/canonical/handler.js @@ -352,7 +352,7 @@ export function validateCanonicalFormat(canonicalUrl, baseUrl, log) { } // Check if the canonical URL has the same domain as the base URL - if (url.hostname !== base.hostname) { + if (url.hostname !== base.hostname && url.hostname !== `www.${base.hostname}` && `www.${url.hostname}` !== base.hostname) { checks.push({ check: CANONICAL_CHECKS.CANONICAL_URL_SAME_DOMAIN.check, success: false, diff --git a/test/audits/canonical.test.js b/test/audits/canonical.test.js index d80f64a1..1ec8be4c 100644 --- a/test/audits/canonical.test.js +++ b/test/audits/canonical.test.js @@ -325,6 +325,28 @@ describe('Canonical URL Tests', () => { expect(log.info).to.have.been.calledWith('Canonical URL https://example.com uses a different protocol than base URL http://example.com'); }); + it('should pass when canonical URL and base URL are identical, including the www prefix', () => { + const canonicalUrl = 'https://www.example.com'; + const baseUrl = 'https://example.com'; + const result = validateCanonicalFormat(canonicalUrl, baseUrl, log); + + expect(result).to.deep.include({ + check: 'canonical-url-same-domain', + success: true, + }); + }); + + it('should pass when canonical URL and base URL are identical, including the www prefix', () => { + const canonicalUrl = 'https://example.com'; + const baseUrl = 'https://www.example.com'; + const result = validateCanonicalFormat(canonicalUrl, baseUrl, log); + + expect(result).to.deep.include({ + check: 'canonical-url-same-domain', + success: true, + }); + }); + it('should fail if the canonical URL is not absolute', () => { const canonicalUrl = '/relative/url'; const baseUrl = 'http://example.com';