Page prevented back/forward cache restoration - Lighthouse, about Facebook comments plugin

37 Views Asked by At

Working to improve CrUX metrics on my website and one of the issues that pops up on Lighthouse is bfcache restoration prevented by Facebook comments plugin. Specifically, it has unload event listener registered on one of its URLs. Unfortunately it's too late for me to switch to custom comments plugin since there are a lot of historical comments accumulated throughout the years on various pages.

I know there is a push for a new HTTP header that will instruct browsers to deny iframes using this event but I am looking for what can be done now.

Scripts like these would only work on the current document events and not child iframes obviously:

// Ignore the window.onunload attribute.
Object.defineProperty(window, 'onunload', {set: function(handler) {}});
if (!window.original_addEventListener) {
  window.original_addEventListener = window.addEventListener;
  function addEventListener_monkeypatch(event_type, handler, opt) {
    // Ignore unload handler.
    if (event_type !== 'unload') {
      this.original_addEventListener(event_type, handler, opt);
    } else {
      // We can also detect the usage of unload handlers.
      console.trace('unload handlers were used');
    }
  }
  window.addEventListener = addEventListener_monkeypatch;
}

Tried asking the same on Facebook developers forum, not a reply in sight after days. So, what can be done, apart from removing the plugin?

0

There are 0 best solutions below