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

Refactor YoutubeAtom so it does not rely on an external elementId #10218

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

mxdvl
Copy link
Contributor

@mxdvl mxdvl commented Jan 16, 2024

What does this change?

Generate the elementId on the client from an increasing integer.

See the following article to test: http://localhost:3030/Article/https://www.theguardian.com/world/live/2022/mar/28/russia-ukraine-war-latest-news-zelenskiy-putin-live-updates

Why?

The elementId only needs to be truly unique on the client-side which. If there are clashes on the server, it’s fine.

We cannot use the useId hook here as it produces identical IDs for the same video, due to the tree inside the island being identical…

Screenshots

N/A – Identical

@mxdvl mxdvl added this to the Health milestone Jan 16, 2024
Copy link

github-actions bot commented Jan 16, 2024

Size Change: +103 B (0%)

Total Size: 744 kB

ℹ️ View Unchanged
Filename Size Change
dotcom-rendering/dist/11.client.web.********************.js 5.19 kB 0 B
dotcom-rendering/dist/112.client.web.********************.js 823 B 0 B
dotcom-rendering/dist/1191.client.web.********************.js 679 B 0 B
dotcom-rendering/dist/1229.client.web.********************.js 8.67 kB 0 B
dotcom-rendering/dist/1407.client.web.********************.js 639 B 0 B
dotcom-rendering/dist/1459.client.web.********************.js 877 B 0 B
dotcom-rendering/dist/1573.client.web.********************.js 5.85 kB 0 B
dotcom-rendering/dist/1738.client.web.********************.js 3.52 kB 0 B
dotcom-rendering/dist/1749.client.web.********************.js 920 B 0 B
dotcom-rendering/dist/2293.client.web.********************.js 581 B 0 B
dotcom-rendering/dist/2336.client.web.********************.js 4.76 kB 0 B
dotcom-rendering/dist/307.client.web.********************.js 13.2 kB -8 B (0%)
dotcom-rendering/dist/3375.client.web.********************.js 2.7 kB 0 B
dotcom-rendering/dist/393.client.web.********************.js 921 B 0 B
dotcom-rendering/dist/3958.client.web.********************.js 39.9 kB 0 B
dotcom-rendering/dist/3986.client.web.********************.js 496 B 0 B
dotcom-rendering/dist/4040.client.web.********************.js 651 B 0 B
dotcom-rendering/dist/4045.client.web.********************.js 642 B 0 B
dotcom-rendering/dist/405.client.web.********************.js 915 B 0 B
dotcom-rendering/dist/4094.client.web.********************.js 3.27 kB 0 B
dotcom-rendering/dist/4140.client.web.********************.js 4.71 kB 0 B
dotcom-rendering/dist/4215.client.web.********************.js 409 B 0 B
dotcom-rendering/dist/4269.client.web.********************.js 667 B 0 B
dotcom-rendering/dist/4298.client.web.********************.js 8.4 kB +39 B (0%)
dotcom-rendering/dist/4304.client.web.********************.js 5.46 kB 0 B
dotcom-rendering/dist/4390.client.web.********************.js 507 B 0 B
dotcom-rendering/dist/4438.client.web.********************.js 802 B 0 B
dotcom-rendering/dist/4442.client.web.********************.js 712 B 0 B
dotcom-rendering/dist/476.client.web.********************.js 3.1 kB 0 B
dotcom-rendering/dist/478.client.web.********************.js 594 B 0 B
dotcom-rendering/dist/4870.client.web.********************.js 747 B 0 B
dotcom-rendering/dist/4911.client.web.********************.js 780 B 0 B
dotcom-rendering/dist/4982.client.web.********************.js 3.82 kB 0 B
dotcom-rendering/dist/5020.client.web.********************.js 994 B 0 B
dotcom-rendering/dist/5041.client.web.********************.js 3.77 kB 0 B
dotcom-rendering/dist/5047.client.web.********************.js 779 B 0 B
dotcom-rendering/dist/5242.client.web.********************.js 529 B 0 B
dotcom-rendering/dist/5290.client.web.********************.js 3.24 kB 0 B
dotcom-rendering/dist/5499.client.web.********************.js 2.47 kB 0 B
dotcom-rendering/dist/5619.client.web.********************.js 926 B 0 B
dotcom-rendering/dist/5730.client.web.********************.js 954 B 0 B
dotcom-rendering/dist/5985.client.web.********************.js 748 B 0 B
dotcom-rendering/dist/6043.client.web.********************.js 852 B 0 B
dotcom-rendering/dist/6140.client.web.********************.js 852 B 0 B
dotcom-rendering/dist/6369.client.web.********************.js 5.08 kB 0 B
dotcom-rendering/dist/6407.client.web.********************.js 3.67 kB 0 B
dotcom-rendering/dist/6651.client.web.********************.js 905 B 0 B
dotcom-rendering/dist/6693.client.web.********************.js 823 B 0 B
dotcom-rendering/dist/6756.client.web.********************.js 525 B 0 B
dotcom-rendering/dist/6853.client.web.********************.js 1 kB 0 B
dotcom-rendering/dist/7018.client.web.********************.js 787 B 0 B
dotcom-rendering/dist/7167.client.web.********************.js 16.5 kB 0 B
dotcom-rendering/dist/7356.client.web.********************.js 1 kB 0 B
dotcom-rendering/dist/7536.client.web.********************.js 4.31 kB 0 B
dotcom-rendering/dist/7714.client.web.********************.js 5.74 kB 0 B
dotcom-rendering/dist/7855.client.web.********************.js 787 B 0 B
dotcom-rendering/dist/7880.client.web.********************.js 10.1 kB +65 B (+1%)
dotcom-rendering/dist/8002.client.web.********************.js 801 B 0 B
dotcom-rendering/dist/8288.client.web.********************.js 23 kB 0 B
dotcom-rendering/dist/8344.client.web.********************.js 1.56 kB 0 B
dotcom-rendering/dist/8368.client.web.********************.js 2.98 kB 0 B
dotcom-rendering/dist/8388.client.web.********************.js 619 B 0 B
dotcom-rendering/dist/841.client.web.********************.js 788 B 0 B
dotcom-rendering/dist/8784.client.web.********************.js 5.16 kB 0 B
dotcom-rendering/dist/8818.client.web.********************.js 746 B 0 B
dotcom-rendering/dist/8937.client.web.********************.js 888 B 0 B
dotcom-rendering/dist/9106.client.web.********************.js 2.93 kB 0 B
dotcom-rendering/dist/9173.client.web.********************.js 724 B 0 B
dotcom-rendering/dist/931.client.web.********************.js 2.77 kB 0 B
dotcom-rendering/dist/9314.client.web.********************.js 823 B 0 B
dotcom-rendering/dist/9398.client.web.********************.js 2.96 kB 0 B
dotcom-rendering/dist/9417.client.web.********************.js 2.87 kB 0 B
dotcom-rendering/dist/9458.client.web.********************.js 5.09 kB 0 B
dotcom-rendering/dist/9461.client.web.********************.js 2.38 kB 0 B
dotcom-rendering/dist/9529.client.web.********************.js 3.23 kB 0 B
dotcom-rendering/dist/958.client.web.********************.js 3.17 kB 0 B
dotcom-rendering/dist/9591.client.web.********************.js 1.84 kB 0 B
dotcom-rendering/dist/9621.client.web.********************.js 719 B 0 B
dotcom-rendering/dist/9676.client.web.********************.js 888 B 0 B
dotcom-rendering/dist/9806.client.web.********************.js 4.59 kB 0 B
dotcom-rendering/dist/9856.client.web.********************.js 5.75 kB 0 B
dotcom-rendering/dist/9879.client.web.********************.js 3.39 kB 0 B
dotcom-rendering/dist/9905.client.web.********************.js 8.24 kB 0 B
dotcom-rendering/dist/9970.client.web.********************.js 3.35 kB 0 B
dotcom-rendering/dist/9978.client.web.********************.js 960 B 0 B
dotcom-rendering/dist/AdPortals-importable.client.web.********************.js 3.72 kB 0 B
dotcom-rendering/dist/AlreadyVisited-importable.client.web.********************.js 421 B 0 B
dotcom-rendering/dist/AppsEpic-importable.client.web.********************.js 4.09 kB 0 B
dotcom-rendering/dist/AppsFooter-importable.client.web.********************.js 3.58 kB 0 B
dotcom-rendering/dist/AppsLightboxImage-importable.client.web.********************.js 3 kB 0 B
dotcom-rendering/dist/AppsLightboxImageStore-importable.client.web.********************.js 2.42 kB 0 B
dotcom-rendering/dist/AudioAtomWrapper-importable.client.web.********************.js 3.58 kB 0 B
dotcom-rendering/dist/AustralianTerritorySwitcher-importable.client.web.********************.js 2.01 kB 0 B
dotcom-rendering/dist/Branding-importable.client.web.********************.js 2.54 kB 0 B
dotcom-rendering/dist/braze-web-sdk-core.client.web.********************.js 36.9 kB 0 B
dotcom-rendering/dist/BrazeMessaging-importable.client.web.********************.js 5.25 kB 0 B
dotcom-rendering/dist/CalloutBlockComponent-importable.client.web.********************.js 6.76 kB 0 B
dotcom-rendering/dist/CalloutEmbedBlockComponent-importable.client.web.********************.js 5.76 kB 0 B
dotcom-rendering/dist/CardCommentCount-importable.client.web.********************.js 4.42 kB 0 B
dotcom-rendering/dist/Carousel-importable.client.web.********************.js 7.79 kB 0 B
dotcom-rendering/dist/CarouselForNewsletters-importable.client.web.********************.js 5.65 kB 0 B
dotcom-rendering/dist/ChartAtom-importable.client.web.********************.js 541 B 0 B
dotcom-rendering/dist/CommentCount-importable.client.web.********************.js 3.24 kB 0 B
dotcom-rendering/dist/DiscussionContainer-importable.client.web.********************.js 23.5 kB 0 B
dotcom-rendering/dist/DiscussionMeta-importable.client.web.********************.js 3.76 kB 0 B
dotcom-rendering/dist/DocumentBlockComponent-importable.client.web.********************.js 3.58 kB 0 B
dotcom-rendering/dist/EmbedBlockComponent-importable.client.web.********************.js 4.13 kB 0 B
dotcom-rendering/dist/EnhancePinnedPost-importable.client.web.********************.js 2.01 kB 0 B
dotcom-rendering/dist/FetchOnwardsData-importable.client.web.********************.js 2.52 kB 0 B
dotcom-rendering/dist/FilterKeyEventsToggle-importable.client.web.********************.js 3.35 kB 0 B
dotcom-rendering/dist/FocusStyles-importable.client.web.********************.js 609 B 0 B
dotcom-rendering/dist/FollowWrapper-importable.client.web.********************.js 789 B 0 B
dotcom-rendering/dist/FooterLabel-importable.client.web.********************.js 341 B 0 B
dotcom-rendering/dist/frameworks.client.web.********************.js 20.7 kB 0 B
dotcom-rendering/dist/GetCricketScoreboard-importable.client.web.********************.js 4.15 kB 0 B
dotcom-rendering/dist/GetMatchNav-importable.client.web.********************.js 10.7 kB +7 B (0%)
dotcom-rendering/dist/GetMatchStats-importable.client.web.********************.js 1.63 kB 0 B
dotcom-rendering/dist/GetMatchTabs-importable.client.web.********************.js 2.62 kB 0 B
dotcom-rendering/dist/guardian-braze-components-banner.client.web.********************.js 14.7 kB 0 B
dotcom-rendering/dist/guardian-braze-components-end-of-article.client.web.********************.js 9.29 kB 0 B
dotcom-rendering/dist/GuideAtomWrapper-importable.client.web.********************.js 781 B 0 B
dotcom-rendering/dist/HeaderTopBar-importable.client.web.********************.js 10.9 kB 0 B
dotcom-rendering/dist/index.client.web.********************.js 47.1 kB +2 B (0%)
dotcom-rendering/dist/InstagramBlockComponent-importable.client.web.********************.js 3.66 kB 0 B
dotcom-rendering/dist/InteractiveBlockComponent-importable.client.web.********************.js 6.1 kB 0 B
dotcom-rendering/dist/InteractiveContentsBlockComponent-importable.client.web.********************.js 4.66 kB 0 B
dotcom-rendering/dist/InteractiveSupportButton-importable.client.web.********************.js 3.52 kB 0 B
dotcom-rendering/dist/KeyEventsCarousel-importable.client.web.********************.js 4.19 kB 0 B
dotcom-rendering/dist/KnowledgeQuizAtom-importable.client.web.********************.js 3.53 kB 0 B
dotcom-rendering/dist/LatestLinks-importable.client.web.********************.js 1.12 kB 0 B
dotcom-rendering/dist/LightboxHash-importable.client.web.********************.js 436 B 0 B
dotcom-rendering/dist/LightboxJavascript-importable.client.web.********************.js 4.57 kB 0 B
dotcom-rendering/dist/LiveBlogEpic-importable.client.web.********************.js 3.65 kB 0 B
dotcom-rendering/dist/Liveness-importable.client.web.********************.js 4.92 kB 0 B
dotcom-rendering/dist/ManyNewsletterSignUp-importable.client.web.********************.js 6.33 kB 0 B
dotcom-rendering/dist/MapEmbedBlockComponent-importable.client.web.********************.js 5.66 kB 0 B
dotcom-rendering/dist/Metrics-importable.client.web.********************.js 2.29 kB 0 B
dotcom-rendering/dist/MostViewedFooter-importable.client.web.********************.js 4.04 kB 0 B
dotcom-rendering/dist/MostViewedFooterData-importable.client.web.********************.js 6.71 kB 0 B
dotcom-rendering/dist/MostViewedRightWrapper-importable.client.web.********************.js 4.41 kB 0 B
dotcom-rendering/dist/OnwardsUpper-importable.client.web.********************.js 4.23 kB 0 B
dotcom-rendering/dist/PersonalityQuizAtom-importable.client.web.********************.js 3.66 kB 0 B
dotcom-rendering/dist/ProfileAtom-importable.client.web.********************.js 543 B 0 B
dotcom-rendering/dist/ProfileAtomWrapper-importable.client.web.********************.js 802 B 0 B
dotcom-rendering/dist/PulsingDot-importable.client.web.********************.js 748 B 0 B
dotcom-rendering/dist/QandaAtom-importable.client.web.********************.js 539 B 0 B
dotcom-rendering/dist/ReaderRevenueDev-importable.client.web.********************.js 470 B 0 B
dotcom-rendering/dist/readerRevenueDevUtils.client.web.********************.js 1.96 kB 0 B
dotcom-rendering/dist/ReaderRevenueLinks-importable.client.web.********************.js 5.83 kB 0 B
dotcom-rendering/dist/RelativeTime-importable.client.web.********************.js 1.98 kB 0 B
dotcom-rendering/dist/RichLinkComponent-importable.client.web.********************.js 6.21 kB 0 B
dotcom-rendering/dist/SecureSignup-importable.client.web.********************.js 3.57 kB 0 B
dotcom-rendering/dist/SendAMessage-importable.client.web.********************.js 4.39 kB 0 B
dotcom-rendering/dist/SendTargetingParams-importable.client.web.********************.js 2.11 kB 0 B
dotcom-rendering/dist/sentry.client.web.********************.js 771 B 0 B
dotcom-rendering/dist/SetABTests-importable.client.web.********************.js 3.37 kB 0 B
dotcom-rendering/dist/SetAdTargeting-importable.client.web.********************.js 482 B 0 B
dotcom-rendering/dist/shimport.client.web.********************.js 2.79 kB 0 B
dotcom-rendering/dist/ShowHideContainers-importable.client.web.********************.js 643 B 0 B
dotcom-rendering/dist/ShowMore-importable.client.web.********************.js 5.68 kB -2 B (0%)
dotcom-rendering/dist/SignInGateMain.client.web.********************.js 3.9 kB 0 B
dotcom-rendering/dist/SignInGateMainCheckoutComplete.client.web.********************.js 5 kB 0 B
dotcom-rendering/dist/SignInGateSelector-importable.client.web.********************.js 5.63 kB 0 B
dotcom-rendering/dist/SlotBodyEnd-importable.client.web.********************.js 6.84 kB 0 B
dotcom-rendering/dist/SpotifyBlockComponent-importable.client.web.********************.js 5.49 kB 0 B
dotcom-rendering/dist/StickyBottomBanner-importable.client.web.********************.js 5.55 kB 0 B
dotcom-rendering/dist/SubNav-importable.client.web.********************.js 2.24 kB 0 B
dotcom-rendering/dist/SupportTheG-importable.client.web.********************.js 5.96 kB 0 B
dotcom-rendering/dist/TableOfContents-importable.client.web.********************.js 3.1 kB 0 B
dotcom-rendering/dist/TimelineAtom-importable.client.web.********************.js 1.24 kB 0 B
dotcom-rendering/dist/TweetBlockComponent-importable.client.web.********************.js 1.02 kB 0 B
dotcom-rendering/dist/UnsafeEmbedBlockComponent-importable.client.web.********************.js 3.67 kB 0 B
dotcom-rendering/dist/VideoFacebookBlockComponent-importable.client.web.********************.js 5.67 kB 0 B
dotcom-rendering/dist/VineBlockComponent-importable.client.web.********************.js 3.51 kB 0 B
dotcom-rendering/dist/WeatherWrapper-importable.client.web.********************.js 5.45 kB 0 B
dotcom-rendering/dist/YoutubeBlockComponent-importable.client.web.********************.js 3.9 kB 0 B

