Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PBjs Core: override refererInfo.canonicalUrl if pageUrl is defined in config #6591

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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