Remove or explain usages of unload and beforeunload listeners
These listeners prevent the browser from adding documents to the back/forward cache when they are added to the window object.
After doing some research on this, unload / beforeunload can be replaced with the pagehideand the event.persisted property. We need to figure out what this means for analytics and metrics since pages will now be able to unload/load from the cache instead of a full load (so if we simply use pagehide instead of unload and pageshow instead of load then metrics that relied on these events to measure load time for example would be skewed). The two options are:
- Define
unloadaspagehidewith!event.persistedand defineloadaspageshowwith!event.persisted - Change the metrics to make use of a compound event (or two separate events) composed of both the
pagehide/pageshowevent and whether the page was loaded from the cache or not. I am investigating what metrics/analytics rely onload/unload/beforeunloadand will update this.
/cc @sparhami
More context on this.
The analytics usage was only added on Safari and will be removed through #24065 since it is not critical (see context in #24071 .
The viewer usage – on the Google AMP Viewer side at least – was added to detect whether an AMP document unloads inside a viewer (and it shouldn't) and displays an error.
Based on my limited understanding, there is no usage of BFCache inside the viewer and since the beforeunload listener for this case is added through the amp-viewer-integration extension (which is only added by AMP caches as far as I know), it shouldn't affect pages that are not cached. We should however detect whether the page is served from a viewer and gate the listener in order to enable BFCache on pages that are cached by an AMP Cache but not served inside a viewer. Another alternative is to remove the listener all together since this is a rare case.
/cc @cvializ @sparhami and Eric Steinlauf for comments
I think @wassgha's analysis is correct. The unload event is used specifically in the case of an AMP Viewer. While it is extremely rare, it really shouldn't be allowed since The AMP Viewer does not expect to contain non-AMP content.
I'm not familiar with the use of beforeunload in amp-viewer-integration.
@ericfs do you know anyone other than @chenshay who would be more familiar with amp-viewer-integration? We can tag them to comment on this
I'm not sure. Can you point me to the code you're talking about and I can take a look?
This is the code
https://github.com/cvializ/amphtml/blob/58cfd262e6f4230dcad20f89874b45bc2127f059/extensions/amp-viewer-integration/0.1/amp-viewer-integration.js#L225-L230
Oh, ok. My explanation above in https://github.com/ampproject/amphtml/issues/23634#issuecomment-523123599 does apply to that. I thought you were saying there is an additional listener for beforeunload. If there is, that's the one I'm not familiar with.
Closed by #24172