Skip to content

Discussion as an island - #3839

Merged
oliverlloyd merged 16 commits into
mainfrom
oliver/discussion-island
Jan 21, 2022
Merged

oliverlloyd merged 16 commits into
mainfrom
oliver/discussion-island

Conversation

@oliverlloyd

@oliverlloyd oliverlloyd commented Jan 17, 2022 •

Copy link
Copy Markdown
Contributor

What does this change?

This moves Discussion over to the island pattern

Along the way

To make this transition work the following refactors and changes were needed

Discussion v9.1.1

It turns out Discussion wasn't being included in our automated accessibility checks but that when we loaded it onto the page via an island this issue was fixed. However, this meant we started to see new problems in the report failing the build. To get past this I fixed those issues in discussion-rendering and brought that new release in here

No user in App.tsx

This PR removes the global state of user from App.tsx

No more beingHydrated

This prop was needed to fix an issue with hydration in Storybook. This problem was solved by the islands pattern so this prop could be removed.

DiscussionContainer

This is a wrapper component that is being used to compose the decision about if the user is signed in or not. If they are, we render another wrapper component DiscussionWhenSigned which is where we fetch the user profile, and if they are not, we directly render Discussion itself.

This level of composition is needed because you cannot conditionally call a react hook.

Why?

🏖️ Islands are where we want to be 🏝️

@github-actions

github-actions Bot commented Jan 17, 2022 •

Copy link
Copy Markdown

Size Change: -127 kB (-5%) ✅

Total Size: 2.59 MB

