amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Remove or explain usages of unload and beforeunload listeners

Open cvializ opened this issue 7 years ago • 8 comments

These listeners prevent the browser from adding documents to the back/forward cache when they are added to the window object.

cvializ avatar Aug 01 '19 18:08 cvializ

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 unload as pagehide with !event.persisted and define load as pageshow with !event.persisted
  • Change the metrics to make use of a compound event (or two separate events) composed of both the pagehide/pageshow event and whether the page was loaded from the cache or not. I am investigating what metrics/analytics rely on load/unload/beforeunload and will update this.

/cc @sparhami

wassgha avatar Aug 19 '19 16:08 wassgha

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

wassgha avatar Aug 20 '19 17:08 wassgha

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 avatar Aug 20 '19 17:08 ericfs

@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

wassgha avatar Aug 20 '19 20:08 wassgha

I'm not sure. Can you point me to the code you're talking about and I can take a look?

ericfs avatar Aug 20 '19 20:08 ericfs

This is the code

https://github.com/cvializ/amphtml/blob/58cfd262e6f4230dcad20f89874b45bc2127f059/extensions/amp-viewer-integration/0.1/amp-viewer-integration.js#L225-L230

wassgha avatar Aug 21 '19 23:08 wassgha

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.

ericfs avatar Aug 22 '19 14:08 ericfs

Closed by #24172

wassgha avatar Aug 24 '19 01:08 wassgha