Skip to content

Commit

Permalink
fix: same domain check with www for baseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiAlexandruParaschiv committed Aug 22, 2024
1 parent 86e0b05 commit 61cb387
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/canonical/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions test/audits/canonical.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 61cb387

Please sign in to comment.