Skip to content

Commit bcd89e0

Browse files
Fix OneTrust cookie banner not persisting on cbl-reactnative.dev.
The Couchbase OneTrust script is registered for couchbase.com, so consent cookies were never written on this domain and the banner reappeared on every refresh.
1 parent 2fd64fd commit bcd89e0

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

docusaurus.config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,26 @@ const config = {
2525
onBrokenLinks: 'warn',
2626
onBrokenMarkdownLinks: 'warn',
2727

28-
// OneTrust Cookies Consent Notice for couchbase.com
28+
// OneTrust cookie consent (Couchbase script; see static/js/onetrust-wrapper.js
29+
// for cbl-reactnative.dev persistence workaround).
2930
headTags: [
3031
{
3132
tagName: 'script',
3233
attributes: {
3334
src: 'https://cdn.cookielaw.org/scripttemplates/otSDKStub.js',
3435
type: 'text/javascript',
3536
charset: 'UTF-8',
36-
'data-domain-script': '748511ff-10bf-44bf-88b8-36382e5b5fd9',
37+
'data-domain-script':
38+
process.env.ONETRUST_DOMAIN_SCRIPT ||
39+
'748511ff-10bf-44bf-88b8-36382e5b5fd9',
3740
},
3841
},
3942
{
4043
tagName: 'script',
4144
attributes: {
45+
src: '/js/onetrust-wrapper.js',
4246
type: 'text/javascript',
4347
},
44-
innerHTML: 'function OptanonWrapper() {}',
4548
},
4649
{
4750
tagName: 'script',

static/js/onetrust-wrapper.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* OneTrust callback for cbl-reactnative.dev.
3+
*
4+
* The Couchbase OneTrust production script (748511ff-…) is registered for
5+
* couchbase.com, so OneTrust cannot write OptanonAlertBoxClosed on this
6+
* domain. Persist consent cookies on the current host when the SDK records
7+
* a choice but fails to store them.
8+
*/
9+
function OptanonWrapper() {
10+
if (typeof OneTrust === 'undefined') {
11+
return;
12+
}
13+
14+
var host = window.location.hostname;
15+
var isCouchbaseDomain =
16+
host === 'couchbase.com' || host.endsWith('.couchbase.com');
17+
18+
function hasConsentGroups() {
19+
return (
20+
typeof OptanonActiveGroups !== 'undefined' &&
21+
OptanonActiveGroups.replace(/,/g, '').length > 0
22+
);
23+
}
24+
25+
function hasAlertCookie() {
26+
return document.cookie.indexOf('OptanonAlertBoxClosed=') !== -1;
27+
}
28+
29+
function persistConsentCookies() {
30+
if (isCouchbaseDomain || hasAlertCookie()) {
31+
return;
32+
}
33+
34+
if (!hasConsentGroups() && OneTrust.IsAlertBoxClosed && OneTrust.IsAlertBoxClosed()) {
35+
return;
36+
}
37+
38+
if (!hasConsentGroups()) {
39+
return;
40+
}
41+
42+
var expiry = new Date();
43+
expiry.setFullYear(expiry.getFullYear() + 1);
44+
var expiryStr = 'expires=' + expiry.toUTCString();
45+
var secure = window.location.protocol === 'https:' ? '; Secure' : '';
46+
47+
document.cookie =
48+
'OptanonAlertBoxClosed=' +
49+
new Date().toISOString() +
50+
'; path=/; ' +
51+
expiryStr +
52+
'; SameSite=Lax' +
53+
secure;
54+
55+
document.cookie =
56+
'OptanonConsent=' +
57+
'isGpcEnabled=0&datestamp=' +
58+
encodeURIComponent(new Date().toString()) +
59+
'&version=202504.1.0&groups=' +
60+
encodeURIComponent(OptanonActiveGroups) +
61+
'&hosts=&consentId=&interactionCount=1&landingPath=NotLandingPage&AwaitingReconsent=false' +
62+
'; path=/; ' +
63+
expiryStr +
64+
'; SameSite=Lax' +
65+
secure;
66+
}
67+
68+
OneTrust.OnConsentChanged(persistConsentCookies);
69+
persistConsentCookies();
70+
}

0 commit comments

Comments
 (0)