Filename Size Change
dotcom-rendering/dist/195.js 1.12 kB +7 B (+1%)
dotcom-rendering/dist/195.legacy.js 1.23 kB +7 B (+1%)
dotcom-rendering/dist/272.js 0 B -8.6 kB (removed) 🏆
dotcom-rendering/dist/331.js 16.5 kB +318 B (+2%)
dotcom-rendering/dist/331.legacy.js 16.7 kB +310 B (+2%)
dotcom-rendering/dist/39.js 0 B -4.05 kB (removed) 🏆
dotcom-rendering/dist/39.legacy.js 0 B -4.21 kB (removed) 🏆
dotcom-rendering/dist/460.legacy.js 0 B -6.75 kB (removed) 🏆
dotcom-rendering/dist/468.legacy.js 3.1 kB +14 B (0%)
dotcom-rendering/dist/597.js 5.37 kB +2 B (0%)
dotcom-rendering/dist/597.legacy.js 5.52 kB +1 B (0%)
dotcom-rendering/dist/602.js 0 B -4 kB (removed) 🏆
dotcom-rendering/dist/602.legacy.js 0 B -4.11 kB (removed) 🏆
dotcom-rendering/dist/624.js 0 B -3.58 kB (removed) 🏆
dotcom-rendering/dist/624.legacy.js 0 B -3.7 kB (removed) 🏆
dotcom-rendering/dist/639.js 4.08 kB -187 B (-4%)
dotcom-rendering/dist/639.legacy.js 4.26 kB -117 B (-3%)
dotcom-rendering/dist/DocumentBlockComponent.js 565 B -1 B (0%)
dotcom-rendering/dist/DocumentBlockComponent.legacy.js 594 B -2 B (0%)
dotcom-rendering/dist/frontend.server.js 1.81 MB -13.4 kB (-1%)
dotcom-rendering/dist/guardian-braze-components-banner.js 9.78 kB +205 B (+2%)
dotcom-rendering/dist/guardian-braze-components-banner.legacy.js 9.78 kB +205 B (+2%)
dotcom-rendering/dist/InteractiveBlockComponent.js 3.04 kB +84 B (+3%)
dotcom-rendering/dist/InteractiveBlockComponent.legacy.js 3.17 kB +82 B (+3%)
dotcom-rendering/dist/InteractiveContentsBlockComponent.legacy.js 1.95 kB +1 B (0%)
dotcom-rendering/dist/islands.js 7.29 kB +49 B (+1%)
dotcom-rendering/dist/islands.legacy.js 8.05 kB +47 B (+1%)
dotcom-rendering/dist/MostViewedFooterData.js 6.36 kB +84 B (+1%)
dotcom-rendering/dist/MostViewedFooterData.legacy.js 6.46 kB +97 B (+2%)
dotcom-rendering/dist/OnwardsLower.js 10 kB +327 B (+3%)
dotcom-rendering/dist/OnwardsLower.legacy.js 10.2 kB +328 B (+3%)
dotcom-rendering/dist/OnwardsUpper.js 14.8 kB +626 B (+4%)
dotcom-rendering/dist/OnwardsUpper.legacy.js 15.1 kB +702 B (+5%) 🔍
dotcom-rendering/dist/react.js 99.8 kB -36.5 kB (-27%) 🎉
dotcom-rendering/dist/react.legacy.js 107 kB -34.5 kB (-24%) 🎉
dotcom-rendering/dist/RichLinkComponent.js 0 B -3.25 kB (removed) 🏆
dotcom-rendering/dist/RichLinkComponent.legacy.js 0 B -3.29 kB (removed) 🏆
dotcom-rendering/dist/SignInGateMain.js 1.81 kB -1 B (0%)
dotcom-rendering/dist/SignInGateMain.legacy.js 1.85 kB -2 B (0%)
dotcom-rendering/dist/YoutubeBlockComponent.js 2.57 kB +1 B (0%)
dotcom-rendering/dist/YoutubeBlockComponent.legacy.js 2.7 kB +1 B (0%)
ℹ️ View Unchanged
Filename Size
dotcom-rendering/dist/126.js 6.65 kB
dotcom-rendering/dist/126.legacy.js 6.72 kB
dotcom-rendering/dist/327.js 4.46 kB
dotcom-rendering/dist/327.legacy.js 4.47 kB
dotcom-rendering/dist/330.js 4.29 kB
dotcom-rendering/dist/330.legacy.js 4.38 kB
dotcom-rendering/dist/343.js 1.63 kB
dotcom-rendering/dist/343.legacy.js 1.71 kB
dotcom-rendering/dist/348.js 4.02 kB
dotcom-rendering/dist/348.legacy.js 4.12 kB
dotcom-rendering/dist/400.js 21.5 kB
dotcom-rendering/dist/400.legacy.js 21.5 kB
dotcom-rendering/dist/611.legacy.js 31.1 kB
dotcom-rendering/dist/75.js 7.95 kB
dotcom-rendering/dist/75.legacy.js 8.41 kB
dotcom-rendering/dist/760.js 5.5 kB
dotcom-rendering/dist/760.legacy.js 5.67 kB
dotcom-rendering/dist/764.js 8.89 kB
dotcom-rendering/dist/764.legacy.js 6.88 kB
dotcom-rendering/dist/893.js 30.5 kB
dotcom-rendering/dist/924.js 4.04 kB
dotcom-rendering/dist/924.legacy.js 4.14 kB
dotcom-rendering/dist/965.js 2.16 kB
dotcom-rendering/dist/965.legacy.js 2.23 kB
dotcom-rendering/dist/atomIframe.js 1.87 kB
dotcom-rendering/dist/atomIframe.legacy.js 2.14 kB
dotcom-rendering/dist/bootCmp.js 7.39 kB
dotcom-rendering/dist/bootCmp.legacy.js 10.9 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.js 4.14 kB
dotcom-rendering/dist/CalloutBlockComponent.legacy.js 4.46 kB
dotcom-rendering/dist/cmp.js 7.51 kB
dotcom-rendering/dist/coreVitals.js 4.03 kB
dotcom-rendering/dist/coreVitals.legacy.js 4.33 kB
dotcom-rendering/dist/dynamicImport.js 2.99 kB
dotcom-rendering/dist/dynamicImport.legacy.js 3.29 kB
dotcom-rendering/dist/embedIframe.js 1.88 kB
dotcom-rendering/dist/embedIframe.legacy.js 2.14 kB
dotcom-rendering/dist/ga.js 3.88 kB
dotcom-rendering/dist/ga.legacy.js 4.14 kB
dotcom-rendering/dist/guardian-braze-components-end-of-article.js 6.56 kB
dotcom-rendering/dist/guardian-braze-components-end-of-article.legacy.js 6.57 kB
dotcom-rendering/dist/InteractiveContentsBlockComponent.js 1.87 kB
dotcom-rendering/dist/MapEmbedBlockComponent.js 1.87 kB
dotcom-rendering/dist/MapEmbedBlockComponent.legacy.js 1.93 kB
dotcom-rendering/dist/newsletterEmbedIframe.js 1.83 kB
dotcom-rendering/dist/newsletterEmbedIframe.legacy.js 2.1 kB
dotcom-rendering/dist/ophan.js 7.18 kB
dotcom-rendering/dist/ophan.legacy.js 7.38 kB
dotcom-rendering/dist/relativeTime.js 2.41 kB
dotcom-rendering/dist/relativeTime.legacy.js 2.68 kB
dotcom-rendering/dist/sentry.js 676 B
dotcom-rendering/dist/sentry.legacy.js 688 B
dotcom-rendering/dist/sentryLoader.js 4.74 kB
dotcom-rendering/dist/sentryLoader.legacy.js 7.71 kB
dotcom-rendering/dist/shimport.js 2.75 kB
dotcom-rendering/dist/shimport.legacy.js 2.76 kB
dotcom-rendering/dist/VideoFacebookBlockComponent.js 1.88 kB
dotcom-rendering/dist/VideoFacebookBlockComponent.legacy.js 1.93 kB
dotcom-rendering/dist/VineBlockComponent.js 575 B
dotcom-rendering/dist/VineBlockComponent.legacy.js 591 B