compressed-size-action

@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from aede77a to b8e4bcf Compare January 16, 2024 10:49
@mxdvl mxdvl marked this pull request as ready for review January 16, 2024 12:21
@mxdvl mxdvl requested a review from a team as a code owner January 16, 2024 12:22
@mxdvl mxdvl added the run_chromatic Runs chromatic when label is applied label Jan 16, 2024
Copy link

Hello 👋! When you're ready to run Chromatic, please apply the run_chromatic label to this PR.

Click here to see the Chromatic project.

Copy link
Member

@arelra arelra left a comment

Choose a reason for hiding this comment

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

The YoutubeAtom is partially server rendered so the overlay shows on the SSR. The overlay uses the unique id too so it might mean we temporarily have ids in the DOM that are not-unique.

This might not be an issue in practice if the ids get set before the YouTube player initialises but I know we've had issues in the past with the player not being able to correctly identify the right video to play because of duplicate ids.

It's worth trying the storybook tests manually to check they work as expected, particularly the duplicate video tests.

@mxdvl mxdvl marked this pull request as draft January 16, 2024 12:53
@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from 7c45379 to c0b8974 Compare January 16, 2024 14:33
@mxdvl mxdvl changed the base branch from main to mxdvl/refactor-youtube-atom-tests January 16, 2024 14:59
@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from c0b8974 to cfbd8a9 Compare January 16, 2024 14:59
Base automatically changed from mxdvl/refactor-youtube-atom-tests to main January 16, 2024 15:22
@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch 3 times, most recently from 03c6d50 to 13f7942 Compare January 16, 2024 16:03
@mxdvl mxdvl changed the title Refactor YoutubeAtom so it does not rely on elementId Refactor YoutubeAtom so it does not rely on an external elementId Jan 16, 2024
@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from 13f7942 to 7f2142b Compare January 16, 2024 17:08
@mxdvl mxdvl marked this pull request as ready for review January 16, 2024 17:22
Copy link

