Skip to content

Island handle their own placeholders#8944

Merged
mxdvl merged 3 commits intomainfrom
oj/islands/placeholder-simplification
Oct 2, 2023
Merged

Island handle their own placeholders#8944
mxdvl merged 3 commits intomainfrom
oj/islands/placeholder-simplification

Conversation

@mxdvl
Copy link
Copy Markdown
Contributor

@mxdvl mxdvl commented Sep 28, 2023

What does this change?

  • remove's placeholderHeight prop from Island
  • means components that are rendered in client-side-only Islands must explicitly provide their own server-rendered placeholder

Why?

Describing the fallback should be the component's role, not the Island's.

N.B. nearly all uses of placeholderHeight also rendered their own Placeholder (cleared up here), which suggests that the API was unclear...

Screenshots

N/A – identical look and behaviour

@mxdvl mxdvl added Health 📶 Open Journalism run_chromatic Runs chromatic when label is applied labels Sep 28, 2023
@mxdvl mxdvl changed the title refactor(Island): simplify placeholder Island handle their own placeholders Sep 28, 2023
@mxdvl mxdvl force-pushed the oj/islands/placeholder-simplification branch from 1bc7616 to 53a9b74 Compare September 28, 2023 13:22
@github-actions
Copy link
Copy Markdown

github-actions bot commented Sep 28, 2023

Size Change: 0 B 🆕

Total Size: 0 B

compressed-size-action

Copy link
Copy Markdown
Member

@sndrs sndrs left a comment

Choose a reason for hiding this comment

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

coupel of missed bits i think

@sndrs
Copy link
Copy Markdown
Member

sndrs commented Sep 28, 2023

I paired on this so i'll leave it to @guardian/dotcom-platform to approve, but looks good to me!

sndrs
sndrs previously approved these changes Sep 28, 2023
@sndrs sndrs dismissed their stale review September 28, 2023 14:58

leave it team to approve (i hit the wrong button)

@mxdvl mxdvl marked this pull request as ready for review September 28, 2023 14:58
@mxdvl mxdvl requested a review from a team as a code owner September 28, 2023 14:58
@mxdvl mxdvl force-pushed the oj/islands/placeholder-simplification branch from d86e21a to 20a808f Compare September 29, 2023 06:44
mxdvl and others added 3 commits September 29, 2023 07:44
Placeholder should be handled by Islands directly,
which they are doing for the most part already.

Co-authored-by: Alex Sanders <[email protected]>
Co-authored-by: Alex Sanders <[email protected]>
In order to get React to reconcile the server and client DOM,
we need to ensure that the initial client render matches what
we rendered on the server, so we place this behind a useHydrated hook.

See “The Perils of Rehydration“
https://www.joshwcomeau.com/react/the-perils-of-rehydration/#the-solution-9
@mxdvl mxdvl force-pushed the oj/islands/placeholder-simplification branch from 20a808f to b4f06c5 Compare September 29, 2023 06:44
Copy link
Copy Markdown
Contributor

@cemms1 cemms1 left a comment

Choose a reason for hiding this comment

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

Makes sense!

@mxdvl mxdvl merged commit bd68053 into main Oct 2, 2023
@mxdvl mxdvl deleted the oj/islands/placeholder-simplification branch October 2, 2023 13:49

export const useHydrated = (): boolean => {
const [hydrated, setHydrated] = useState(false);
useOnce(() => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Think the useOnce is unnecessary here as its use case is to check all of the items in the dependency array to be defined before invoking the callback. In this case there is no dependency array so a regular useEffect with an empty dependency array should ensure the useEffect is called once.

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.

Good point, I simply went with useOnce as it was more semantic.

}, [hasCommentsHash]);

useEffect(() => {
if (isServer) return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

useEffect shouldn't run on the server so not sure why this is required here


const handlePermalink = (commentId: number) => {
if (typeof window === 'undefined') return false;
if (isServer) return false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If this component shouldn't run on the server then is it better to wrap the isServer check around the component that composes it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also if we do different first-pass renders on the server and client and then try to hydrate then we might get hydration errors:

https://react.dev/reference/react-dom/client/hydrateRoot#caveats

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 has caused issues in GetMatchStats which can be fixed by #9007

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