Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect performance observer when page is already hidden #196

Closed
wants to merge 1 commit into from

Conversation

monis0395
Copy link
Contributor

Added a check in onHidden.ts, to check if page is already hidden when onHidden() is called. This would allow listeners like FID, LCP to disconnect performance observer whenever the page is hidden

… onHidden() is called. This would allow listeners like FID, LCP to disconnect performance observer whenever the page is hidden
@monis0395
Copy link
Contributor Author

This will resolve part 1 of #193

@vanderhoop
Copy link
Contributor

Any movement on this? It would seem this change would avoid unrepresentative metrics being reported.

@monis0395
Copy link
Contributor Author

@philipwalton can u please review this PR ?

if @vanderhoop is talking about the case where page was already hidden when the module loaded then become visible getLCP would be reporting false metrics when reportAllChanges is true?

This PR:#196 or PR:#197 should fix that!

@@ -34,4 +33,7 @@ export const onHidden = (cb: OnHiddenCallback, once?: boolean) => {
// Some browsers have buggy implementations of visibilitychange,
// so we use pagehide in addition, just to be safe.
addEventListener('pagehide', onHiddenOrPageHide, true);

// if the document is already hidden
onHiddenOrPageHide({});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this will cause problem for other functions that use onHidden. We do not want to call the callback immediately—only when the page is actually hidden.

I see that your changes to the visibilityWatcher.ts file account for calling it right away, but other modules import this function as well, and it won't work in many of those cases.

@philipwalton
Copy link
Member

Sorry for the late reply, @monis0395. I took a look at your proposed changes, and (unless I'm missing something), I don't think this addresses the original issue in #193.

The point you brought up there was that Performance Observer objects might be created in situation where they don't have to be (because the page is already hidden). In those cases I think the best solution is to not create them in the first place rather than creating them and immediately disconnecting them.

@philipwalton
Copy link
Member

Any movement on this? It would seem this change would avoid unrepresentative metrics being reported.

@vanderhoop this PR and the linked issue do not change what data the library reports, it only changes whether or not a Performance Observer object is created. Please file an issue if you're seeing cases where LCP is reported for pages loaded in the background.

@vanderhoop
Copy link
Contributor

@vanderhoop this PR and the linked issue do not change what data the library reports, it only changes whether or not a Performance Observer object is created. Please file an issue if you're seeing cases where LCP is reported for pages loaded in the background.

Hey @philipwalton, thanks for the quick response 🙏. My coworker, @dvndrsn, has cooked up a great reproduction video for the odd behavior we're seeing for LCP reporting for tabs that have been backgrounded. I'll work with him to scrub it and submit an issue.

@monis0395
Copy link
Contributor Author

The point you brought up there was that Performance Observer objects might be created in situation where they don't have to be (because the page is already hidden).

Yes! You are correct about this

In those cases I think the best solution is to not create them in the first place rather than creating them and immediately disconnecting them.

I am immediately disconnecting Performance Observer(po) because onBFCacheRestore is only attached when po is present. Thus I thought of immediately disconnecting them. I am not fully aware when should we attach onBFCacheRestore

If u have any approaches, I'll be glad to implement them

PS: #197 (comment) this was quite informative.
I did not know that when the document visibility changes from hidden -> visible, no LCP value would be set on the metric

PPS: sorry for the late reply

@philipwalton
Copy link
Member

Closing as this is no longer doable since the code has changed since this PR was filed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants