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

feat: useAB with SWR (no Context or Provider) #4200

Merged
merged 4 commits into from
Mar 25, 2022
Merged

Conversation

mxdvl
Copy link
Contributor

@mxdvl mxdvl commented Mar 7, 2022

What does this change?

Leverages SWR to provide the ABTestAPI via useAB. Instead of using a Provider, we have an Island dedicated to mutating the SWR cache (SetABTests).

This introduces a new pattern: SWR’s mutate, which is an a tool usually built for optimistic data mutation, where we don’t wait for the promise to resolve. In this specific case, the promise itself will never resolve, and we have to set the value ourselves.

Why?

We currently need to have AB Providers for each Island that relies on the AB Test API. This leads to duplication of props and a lot more boilerplate code.

We want the platform to be as easy as possible to use.

See #3955

@github-actions
Copy link

github-actions bot commented Mar 7, 2022

Size Change: -26.6 kB (-2%)

Total Size: 1.53 MB

Filename Size Change
dotcom-rendering/dist/1189.js 0 B -3.58 kB (removed) 🏆
dotcom-rendering/dist/2839.legacy.js 0 B -3.67 kB (removed) 🏆
dotcom-rendering/dist/4475.js 0 B -6.1 kB (removed) 🏆
dotcom-rendering/dist/53.js 4.12 kB +4 B (0%)
dotcom-rendering/dist/53.legacy.js 4.13 kB +4 B (0%)
dotcom-rendering/dist/6188.js 0 B -4.46 kB (removed) 🏆
dotcom-rendering/dist/7532.legacy.js 0 B -6.17 kB (removed) 🏆
dotcom-rendering/dist/8525.legacy.js 0 B -4.8 kB (removed) 🏆
dotcom-rendering/dist/BrazeMessaging-importable.js 9.57 kB -27 B (0%)
dotcom-rendering/dist/BrazeMessaging-importable.legacy.js 10.3 kB -51 B (0%)
dotcom-rendering/dist/CommercialMetrics-importable.js 6.67 kB +2.19 kB (+49%) 🚨
dotcom-rendering/dist/CommercialMetrics-importable.legacy.js 7.32 kB +2.48 kB (+51%) 🆘
dotcom-rendering/dist/CoreVitals-importable.js 9.35 kB +2.08 kB (+29%) 🚨
dotcom-rendering/dist/CoreVitals-importable.legacy.js 9.86 kB +2.24 kB (+29%) 🚨
dotcom-rendering/dist/frontend.server.js 408 kB -762 B (0%)
dotcom-rendering/dist/initDiscussion.js 8.86 kB +20 B (0%)
dotcom-rendering/dist/initDiscussion.legacy.js 9.13 kB +21 B (0%)
dotcom-rendering/dist/islands.js 9.07 kB +22 B (0%)
dotcom-rendering/dist/islands.legacy.js 9.83 kB +24 B (0%)
dotcom-rendering/dist/MostViewedFooterData-importable.js 12.3 kB +306 B (+3%)
dotcom-rendering/dist/MostViewedFooterData-importable.legacy.js 10.1 kB -2.08 kB (-17%) 👏
dotcom-rendering/dist/SignInGateSelector-importable.js 3.87 kB -1.6 kB (-29%) 🎉
dotcom-rendering/dist/SignInGateSelector-importable.legacy.js 4.18 kB -1.7 kB (-29%) 🎉
dotcom-rendering/dist/SlotBodyEnd-importable.js 8.91 kB -3.2 kB (-26%) 🎉
dotcom-rendering/dist/SlotBodyEnd-importable.legacy.js 10.1 kB -3.21 kB (-24%) 🎉
dotcom-rendering/dist/StickyBottomBanner-importable.js 13.2 kB +2.94 kB (+29%) 🚨
dotcom-rendering/dist/StickyBottomBanner-importable.legacy.js 14.9 kB +2.5 kB (+20%) 🚨
ℹ️ View Unchanged
Filename Size
dotcom-rendering/dist/1576.legacy.js 3.21 kB
dotcom-rendering/dist/1956.js 4.13 kB
dotcom-rendering/dist/2550.legacy.js 4.35 kB
dotcom-rendering/dist/288.js 4.37 kB
dotcom-rendering/dist/2947.js 2.59 kB
dotcom-rendering/dist/2947.legacy.js 2.67 kB
dotcom-rendering/dist/3515.js 22.6 kB
dotcom-rendering/dist/3515.legacy.js 23.4 kB
dotcom-rendering/dist/4109.legacy.js 2.91 kB
dotcom-rendering/dist/5080.js 5.28 kB
dotcom-rendering/dist/5080.legacy.js 5.43 kB
dotcom-rendering/dist/5157.legacy.js 2.73 kB
dotcom-rendering/dist/5226.js 5.5 kB
dotcom-rendering/dist/5226.legacy.js 5.91 kB
dotcom-rendering/dist/5353.legacy.js 2.56 kB
dotcom-rendering/dist/5356.js 32.3 kB
dotcom-rendering/dist/5356.legacy.js 30.2 kB
dotcom-rendering/dist/6021.js 3.95 kB
dotcom-rendering/dist/6021.legacy.js 4.19 kB
dotcom-rendering/dist/6398.js 3.07 kB
dotcom-rendering/dist/6400.js 21.5 kB
dotcom-rendering/dist/6400.legacy.js 21.5 kB
dotcom-rendering/dist/7388.legacy.js 4.32 kB
dotcom-rendering/dist/7576.js 4.21 kB
dotcom-rendering/dist/7576.legacy.js 4.38 kB
dotcom-rendering/dist/7599.js 2.46 kB
dotcom-rendering/dist/7756.js 3.7 kB
dotcom-rendering/dist/7756.legacy.js 3.73 kB
dotcom-rendering/dist/7800.js 11.2 kB
dotcom-rendering/dist/7809.legacy.js 2.69 kB
dotcom-rendering/dist/8129.legacy.js 11.8 kB
dotcom-rendering/dist/9804.js 2.76 kB
dotcom-rendering/dist/9804.legacy.js 2.87 kB
dotcom-rendering/dist/AlreadyVisited-importable.js 4.5 kB
dotcom-rendering/dist/AlreadyVisited-importable.legacy.js 4.5 kB
dotcom-rendering/dist/atomIframe.js 1.74 kB
dotcom-rendering/dist/atomIframe.legacy.js 2.01 kB
dotcom-rendering/dist/AudioAtomWrapper-importable.js 441 B
dotcom-rendering/dist/AudioAtomWrapper-importable.legacy.js 501 B
dotcom-rendering/dist/bootCmp.js 7.76 kB
dotcom-rendering/dist/bootCmp.legacy.js 11.3 kB
dotcom-rendering/dist/Branding-importable.js 3.96 kB
dotcom-rendering/dist/Branding-importable.legacy.js 3.98 kB
dotcom-rendering/dist/braze-web-sdk-core.js 36.1 kB
dotcom-rendering/dist/braze-web-sdk-core.legacy.js 36.1 kB
dotcom-rendering/dist/CalloutBlockComponent-importable.js 6.98 kB
dotcom-rendering/dist/CalloutBlockComponent-importable.legacy.js 7.34 kB
dotcom-rendering/dist/ChartAtomWrapper-importable.js 247 B
dotcom-rendering/dist/ChartAtomWrapper-importable.legacy.js 257 B
dotcom-rendering/dist/CommentCount-importable.js 6.18 kB
dotcom-rendering/dist/CommentCount-importable.legacy.js 6.33 kB
dotcom-rendering/dist/DiscussionContainer-importable.js 5.75 kB
dotcom-rendering/dist/DiscussionContainer-importable.legacy.js 6.04 kB
dotcom-rendering/dist/DiscussionMeta-importable.js 6.63 kB
dotcom-rendering/dist/DiscussionMeta-importable.legacy.js 6.79 kB
dotcom-rendering/dist/DocumentBlockComponent-importable.js 2.93 kB
dotcom-rendering/dist/DocumentBlockComponent-importable.legacy.js 3.03 kB
dotcom-rendering/dist/dynamicImport.js 2.85 kB
dotcom-rendering/dist/dynamicImport.legacy.js 3.14 kB
dotcom-rendering/dist/EditionDropdown-importable.js 3.67 kB
dotcom-rendering/dist/EditionDropdown-importable.legacy.js 3.73 kB
dotcom-rendering/dist/EmbedBlockComponent-importable.js 2.8 kB
dotcom-rendering/dist/EmbedBlockComponent-importable.legacy.js 2.88 kB
dotcom-rendering/dist/embedIframe.js 1.74 kB
dotcom-rendering/dist/embedIframe.legacy.js 2.02 kB
dotcom-rendering/dist/EnhancePinnedPost-importable.js 5.14 kB
dotcom-rendering/dist/EnhancePinnedPost-importable.legacy.js 5.71 kB
dotcom-rendering/dist/FilterKeyEventsToggle-importable.js 3.16 kB
dotcom-rendering/dist/FilterKeyEventsToggle-importable.legacy.js 3.21 kB
dotcom-rendering/dist/FocusStyles-importable.js 4.69 kB
dotcom-rendering/dist/FocusStyles-importable.legacy.js 4.75 kB
dotcom-rendering/dist/ga.js 3.74 kB
dotcom-rendering/dist/ga.legacy.js 4.01 kB
dotcom-rendering/dist/GetMatchNav-importable.js 4.95 kB
dotcom-rendering/dist/GetMatchNav-importable.legacy.js 5.07 kB
dotcom-rendering/dist/GetMatchStats-importable.js 8.94 kB
dotcom-rendering/dist/GetMatchStats-importable.legacy.js 9.73 kB
dotcom-rendering/dist/GetMatchTabs-importable.js 5.79 kB
dotcom-rendering/dist/GetMatchTabs-importable.legacy.js 6 kB
dotcom-rendering/dist/guardian-braze-components-banner.js 11.3 kB
dotcom-rendering/dist/guardian-braze-components-banner.legacy.js 11.3 kB
dotcom-rendering/dist/guardian-braze-components-end-of-article.js 7.68 kB
dotcom-rendering/dist/guardian-braze-components-end-of-article.legacy.js 7.7 kB
dotcom-rendering/dist/GuideAtomWrapper-importable.js 248 B
dotcom-rendering/dist/GuideAtomWrapper-importable.legacy.js 257 B
dotcom-rendering/dist/InstagramBlockComponent-importable.js 2.94 kB
dotcom-rendering/dist/InstagramBlockComponent-importable.legacy.js 3.02 kB
dotcom-rendering/dist/InteractiveBlockComponent-importable.js 6.63 kB
dotcom-rendering/dist/InteractiveBlockComponent-importable.legacy.js 6.89 kB
dotcom-rendering/dist/KnowledgeQuizAtomWrapper-importable.js 252 B
dotcom-rendering/dist/KnowledgeQuizAtomWrapper-importable.legacy.js 263 B
dotcom-rendering/dist/LabsHeader-importable.js 8.26 kB
dotcom-rendering/dist/LabsHeader-importable.legacy.js 8.4 kB
dotcom-rendering/dist/Links-importable.js 6.15 kB
dotcom-rendering/dist/Links-importable.legacy.js 6.52 kB
dotcom-rendering/dist/LiveBlogEpic-importable.js 5 kB
dotcom-rendering/dist/LiveBlogEpic-importable.legacy.js 3.16 kB
dotcom-rendering/dist/Liveness-importable.js 5.62 kB
dotcom-rendering/dist/Liveness-importable.legacy.js 5.75 kB
dotcom-rendering/dist/MapEmbedBlockComponent-importable.js 7.66 kB
dotcom-rendering/dist/MapEmbedBlockComponent-importable.legacy.js 7.93 kB
dotcom-rendering/dist/MostViewedRightWrapper-importable.js 7.04 kB
dotcom-rendering/dist/MostViewedRightWrapper-importable.legacy.js 7.55 kB
dotcom-rendering/dist/newsletterEmbedIframe.js 1.9 kB
dotcom-rendering/dist/newsletterEmbedIframe.legacy.js 2.16 kB
dotcom-rendering/dist/OnwardsLower-importable.js 15.9 kB
dotcom-rendering/dist/OnwardsLower-importable.legacy.js 16.2 kB
dotcom-rendering/dist/OnwardsUpper-importable.js 16.9 kB
dotcom-rendering/dist/OnwardsUpper-importable.legacy.js 17.5 kB
dotcom-rendering/dist/ophan.js 7.04 kB
dotcom-rendering/dist/ophan.legacy.js 7.26 kB
dotcom-rendering/dist/PersonalityQuizAtomWrapper-importable.js 254 B
dotcom-rendering/dist/PersonalityQuizAtomWrapper-importable.legacy.js 266 B
dotcom-rendering/dist/ProfileAtomWrapper-importable.js 248 B
dotcom-rendering/dist/ProfileAtomWrapper-importable.legacy.js 258 B
dotcom-rendering/dist/PulsingDot-importable.js 1.64 kB
dotcom-rendering/dist/PulsingDot-importable.legacy.js 1.74 kB
dotcom-rendering/dist/QandaAtomWrapper-importable.js 247 B
dotcom-rendering/dist/QandaAtomWrapper-importable.legacy.js 257 B
dotcom-rendering/dist/ReaderRevenueDev-importable.js 4.56 kB
dotcom-rendering/dist/ReaderRevenueDev-importable.legacy.js 4.58 kB
dotcom-rendering/dist/readerRevenueDevUtils.js 3.21 kB
dotcom-rendering/dist/readerRevenueDevUtils.legacy.js 4.08 kB
dotcom-rendering/dist/ReaderRevenueLinks-importable.js 5 kB
dotcom-rendering/dist/ReaderRevenueLinks-importable.legacy.js 5.4 kB
dotcom-rendering/dist/relativeTime.js 2.28 kB
dotcom-rendering/dist/relativeTime.legacy.js 2.56 kB
dotcom-rendering/dist/RichLinkComponent-importable.js 8.35 kB
dotcom-rendering/dist/RichLinkComponent-importable.legacy.js 8.54 kB
dotcom-rendering/dist/sentry.js 666 B
dotcom-rendering/dist/sentry.legacy.js 673 B
dotcom-rendering/dist/sentryLoader.js 4.72 kB
dotcom-rendering/dist/sentryLoader.legacy.js 7.69 kB
dotcom-rendering/dist/SetABTests-importable.js 8.94 kB
dotcom-rendering/dist/SetABTests-importable.legacy.js 9.62 kB
dotcom-rendering/dist/ShareCount-importable.js 6.29 kB
dotcom-rendering/dist/ShareCount-importable.legacy.js 6.45 kB
dotcom-rendering/dist/shimport.js 2.76 kB
dotcom-rendering/dist/shimport.legacy.js 2.76 kB
dotcom-rendering/dist/SignInGateMain.js 4.47 kB
dotcom-rendering/dist/SignInGateMain.legacy.js 4.57 kB
dotcom-rendering/dist/SpotifyBlockComponent-importable.js 7.57 kB
dotcom-rendering/dist/SpotifyBlockComponent-importable.legacy.js 7.85 kB
dotcom-rendering/dist/SubNav-importable.js 5.91 kB
dotcom-rendering/dist/SubNav-importable.legacy.js 6.09 kB
dotcom-rendering/dist/TimelineAtomWrapper-importable.js 249 B
dotcom-rendering/dist/TimelineAtomWrapper-importable.legacy.js 258 B
dotcom-rendering/dist/TopRightAdSlot-importable.js 2.03 kB
dotcom-rendering/dist/TopRightAdSlot-importable.legacy.js 2.36 kB
dotcom-rendering/dist/UnsafeEmbedBlockComponent-importable.js 2.92 kB
dotcom-rendering/dist/UnsafeEmbedBlockComponent-importable.legacy.js 3.01 kB
dotcom-rendering/dist/VideoFacebookBlockComponent-importable.js 7.67 kB
dotcom-rendering/dist/VideoFacebookBlockComponent-importable.legacy.js 7.93 kB
dotcom-rendering/dist/VineBlockComponent-importable.js 2.75 kB
dotcom-rendering/dist/VineBlockComponent-importable.legacy.js 2.83 kB
dotcom-rendering/dist/YoutubeBlockComponent-importable.js 7.95 kB
dotcom-rendering/dist/YoutubeBlockComponent-importable.legacy.js 8.28 kB

compressed-size-action

@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from 53e23d1 to a6ffb21 Compare March 7, 2022 15:30
@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from a6ffb21 to 9b94183 Compare March 7, 2022 15:46
@mxdvl
Copy link
Contributor Author

mxdvl commented Mar 7, 2022

This might not be fully baked yet… @OllysCoding feel free to pick up and modify as necessary.

I think the failing tests are because App.tsx creates a distinct instance of useAB, and that one never resolves. Should be fixable by adding the SetABTests component in somewhere.

@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from 9b94183 to 566e892 Compare March 7, 2022 16:48
@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from 566e892 to ea52d8d Compare March 16, 2022 18:50
@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from ea52d8d to 3ac7f87 Compare March 16, 2022 19:09
@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from 3ac7f87 to 4a72524 Compare March 17, 2022 10:35
@mxdvl mxdvl force-pushed the mxdvl/islands/ab-swr branch from 4a72524 to 6eac789 Compare March 17, 2022 10:42
Copy link
Contributor

@oliverlloyd oliverlloyd left a comment

Choose a reason for hiding this comment

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

I made two comments but we've already discussed this in person and it's all good 👍

@@ -77,6 +71,13 @@ export const Page = ({ CAPI, NAV, format }: Props) => {
shouldHideReaderRevenue={CAPI.shouldHideReaderRevenue}
/>
</Island>
<Island clientOnly={true}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Not really sure if this is a problem or not but is there a risk that we have code in other islands that starts to execute prior or in parallel to this initialisation?

We currently deal with dependency by putting things in the head but perhaps we can just early return in local components if there's a dependency?

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 think this is down to consumer to make this decision. There’s a chance that the ABTestApi is undefined. Most cases are already using a useOnce, so it looks safe.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good 👍

@mxdvl mxdvl changed the title feat(proposal): useAB with SWR (no Context or Provider) feat: useAB with SWR (no Context or Provider) Mar 25, 2022
mxdvl and others added 3 commits March 25, 2022 14:35
A hook which returns the AB Test Api when available,
or undefined otherwise.

Leverages an immutable SWR to satisfy all requests to the
AB Core. As soon as the tests are available, all instances of
the useAB hook will render.

Previously, we had an AB Provider wrapped around App.tsx.
With Islands, each island need to provide its own Provider,
leading to much prop repetition.

Co-authored-by: Olly <OllysCoding@users.noreply.github.com>
Favour lib/useAB instead – #4200

Fix tests for new useAB hook

Co-authored-by: Olly <9575458+OllysCoding@users.noreply.github.com>
By using AB & setABTests in preview.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants