Skip to content

Commit

Permalink
Test bfcache fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgibson committed May 28, 2024
1 parent fe5790d commit 14a84e0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions media/js/base/consent/init.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ function updateBodyPadding() {

const onResize = debounce(updateBodyPadding, 200);

/**
* If user clicks back button from /cookie-settings/ and
* the page is in bfcache, reload the page if they have
* set a consent cookie to avoid showing the banner again
* inadvertently.
* @param {Object} e - The page navigation event.
*/
function handleBfCacheNavigation(e) {
if (e.persisted && hasConsentCookie()) {
window.location.reload();
}
}

/**
* Opens the consent banner and binds button click event listeners.
*/
Expand All @@ -63,6 +76,7 @@ function openBanner() {
document
.getElementById('moz-consent-banner-button-reject')
.addEventListener('click', MozConsentBanner.onRejectClick, false);
window.addEventListener('pageshow', handleBfCacheNavigation, false);

// Show banner
document.getElementById('moz-consent-banner').classList.add('is-visible');
Expand All @@ -82,6 +96,7 @@ function closeBanner() {
document
.getElementById('moz-consent-banner-button-reject')
.removeEventListener('click', MozConsentBanner.onRejectClick, false);
window.removeEventListener('pageshow', handleBfCacheNavigation, false);

// Hide banner
document
Expand Down

0 comments on commit 14a84e0

Please sign in to comment.