compressed-size-action

Comment on lines -29 to -40
// **************************************************************************
// beingHydrated?
// We use this prop to solve a problem we have with Storybook. If you remove
// it then the page will render fine on production but our layout stories
// will render two copies of Discussion, side by side. The reason for this
// is because we technically server side render these layout stories on the
// client, inside the story file itself.
//
// Yes, it's true, having props purely to solve test implementation problems
// is not great. If you feel strongly about this and want to remove this
// prop I'm okay with that. If you were able to solve this another way
// then thank you!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👋

Comment on lines +65 to +71
const { data: user } = useApi<{ userProfile: UserProfile }>(
isSignedIn
? joinUrl(
discussionApiUrl,
'profile/me?strict_sanctions_check=false',
)
: '',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How does useApi behave when passed an empty string? Does it throw an error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've removed that pattern now and replaced it with composition. I was hoping to avoid using that approach because it's a lot of file renaming but it seems to be the only way. If you pass fetch '' then it makes a call to the domain the page is on, which isn't what we want.

@oliverlloyd
oliverlloyd marked this pull request as ready for review January 18, 2022 17:09
)}

{beingHydrated && !isExpanded && (
<Lazy margin={300} disableFlexStyles={true}>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We no longer need this Lazy component because Island provides this deferred loading built in

// prop I'm okay with that. If you were able to solve this another way
// then thank you!
beingHydrated?: boolean;
// **************************************************************************

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This prop was needed to fix an issue with hydration in Storybook. This problem was solved by the islands pattern so this prop could be removed.

export const App = ({ CAPI, ophanRecord }: Props) => {
log('dotcom', `App.tsx render #${(renderCount += 1)}`);
const isSignedIn = !!getCookie({ name: 'GU_U', shouldMemoize: true });
const [user, setUser] = useState<UserProfile | null>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This PR removes the global state of user from App.tsx

CAPI.config.discussionApiClientHeader
}
enableDiscussionSwitch={
CAPI.config.switches.enableDiscussionSwitch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We now set this property correctly using the actual switch value from CAPI

@jamesgorrie

Copy link
Copy Markdown
Contributor

part of #3629

</div>
</Hide>

{beingHydrated && isExpanded && (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we test what happens with expanded behaviour

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 to Olly's suggestion

@OllysCoding OllysCoding left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 Would like to know the results of testing isExpanded behaviour

@ioannakok ioannakok left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 I think this is a great refactoring

@oliverlloyd
oliverlloyd merged commit 541f0ba into main Jan 21, 2022
@oliverlloyd
oliverlloyd deleted the oliver/discussion-island branch January 21, 2022 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants