diff --git a/src/canonical/handler.js b/src/canonical/handler.js
index 8888c735..2697f02a 100644
--- a/src/canonical/handler.js
+++ b/src/canonical/handler.js
@@ -11,6 +11,7 @@
*/
import { JSDOM } from 'jsdom';
+import { composeBaseURL } from '@adobe/spacecat-shared-utils';
import { fetch } from '../support/utils.js';
import { AuditBuilder } from '../common/audit-builder.js';
import { noopUrlResolver } from '../common/audit.js';
@@ -352,7 +353,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 (composeBaseURL(url.hostname) !== composeBaseURL(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..3b2dc7dd 100644
--- a/test/audits/canonical.test.js
+++ b/test/audits/canonical.test.js
@@ -98,7 +98,7 @@ describe('Canonical URL Tests', () => {
describe('validateCanonicalTag', () => {
it('should handle missing canonical tag', async () => {
const url = 'http://example.com';
- const html = '
';
+ const html = 'test';
nock('http://example.com').get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -153,7 +153,7 @@ describe('Canonical URL Tests', () => {
it('should handle invalid canonical URL correctly', async () => {
const url = 'http://example.com';
- const html = '';
+ const html = 'test';
nock(url).get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -168,7 +168,7 @@ describe('Canonical URL Tests', () => {
it('should handle empty canonical tag', async () => {
const url = 'http://example.com';
- const html = '';
+ const html = 'test';
nock(url).get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -184,7 +184,7 @@ describe('Canonical URL Tests', () => {
it('should handle multiple canonical tags', async () => {
const url = 'http://example.com';
- const html = '';
+ const html = 'test';
nock(url).get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -198,7 +198,7 @@ describe('Canonical URL Tests', () => {
it('should fail if the canonical tag is not in the head section', async () => {
const url = 'http://example.com';
- const html = '';
+ const html = 'test';
nock(url).get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -325,6 +325,22 @@ 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, regardless of the www prefix', () => {
+ const cases = [
+ { canonicalUrl: 'https://www.example.com', baseUrl: 'https://example.com' },
+ { canonicalUrl: 'https://example.com', baseUrl: 'https://www.example.com' },
+ ];
+
+ cases.forEach(({ canonicalUrl, baseUrl }) => {
+ 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';
@@ -340,7 +356,7 @@ describe('Canonical URL Tests', () => {
it('should pass if the canonical URL points to itself', async () => {
const url = 'http://example.com';
- const html = ``;
+ const html = `test`;
nock(url).get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -380,7 +396,7 @@ describe('Canonical URL Tests', () => {
it('should fail if the canonical URL does not point to itself', async () => {
const url = 'http://example.com';
const canonicalUrl = 'http://example.com/other-page';
- const html = ``;
+ const html = `test`;
nock(url).get('/').reply(200, html);
const result = await validateCanonicalTag(url, log);
@@ -470,9 +486,9 @@ describe('Canonical URL Tests', () => {
const expectedCanonicalUrl = 'https://example.com/canonical-page';
const html = `
-
+
-
+ test
Test Page
@@ -518,7 +534,7 @@ describe('Canonical URL Tests', () => {
describe('canonicalAuditRunner', () => {
it('should run canonical audit successfully', async () => {
const baseURL = 'http://example.com';
- const html = ``;
+ const html = `test`;
nock('http://example.com').get('/page1').reply(200, html);
nock(baseURL).get('/').reply(200, html);