From 3369e21b40d4f1bb150b929ff2127961c14945d9 Mon Sep 17 00:00:00 2001 From: rkrom Date: Thu, 25 Mar 2021 13:01:35 +0200 Subject: [PATCH 1/3] add ssr support --- src/index.jsx | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/index.jsx b/src/index.jsx index c181f16..aca3b95 100755 --- a/src/index.jsx +++ b/src/index.jsx @@ -5,6 +5,23 @@ function removeEmpty(obj) { return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null)); } +function createOutbrainScript(scriptSrc) { + if (!window.OBR) { + const script = document.createElement('script'); + script.src = scriptSrc; + + document.head.appendChild(script); + + return script; + } +} + +function removeOutbrainScript(script) { + if (script) { + script.remove(); + } +} + const OutbrainWidget = (props) => { const { dataSrc = '', @@ -17,7 +34,8 @@ const OutbrainWidget = (props) => { isSecured = '', obContentUrl = null, obPortalUrl = null, - obBundleUrl = null + obBundleUrl = null, + scriptSrc = 'https://widgets.outbrain.com/outbrain.js' } = props; const attrs = removeEmpty({ @@ -34,6 +52,12 @@ const OutbrainWidget = (props) => { 'data-ob-bundleurl': obBundleUrl }); + useEffect(() => { + const script = createOutbrainScript(scriptSrc); + + return () => { removeOutbrainScript(script); } + }, [scriptSrc]); + useEffect(() => { const { OBR } = window; if (OBR && OBR.extern && typeof OBR.extern.renderSpaWidgets === 'function') { From 070c8f4cb30159a6fc44d63c62ddc8a6092ca337 Mon Sep 17 00:00:00 2001 From: rkrom Date: Sun, 25 Apr 2021 17:52:24 +0300 Subject: [PATCH 2/3] remove console.log --- src/index.jsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index 7293d21..4a8acea 100755 --- a/src/index.jsx +++ b/src/index.jsx @@ -42,8 +42,6 @@ const OutbrainWidget = (props) => { 'data-external-id': externalId }); - console.log('zagao', attrs); - useEffect(() => { const { OBR } = window; if (OBR && OBR.extern && typeof OBR.extern.renderSpaWidgets === 'function') { From de97912c800ab43046f01f960964710f29a7c66b Mon Sep 17 00:00:00 2001 From: rkrom Date: Sun, 25 Apr 2021 18:06:32 +0300 Subject: [PATCH 3/3] support abort insert ob script with obInjectScript --- src/index.jsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index.jsx b/src/index.jsx index fcb3b78..32f8e4f 100755 --- a/src/index.jsx +++ b/src/index.jsx @@ -35,11 +35,12 @@ const OutbrainWidget = (props) => { obContentUrl = null, obPortalUrl = null, obBundleUrl = null, - scriptSrc = 'https://widgets.outbrain.com/outbrain.js', obLanguage = null, obPsub = null, obAppId = null, - externalId = null + externalId = null, + obInjectScript = true, + scriptSrc = 'https://widgets.outbrain.com/outbrain.js' } = props; const attrs = removeEmpty({ @@ -61,10 +62,12 @@ const OutbrainWidget = (props) => { }); useEffect(() => { + if (!obInjectScript) { return; } + const script = createOutbrainScript(scriptSrc); return () => { removeOutbrainScript(script); } - }, [scriptSrc]); + }, [scriptSrc, obInjectScript]); useEffect(() => { const { OBR } = window;