Skip to content

Comments

Wrap updates-for-element in an IntersectionObserver#285

Merged
julianrubisch merged 6 commits intomainfrom
updates-for-intersection-observer
Nov 9, 2023
Merged

Wrap updates-for-element in an IntersectionObserver#285
julianrubisch merged 6 commits intomainfrom
updates-for-intersection-observer

Conversation

@julianrubisch
Copy link
Contributor

@julianrubisch julianrubisch commented Sep 14, 2023

Feature

Description

This PR addresses an optimization regarding the number of requests that Updatable possibly makes.

The rationale is this: it doesn't really make sense to keep elements that aren't visible up to date. this can be deferred, which defaults to lazy loading.

We have to distinguish 3 cases here:

  • is visible - behavior as usual
  • is not visible - ignore requests. Basically shouldUpdate == false
  • transition from invisible to visible - pull and morph once, then listen as normal

Implementation

I leaned on Turbo's solution here a bit, and introduced a separate AppearanceObserver. The thing I added to the regular IntersectionObserver is an observer for document.visibilityState. This means:

  • not only hidden elements in the DOM tree of the opened browser tab
  • but also generally elements that are in browser tabs that are currently closed/hidden

will not trigger updates. When they come into view, e.g. by scrolling into the viewport, or switching browser tabs, they will perform a one-time update and then continue listening as normal.

In terms of view helpers, I only added an observe_appearance flag to keep this optional. The element will then observe both behaviors though.

  • My code follows the style guidelines of this project
  • Checks (StandardRB & Prettier-Standard) are passing

@netlify
Copy link

netlify bot commented Sep 14, 2023

Deploy Preview for cableready ready!

Name Link
🔨 Latest commit b5a100b
🔍 Latest deploy log https://app.netlify.com/sites/cableready/deploys/65081c7b3b2abb000965b42c
😎 Deploy Preview https://deploy-preview-285--cableready.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@julianrubisch julianrubisch added javascript Pull requests that update Javascript code proposal labels Sep 14, 2023
@hopsoft
Copy link
Contributor

hopsoft commented Sep 14, 2023

This type of optimization is 🤌 Very nice work here.

@julianrubisch julianrubisch marked this pull request as ready for review September 15, 2023 18:31
@julianrubisch julianrubisch marked this pull request as draft September 15, 2023 18:38
@julianrubisch julianrubisch marked this pull request as ready for review September 18, 2023 10:00
Copy link
Contributor

@hopsoft hopsoft left a comment

Choose a reason for hiding this comment

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

Bravo! 👏🏻

@julianrubisch julianrubisch merged commit 951d5dc into main Nov 9, 2023
@julianrubisch julianrubisch deleted the updates-for-intersection-observer branch November 9, 2023 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript Pull requests that update Javascript code proposal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants