Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

feat: More advanced YouTube pre-load decisions based on device #309

Merged
merged 10 commits into from
Dec 2, 2021

Conversation

oliverlloyd
Copy link
Contributor

@oliverlloyd oliverlloyd commented Dec 1, 2021

What does this change?

This enhances the logic we have for deciding when to pre load the iframe by taking into account if the user is on a mobile device or not and also listening for the onTouchStart event

What is pre loading?

A previous PR added the logic to defer the loading of the YouTube iframe. By doing this we reduce the amount of content that gets initially downloaded on page load, improving the reader experience

@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2021

Size Change: +27 B (0%)

Total Size: 51.4 kB

Filename Size Change
dist/commonjs/YoutubeAtom.js 3.45 kB +14 B (0%)
dist/esm/YoutubeAtom.js 3.3 kB +13 B (0%)
ℹ️ View Unchanged
Filename Size
dist/commonjs/Answers.js 1.26 kB
dist/commonjs/AudioAtom.js 2.74 kB
dist/commonjs/ChartAtom.js 400 B
dist/commonjs/common/MaintainAspectRatio.js 422 B
dist/commonjs/common/Placeholder.js 588 B
dist/commonjs/expandableAtom/Body.js 979 B
dist/commonjs/expandableAtom/Container.js 769 B
dist/commonjs/expandableAtom/Footer.js 1.17 kB
dist/commonjs/expandableAtom/Summary.js 1 kB
dist/commonjs/ExplainerAtom.js 664 B
dist/commonjs/GuideAtom.js 436 B
dist/commonjs/index.js 504 B
dist/commonjs/InteractiveAtom.js 494 B
dist/commonjs/InteractiveLayoutAtom.js 443 B
dist/commonjs/KnowledgeQuiz.js 2.42 kB
dist/commonjs/lib/formatTime.js 384 B
dist/commonjs/lib/pillarPalette.js 273 B
dist/commonjs/lib/unifyPageContent.js 694 B
dist/commonjs/PersonalityQuiz.js 2.6 kB
dist/commonjs/Picture.js 1.65 kB
dist/commonjs/ProfileAtom.js 431 B
dist/commonjs/QandaAtom.js 415 B
dist/commonjs/SharingIcons.js 881 B
dist/commonjs/TimelineAtom.js 1.05 kB
dist/commonjs/types.js 97 B
dist/commonjs/VideoAtom.js 522 B
dist/esm/Answers.js 1.14 kB
dist/esm/AudioAtom.js 2.68 kB
dist/esm/ChartAtom.js 330 B
dist/esm/common/MaintainAspectRatio.js 361 B
dist/esm/common/Placeholder.js 524 B
dist/esm/expandableAtom/Body.js 907 B
dist/esm/expandableAtom/Container.js 706 B
dist/esm/expandableAtom/Footer.js 1.1 kB
dist/esm/expandableAtom/Summary.js 944 B
dist/esm/ExplainerAtom.js 606 B
dist/esm/GuideAtom.js 373 B
dist/esm/index.js 244 B
dist/esm/InteractiveAtom.js 433 B
dist/esm/InteractiveLayoutAtom.js 387 B
dist/esm/KnowledgeQuiz.js 2.33 kB
dist/esm/lib/formatTime.js 317 B
dist/esm/lib/pillarPalette.js 224 B
dist/esm/lib/unifyPageContent.js 635 B
dist/esm/PersonalityQuiz.js 2.48 kB
dist/esm/Picture.js 1.58 kB
dist/esm/ProfileAtom.js 369 B
dist/esm/QandaAtom.js 354 B
dist/esm/SharingIcons.js 814 B
dist/esm/TimelineAtom.js 980 B
dist/esm/types.js 31 B
dist/esm/VideoAtom.js 467 B

compressed-size-action

@oliverlloyd
Copy link
Contributor Author

Refactor YouTubeAtom

Copy link
Contributor

@mxdvl mxdvl left a comment

Choose a reason for hiding this comment

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

This is exciting. I wonder, though, adding all these heuristics for mobile is really what we want to do. There’s also some conflation of touch-only, touch-capable and mobile devices.

Could simply waiting for touchstart and triggering hasUserHovered be enough?

src/YoutubeAtom.tsx Outdated Show resolved Hide resolved
const loadIframe =
iframeSrc && (!hasOverlay || hasUserHovered || hasUserLaunchedPlay);
let loadIframe: boolean;
const isMobile = /Mobi/.test(navigator.userAgent);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is more about testing touch v pointer than it is about mobile devices. Maybe the variable name should reflect that.

I know I was the one originally mentioning touch devices, but thinking about this again, I’m wondering if the best isn’t to just listen to a touchstart event that would act as a hover state.

It’s important to note that these types of optimisations based on the availability of touch have a fundamental flaw: they make assumptions about user behavior based on device capabilities. More explicitly, the example above assumes that because a device is capable of touch input, a user will in fact use touch as the only way to interact with it.

from 2013: Detecting touch: it's the 'why', not the 'how'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a really interesting, and probably very valid, point. I'm going to look into this some more (thanks for the links!) and chat it through with team 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I discussed this with @OllysCoding and we both agreed your points are spot on @mxdvl and so I've refactored this PR to use onTouchStart in addition to onHover. The result of this is a my previously rather large PR is now basically a one-liner.

It's true that this means we sometimes delay the point at which a video is downloaded but as per your points and the linked articles I don't think we were accurately making the decisions about when to do this before and waiting until the user actually interacts is more accurate.

package.json Outdated Show resolved Hide resolved
src/YoutubeAtom.tsx Outdated Show resolved Hide resolved
@oliverlloyd oliverlloyd merged commit f19f9fe into main Dec 2, 2021
@oliverlloyd oliverlloyd deleted the oliver/mobile-logic-for-preloading branch December 2, 2021 14:49
Copy link
Contributor

@mxdvl mxdvl left a comment

Choose a reason for hiding this comment

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

That’s been a fun journey!

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

Successfully merging this pull request may close these issues.

3 participants