Skip to content

Commit

Permalink
native renderer: small size improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Dec 12, 2023
1 parent 709322e commit 393b90b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
17 changes: 9 additions & 8 deletions creative/renderers/native/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ export function getReplacer(adId, {assets = [], ortb, nativeKeys = {}}) {
};
}

function loadScript(url, win) {
function loadScript(url, doc) {
return new Promise((resolve, reject) => {
const script = win.document.createElement('script');
const script = doc.createElement('script');
script.onload = resolve;
script.onerror = reject;
script.src = url;
win.document.body.appendChild(script);
doc.body.appendChild(script);
});
}

export function getAdMarkup(adId, nativeData, replacer, win, load = loadScript) {
const {rendererUrl, assets, ortb, adTemplate} = nativeData;
const doc = win.document;
if (rendererUrl) {
return load(rendererUrl, win).then(() => {
return load(rendererUrl, doc).then(() => {
if (typeof win.renderAd !== 'function') {
throw new Error(`Renderer from '${rendererUrl}' does not define renderAd()`);
}
Expand All @@ -57,17 +58,17 @@ export function getAdMarkup(adId, nativeData, replacer, win, load = loadScript)
return win.renderAd(payload);
});
} else {
return Promise.resolve(replacer(adTemplate ?? win.document.body.innerHTML));
return Promise.resolve(replacer(adTemplate ?? doc.body.innerHTML));
}
}

export function render({adId, native}, {sendMessage}, win, getMarkup = getAdMarkup) {
const {head, body} = win.document;
const resize = () => sendMessage(MESSAGE_NATIVE, {
action: ACTION_RESIZE,
height: win.document.body.offsetHeight,
width: win.document.body.offsetWidth
height: body.offsetHeight,
width: body.offsetWidth
});
const {head, body} = win.document;
const replacer = getReplacer(adId, native);
head && (head.innerHTML = replacer(head.innerHTML));
return getMarkup(adId, native, replacer, win).then(markup => {
Expand Down
2 changes: 1 addition & 1 deletion libraries/creative-renderer-native/renderer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/spec/creative/nativeRenderer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Native creative renderer', () => {
loadScript = sinon.stub();
});
it('uses rendererUrl if present', () => {
win.document = {}
const data = {
assets: ['1', '2'],
ortb: 'ortb',
Expand All @@ -24,7 +25,7 @@ describe('Native creative renderer', () => {
}));
return getAdMarkup('123', data, null, win, loadScript).then((markup) => {
expect(markup).to.eql('markup');
sinon.assert.calledWith(loadScript, data.rendererUrl, win);
sinon.assert.calledWith(loadScript, data.rendererUrl, sinon.match(arg => arg ===win.document));
sinon.assert.calledWith(renderAd, sinon.match(arg => {
expect(arg).to.have.members(data.assets);
expect(arg.ortb).to.eql(data.ortb);
Expand Down

0 comments on commit 393b90b

Please sign in to comment.