Skip to content

Commit df2c83a

Browse files
- guardian.switch should be immutable
- added check if Commercial is handling Admiral - updated logging to use 'dotcom' logger
1 parent e9dc479 commit df2c83a

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

dotcom-rendering/src/components/AdmiralScript.importable.tsx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,28 @@ const setUpAdmiralEventLogger = (
198198
});
199199
};
200200

201+
// Check if Commercial has already initialized Admiral (bootstrap loaded, not just stub)
202+
const isComHandlingAdmiral = (): boolean => {
203+
// If window.admiral exists and has been initialized by bootstrap (not just the queue stub)
204+
// the bootstrap replaces the stub with a proper function that doesn't have .q property
205+
type AdmiralStub = Admiral & { q?: any[] };
206+
const w = window as Window & { admiral?: AdmiralStub };
207+
208+
const admiralExists = typeof w.admiral === 'function';
209+
const admiralAsRecord = w.admiral as unknown as Record<string, unknown>;
210+
const admiralIsOnlyStub = admiralExists && Array.isArray(admiralAsRecord.q);
211+
const admiralIsInitialized = admiralExists && !admiralIsOnlyStub;
212+
213+
if (admiralIsInitialized) {
214+
log(
215+
'dotcom',
216+
'🛡️ Admiral - Commercial is handling Admiral, skipping commercial initialization',
217+
);
218+
return true;
219+
}
220+
return false;
221+
};
222+
201223
export const AdmiralScript = () => {
202224
const ab = useAB();
203225
const abTestVariant = getAdmiralAbTestVariant(ab);
@@ -218,6 +240,7 @@ export const AdmiralScript = () => {
218240
const page = window.guardian.config.page;
219241

220242
const shouldRun =
243+
!isComHandlingAdmiral() &&
221244
cmp.hasInitialised() &&
222245
!cmp.willShowPrivacyMessageSync() &&
223246
isInUsa() &&
@@ -246,7 +269,7 @@ export const AdmiralScript = () => {
246269
recordAdmiralOphanEvent(ab, { action: 'INSERT' });
247270

248271
// Initialize Admiral Adblock Recovery
249-
log('commercial', '🛡️ Initialising Admiral Adblock Recovery');
272+
log('dotcom', '🛡️ Initialising Admiral Adblock Recovery');
250273

251274
// Set up window.admiral stub
252275
// This initializes admiral before the bootstrap script loads
@@ -263,7 +286,7 @@ export const AdmiralScript = () => {
263286
w.admiral = stub;
264287
}
265288

266-
log('commercial', '🛡️ Setting up Admiral event logger');
289+
log('dotcom', '🛡️ Setting up Admiral event logger');
267290

268291
// Set up Admiral event logging
269292
setUpAdmiralEventLogger(w.admiral, ab);
@@ -273,9 +296,6 @@ export const AdmiralScript = () => {
273296
w.admiral('targeting', 'set', 'guAbTest', abTestVariant);
274297
}
275298

276-
// Mark that DCR owns Admiral initialization
277-
window.guardian.config.switches.dcrOwnsAdmiral = true;
278-
279299
// Load Admiral bootstrap script
280300
const BASE_AJAX_URL =
281301
window.guardian.config.stage === 'CODE'
@@ -287,12 +307,7 @@ export const AdmiralScript = () => {
287307
admiralScript.async = true;
288308
document.head.appendChild(admiralScript);
289309

290-
log(
291-
'commercial',
292-
`🛡️ Loading Admiral bootstrap script: ${admiralScript.src}`,
293-
);
294-
295-
log('commercial', '🛡️ Admiral initialization complete');
310+
log('dotcom', '🛡️ Admiral initialization complete');
296311

297312
return () => {
298313
// Clean up Admiral bootstrap script

0 commit comments

Comments
 (0)