Hello 👋! When you're ready to run Chromatic, please apply the run_chromatic label to this PR.

Click here to see the Chromatic project.

@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from 7f2142b to 152e323 Compare January 16, 2024 17:33
@@ -297,7 +298,7 @@ test.describe('YouTube Atom', () => {
// Listen for the ophan call made when the video is played
const ophanPlayEventPromise2 = interceptOphanPlayEvent({
page,
id: 'gu-video-youtube-1e89d5bd-489e-470a-857e-4f30e85b5aec',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did we expect this to be the same id as the first video?

Copy link
Member

@arelra arelra Jan 16, 2024

Choose a reason for hiding this comment

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

I think so. Aiui Ophan doesn't care about duplicate videos it just wants to log each video play.

Actually if this is the elementId it looks like a bug as I would have expected we give Ophan the videoId (assetId prop in YoutubeBlockComponent.importable) and not the unique / elementId which can change across renders.

Worth checking with the Ophan team what their expectations are for these events.

Copy link
Member

Choose a reason for hiding this comment

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

Actually this is the CAPI element.id and not the unstable element.elementId so it should persist across renders and of course otherwise the test would fail. The question remains as to what Ophan want to be logged i.e. the element.id or the videoId / assetId

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So correctly done in renderElement but not Card. Quite confusing—I'll update.

@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch 3 times, most recently from f9b7b48 to de6d6d3 Compare January 17, 2024 10:11
@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from de6d6d3 to 82c1dd2 Compare January 17, 2024 10:12
we can generate a unique elementI from an increasing
integer on the client. On the server this defaults
to 0 or `undefined`

rename the elementId to id for the MainMedia type
@mxdvl mxdvl force-pushed the mxdvl/pure-function/element-id/YoutubeAtom branch from 82c1dd2 to e0a4772 Compare January 17, 2024 10:23
Comment on lines 137 to 151
const [loadPlayer, setLoadPlayer] = useState<boolean>(false);
useEffect(() => {
if (elementId === 'server') {
// We may never load the player on the server
return;
}

if (!hasOverlay) {
// load the player if there is no overlay
setLoadPlayer(true);
} else if (overlayClicked) {
// load the player if the overlay has been clicked
setLoadPlayer(true);
}
}, [elementId, hasOverlay, overlayClicked]);
Copy link
Member

@arelra arelra Jan 17, 2024

Choose a reason for hiding this comment

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

I'm wondering if this could be simplified.

Instead of elementId could we pass through index: number | undefined and return in the main render path if isUndefined(index)?

I think that would mean the other state could remain derived and it would remove a useEffect and a re-render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I’ll keep the changes to a minimal and just add one more check:

if (isUndefined(index)) {
	loadPlayer = false;
}

Copy link
Member

@arelra arelra left a comment

Choose a reason for hiding this comment

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

👍

@mxdvl mxdvl merged commit e76b83b into main Jan 17, 2024
26 checks passed
@mxdvl mxdvl deleted the mxdvl/pure-function/element-id/YoutubeAtom branch January 17, 2024 17:20
@prout-bot
Copy link

Seen on PROD (merged by @mxdvl 8 minutes and 57 seconds ago) Please check your changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotcom-rendering run_chromatic Runs chromatic when label is applied Seen-on-PROD
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants