Skip to content

Commit 3521dd6

Browse files
fix: Trigger window._sp_.executeMessaging() after the pageshow event (#2196)
* Add event listener for pageshow * Add changeset * Check if event is persisted before triggering.
1 parent 520f2cb commit 3521dd6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

.changeset/curvy-seas-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@guardian/libs': minor
3+
---
4+
5+
Add pageshow event listener for pageshow event to ensure the Consent or Pay banner is shown after clicking the Browser's back button

libs/@guardian/libs/src/consent-management-platform/sourcepoint.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ export const init = (
362362
}
363363
});
364364

365+
// Handle back navigation to ensure CMP messaging is shown when needed
366+
window.addEventListener('pageshow', (event: PageTransitionEvent) => {
367+
// Only re-execute messaging if the page is being loaded from cache i.e. via back/forward navigation
368+
if (event.persisted) {
369+
window._sp_?.executeMessaging?.();
370+
}
371+
});
372+
365373
spLib.src = `${ENDPOINT}/unified/wrapperMessagingWithoutDetection.js`;
366374

367375
document.body.appendChild(spLib);

0 commit comments

Comments
 (0)