html icon indicating copy to clipboard operation
html copied to clipboard

event.persisted for pagereveal event

Open o-t-w opened this issue 9 months ago • 4 comments

What problem are you trying to solve?

Seeing as this event is fired even when the bfcache is used, doesn't it make sense for the event to have a .persisted property? Suggested here. The spec and the Chrome implementation does not include this.

What solutions exist today?

No response

How would you solve it?

No response

Anything else?

No response

o-t-w avatar Apr 30 '24 23:04 o-t-w

What's wrong with using pageshow?

domenic avatar May 01 '24 14:05 domenic

Apologies if this represents a lack of understanding on my part. I know this event is perhaps exclusively meant for use with view transitions.

Lets say somebody needs to inject some critical dynamic content at the top of the page, for example. They want to do that ASAP to avoid layout shift. They might purposefully use a blocking classic inline script, or maybe an inline <script type="module" async blocking="render"> but then they'd need to use DOMContentLoaded in order to avoid querySelector returning null, but DOMContentLoaded only fires after all deferred scripts. So for their JavaScript to run as early as possible the quicker option is actually to use a deferred script which won't need a DOMContentLoaded event listener. Is there a reason you shouldn't be able to use pagereveal for this sort of purpose? Wouldn't you want to do this sort of thing at the first render opportunity in conjunction with <link rel="expect" href="#visually-critical-thing" blocking="render">? Use-cases for render-blocking other than view transitions have been discussed.

If you added some dynamic content to the DOM inside of pagereveal, navigate to another page, and then press the back button and bfcache has cached the page, the dynamic content would be added to the cached page all over again. If you keep pressing the forward and back button the same DOM nodes get added nth number of times... Wouldn't checking for event.persisted be able to avoid that scenario?

o-t-w avatar May 04 '24 13:05 o-t-w

Sorry, I can't quite understand how that answered my question... Can you show me what goes wrong with using pageshow to accomplish your use case?

domenic avatar May 04 '24 15:05 domenic

For the use case I mentioned, on an initial page load pageshow fires too late to safely avoid layout shift or flashes of wrongly-styled content (it fires after the load event).

o-t-w avatar May 04 '24 15:05 o-t-w