-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathboot.js
256 lines (216 loc) · 8.12 KB
/
boot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// es7 polyfills not provided by pollyfill.io
import 'core-js/modules/es.object.get-own-property-descriptors';
import domready from 'domready';
import { bootStandard } from 'bootstraps/standard/main';
import config from 'lib/config';
import { markTime } from 'lib/user-timing';
import { captureOphanInfo } from 'lib/capture-ophan-info';
import { reportError } from 'lib/report-error';
import { cmp, getLocale, loadScript, onConsentChange } from '@guardian/libs';
import { getCookie } from 'lib/cookies';
import { init as detectAdBlockers } from 'commercial/detect-adblock';
import ophan from 'ophan/ng';
import { isUserLoggedIn } from 'common/modules/identity/api';
import { allowRejectAll } from 'common/modules/userFeatures/cookies/allowRejectAll';
import { refreshUserBenefits } from 'common/modules/userFeatures/user-features';
// Let webpack know where to get files from
// __webpack_public_path__ is a special webpack variable
// https://webpack.js.org/guides/public-path/#set-value-on-the-fly
// eslint-disable-next-line camelcase,no-undef
__webpack_public_path__ = `${config.get('page.assetsPath')}javascripts/`;
// Debug preact in DEV
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-unused-expressions
import(/* webpackChunkName: "preact-debug" */ 'preact/debug');
}
// kick off the app
const go = () => {
domready(async () => {
/**
* Boot Standard
*
*/
markTime('standard boot');
bootStandard();
/**
* User Benefits API
*
*/
await refreshUserBenefits().catch((err) => {
reportError(err, { module: 'c-user-benefits' });
});
/**
* CMP
*
*/
// USNAT and TCFv2
const browserId = getCookie('bwid') || undefined;
const pageViewId = config.get('ophan.pageViewId');
const pubData = {
platform: 'next-gen',
browserId,
pageViewId,
};
// keep this in sync with CONSENT_TIMING in src/client/bootCmp.ts in dotcom-rendering
// mark: CONSENT_TIMING
let recordedConsentTime = false;
onConsentChange((consentState) => {
if (!recordedConsentTime) {
recordedConsentTime = true;
}
// ------------------------------------------------------
// ------------------------------------------------------
// Sending Consent Data to Ophan as its own record
/*
Date: Dec 2023
Author: Anna Voelker
We reproduce here the same code that we had developed for DCR:
https://github.com/guardian/dotcom-rendering/pull/9546
*/
if (!consentState) return;
const consentDetails = () => {
if (consentState.tcfv2) {
return {
consentJurisdiction: 'TCF',
consentUUID: getCookie({ name: 'consentUUID' }) ?? '',
consent: consentState.tcfv2.tcString,
};
}
if (consentState.usnat) {
const consentUUID =
getCookie({ name: 'usnatUUID' }) ??
getCookie({ name: 'ccpaUUID' });
return {
consentJurisdiction: 'USNAT',
consentUUID: consentUUID ?? '',
consent: consentState.usnat.doNotSell ? 'false' : 'true',
};
}
if (consentState.aus) {
return {
consentJurisdiction: 'AUS',
consentUUID: getCookie({ name: 'ccpaUUID' }) ?? '',
consent: consentState.aus.personalisedAdvertising
? 'true'
: 'false',
};
}
return {
consentJurisdiction: 'OTHER',
consentUUID: '',
consent: '',
};
};
// Register changes in consent state with Ophan
ophan.record(consentDetails());
// ------------------------------------------------------
});
const isUserSignedIn = await isUserLoggedIn();
const useNonAdvertisedList = allowRejectAll(isUserSignedIn);
cmp.init({
pubData,
country: await getLocale(),
isUserSignedIn,
useNonAdvertisedList,
});
/**
* Handle Ad blockers
*
*/
detectAdBlockers()
window.guardian.adBlockers.onDetect.push((adblockInUse) => {
if (!adblockInUse) return;
// For the moment we'll hide the top-above-nav slot if we detect that the user has ad blockers enabled
// in order to avoid showing them a large blank space.
// TODO improve shady pie to make better use of the slot.
document.querySelector('.top-banner-ad-container').style.display =
'none';
if (process.env.NODE_ENV !== 'production') {
const needsMessage =
adblockInUse && window.console && window.console.warn;
const message =
'Do you have an adblocker enabled? Commercial features might fail to run, or throw exceptions.';
if (needsMessage) {
window.console.warn(message);
}
}
});
/**
* Commercial boot promise
*
*/
const fetchCommercial = () => {
const noop = () => Promise.resolve({ bootCommercial: () => {} });
markTime('commercial request');
if (!config.get('page.isHosted', false)) {
return loadScript(config.get('page.commercialBundleUrl')).then(noop);
}
return import(
/* webpackChunkName: "commercial" */
'bootstraps/commercial-hosted-legacy'
);
};
/**
* Enhanced boot promise
*
*/
const fetchEnhanced = window.guardian.isEnhanced
? (markTime('enhanced request'),
import(/* webpackChunkName: "enhanced" */ 'bootstraps/enhanced/main'))
: Promise.resolve({ bootEnhanced: () => { } });
/**
* Boot commercial and enhanced
*
*/
Promise.all([
fetchCommercial().then(({ bootCommercial }) => {
markTime('commercial boot');
try {
return bootCommercial();
} catch (err) {
/**
* report sync errors in bootCommercial to
* Sentry with the commercial feature tag
* */
reportError(
err,
{
feature: 'commercial',
},
false
);
}
}),
fetchEnhanced.then(({ bootEnhanced }) => {
markTime('enhanced boot');
try {
return bootEnhanced();
} catch (err) {
/**
* report sync errors in bootEnhanced to
* Sentry with the enhanced feature tag
* */
reportError(
err,
{
feature: 'enhanced',
},
false
);
}
}),
]).then(() => {
if (document.readyState === 'complete') {
captureOphanInfo();
} else {
window.addEventListener('load', captureOphanInfo);
}
});
});
};
// make sure we've patched the env before running the app
if (window.guardian.polyfilled) {
go();
} else {
window.guardian.onPolyfilled = go;
}