From fbbfcdfa22d125f5dfad3092231e8312a4976d2f Mon Sep 17 00:00:00 2001 From: Barry Pollard Date: Tue, 5 Mar 2024 15:04:32 +0000 Subject: [PATCH] Reestablish message port on bfcache restore (#172) * Reestablish message port on bfcache restore * Maybe I should uncomment the code fix :-) --- src/browser_action/vitals.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/browser_action/vitals.js b/src/browser_action/vitals.js index 945227d..4fbca21 100644 --- a/src/browser_action/vitals.js +++ b/src/browser_action/vitals.js @@ -49,6 +49,14 @@ // Set up extension message port with the service worker let port = chrome.runtime.connect(); + // Re-establish the port connection on bfcache restore + window.addEventListener('pageshow', (event) => { + if (event.persisted) { + // The page is restored from BFCache, set up a new connection. + port = chrome.runtime.connect(); + } + }); + function initializeMetrics() { let metricsState = localStorage.getItem('web-vitals-extension-metrics'); if (metricsState) { @@ -302,7 +310,7 @@ metric.attribution.eventEntry) { const subPartString = `${metric.name} sub-part`; const eventEntry = metric.attribution.eventEntry; - + let eventTarget = eventEntry.target; // Sometimes the eventEntry has no target, so we need to hunt it out manually. // As of web-vitals@3.5.2 `attribution.eventTarget` does the same thing,