Skip to content

Commit

Permalink
PBjs Core: override refererInfo.canonicalUrl if pageUrl is defined in…
Browse files Browse the repository at this point in the history
… config (#6591)

* Updated refererDetection logic for canonical url. First run getConfig('pageUrl')and if value exists, return value else run standard logic

* Added test for override from config
  • Loading branch information
mmoschovas authored Apr 21, 2021
1 parent 91edf7f commit b89421c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/refererDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import { logWarn } from './utils.js';
import { config } from './config.js';

/**
* @param {Window} win Window
Expand Down Expand Up @@ -41,6 +42,10 @@ export function detectReferer(win) {
* @returns {string|null}
*/
function getCanonicalUrl(doc) {
let pageURL = config.getConfig('pageUrl');

if (pageURL) return pageURL;

try {
const element = doc.querySelector("link[rel='canonical']");

Expand Down
25 changes: 25 additions & 0 deletions test/spec/refererDetection_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { detectReferer } from 'src/refererDetection.js';
import { config } from 'src/config.js';
import { expect } from 'chai';

/**
Expand Down Expand Up @@ -91,6 +92,10 @@ function buildWindowTree(urls, topReferrer = '', canonicalUrl = null, ancestorOr
describe('Referer detection', () => {
describe('Non cross-origin scenarios', () => {
describe('No iframes', () => {
afterEach(function () {
config.resetConfig();
});

it('Should return the current window location and no canonical URL', () => {
const testWindow = buildWindowTree(['https://example.com/some/page'], 'https://othersite.com/'),
result = detectReferer(testWindow)();
Expand Down Expand Up @@ -156,6 +161,26 @@ describe('Referer detection', () => {
canonicalUrl: 'https://example.com/canonical/page'
});
});

it('Should override canonical URL with config pageUrl', () => {
config.setConfig({'pageUrl': 'testUrl.com'});

const testWindow = buildWindowTree(['https://example.com/some/page', 'https://example.com/other/page', 'https://example.com/third/page'], 'https://othersite.com/', 'https://example.com/canonical/page'),
result = detectReferer(testWindow)();

expect(result).to.deep.equal({
referer: 'https://example.com/some/page',
reachedTop: true,
isAmp: false,
numIframes: 2,
stack: [
'https://example.com/some/page',
'https://example.com/other/page',
'https://example.com/third/page'
],
canonicalUrl: 'testUrl.com'
});
});
});
});

Expand Down

0 comments on commit b89421c

Please sign in to comment.