Skip to content

Commit c94eb4e

Browse files
Geodge RTD module: update preload mechanism (#10911)
* Update preload mechanism to work with an iframe * Update tests --------- Co-authored-by: daniel manan <[email protected]>
1 parent 74f6bce commit c94eb4e

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

modules/geoedgeRtdProvider.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import { submodule } from '../src/hook.js';
1919
import { ajax } from '../src/ajax.js';
20-
import { generateUUID, insertElement, isEmpty, logError } from '../src/utils.js';
20+
import { generateUUID, createInvisibleIframe, insertElement, isEmpty, logError } from '../src/utils.js';
2121
import * as events from '../src/events.js';
2222
import CONSTANTS from '../src/constants.json';
2323
import { loadExternalScript } from '../src/adloader.js';
2424
import { auctionManager } from '../src/auctionManager.js';
25+
import { getRefererInfo } from '../src/refererDetection.js';
2526

2627
/** @type {string} */
2728
const SUBMODULE_NAME = 'geoedge';
@@ -69,17 +70,38 @@ export function setWrapper(responseText) {
6970
wrapper = responseText;
7071
}
7172

73+
export function getInitialParams(key) {
74+
let refererInfo = getRefererInfo();
75+
let params = {
76+
wver: 'pbjs',
77+
wtype: 'pbjs-module',
78+
key,
79+
meta: {
80+
topUrl: refererInfo.page
81+
},
82+
site: refererInfo.domain,
83+
pimp: PV_ID,
84+
fsRan: true,
85+
frameApi: true
86+
};
87+
return params;
88+
}
89+
90+
export function markAsLoaded() {
91+
preloaded = true;
92+
}
93+
7294
/**
7395
* preloads the client
7496
* @param {string} key
7597
*/
7698
export function preloadClient(key) {
77-
let link = document.createElement('link');
78-
link.rel = 'preload';
79-
link.as = 'script';
80-
link.href = getClientUrl(key);
81-
link.onload = () => { preloaded = true };
82-
insertElement(link);
99+
let iframe = createInvisibleIframe();
100+
iframe.id = 'grumiFrame';
101+
insertElement(iframe);
102+
iframe.contentWindow.grumi = getInitialParams(key);
103+
let url = getClientUrl(key);
104+
loadExternalScript(url, SUBMODULE_NAME, markAsLoaded, iframe.contentDocument);
83105
}
84106

85107
/**

test/spec/modules/geoedgeRtdProvider_spec.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import * as utils from '../../../src/utils.js';
22
import {loadExternalScript} from '../../../src/adloader.js';
3-
import {
3+
import * as geoedgeRtdModule from '../../../modules/geoedgeRtdProvider.js';
4+
import {server} from '../../../test/mocks/xhr.js';
5+
import * as events from '../../../src/events.js';
6+
import CONSTANTS from '../../../src/constants.json';
7+
8+
let {
49
geoedgeSubmodule,
510
getClientUrl,
611
getInPageUrl,
712
htmlPlaceholder,
813
setWrapper,
914
getMacros,
10-
wrapper,
11-
WRAPPER_URL
12-
} from '../../../modules/geoedgeRtdProvider.js';
13-
import {server} from '../../../test/mocks/xhr.js';
14-
import * as events from '../../../src/events.js';
15-
import CONSTANTS from '../../../src/constants.json';
15+
WRAPPER_URL,
16+
preloadClient,
17+
markAsLoaded
18+
} = geoedgeRtdModule;
1619

1720
let key = '123123123';
1821
function makeConfig(gpt) {
@@ -65,13 +68,11 @@ describe('Geoedge RTD module', function () {
6568
});
6669
});
6770
describe('init', function () {
68-
let insertElementStub;
69-
7071
before(function () {
71-
insertElementStub = sinon.stub(utils, 'insertElement');
72+
sinon.spy(geoedgeRtdModule, 'preloadClient');
7273
});
7374
after(function () {
74-
utils.insertElement.restore();
75+
geoedgeRtdModule.preloadClient.restore();
7576
});
7677
it('should return false when missing params or key', function () {
7778
let missingParams = geoedgeSubmodule.init({});
@@ -87,14 +88,13 @@ describe('Geoedge RTD module', function () {
8788
let isWrapperRequest = request && request.url && request.url && request.url === WRAPPER_URL;
8889
expect(isWrapperRequest).to.equal(true);
8990
});
90-
it('should preload the client', function () {
91-
let isLinkPreloadAsScript = arg => arg.tagName === 'LINK' && arg.rel === 'preload' && arg.as === 'script' && arg.href === getClientUrl(key);
92-
expect(insertElementStub.calledWith(sinon.match(isLinkPreloadAsScript))).to.equal(true);
91+
it('should call preloadClient', function () {
92+
expect(preloadClient.called);
9393
});
9494
it('should emit billable events with applicable winning bids', function (done) {
9595
let counter = 0;
9696
events.on(CONSTANTS.EVENTS.BILLABLE_EVENT, function (event) {
97-
if (event.vendor === 'geoedge' && event.type === 'impression') {
97+
if (event.vendor === geoedgeSubmodule.name && event.type === 'impression') {
9898
counter += 1;
9999
}
100100
expect(counter).to.equal(1);
@@ -104,18 +104,35 @@ describe('Geoedge RTD module', function () {
104104
});
105105
it('should load the in page code when gpt params is true', function () {
106106
geoedgeSubmodule.init(makeConfig(true));
107-
let isInPageUrl = arg => arg == getInPageUrl(key);
107+
let isInPageUrl = arg => arg === getInPageUrl(key);
108108
expect(loadExternalScript.calledWith(sinon.match(isInPageUrl))).to.equal(true);
109109
});
110110
it('should set the window.grumi config object when gpt params is true', function () {
111111
let hasGrumiObj = typeof window.grumi === 'object';
112112
expect(hasGrumiObj && window.grumi.key === key && window.grumi.fromPrebid).to.equal(true);
113113
});
114114
});
115+
describe('preloadClient', function () {
116+
let iframe;
117+
preloadClient(key);
118+
let loadExternalScriptCall = loadExternalScript.getCall(0);
119+
it('should create an invisible iframe and insert it to the DOM', function () {
120+
iframe = document.getElementById('grumiFrame');
121+
expect(iframe && iframe.style.display === 'none');
122+
});
123+
it('should assign params object to the iframe\'s window', function () {
124+
let grumi = iframe.contentWindow.grumi;
125+
expect(grumi.key).to.equal(key);
126+
});
127+
it('should preload the client into the iframe', function () {
128+
let isClientUrl = arg => arg === getClientUrl(key);
129+
expect(loadExternalScriptCall.calledWithMatch(isClientUrl)).to.equal(true);
130+
});
131+
});
115132
describe('setWrapper', function () {
116133
it('should set the wrapper', function () {
117134
setWrapper(mockWrapper);
118-
expect(wrapper).to.equal(mockWrapper);
135+
expect(geoedgeRtdModule.wrapper).to.equal(mockWrapper);
119136
});
120137
});
121138
describe('getMacros', function () {

0 commit comments

Comments
 (0)