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

fix: post page hydration #3880

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

fix: post page hydration #3880

wants to merge 2 commits into from

Conversation

capJavert
Copy link
Contributor

@capJavert capJavert commented Nov 26, 2024

Changes

  • hydration issues on post page, see comments below for more info

Events

Did you introduce any new tracking events?

Experiment

Did you introduce any new experiments?

Manual Testing

Caution

Please make sure existing components are not breaking/affected by this PR

Preview domain

https://fix-post-page-hydration.preview.app.daily.dev

@capJavert capJavert self-assigned this Nov 26, 2024
Copy link

vercel bot commented Nov 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
daily-webapp ✅ Ready (Inspect) Visit Preview Nov 28, 2024 10:00am
1 Skipped Deployment
Name Status Preview Updated (UTC)
storybook ⬜️ Ignored (Inspect) Nov 28, 2024 10:00am

Comment on lines +136 to 141
useEffect(() => {
if (localBootData) {
return localBootData;
setCachedBootData(localBootData);

return;
}
Copy link
Contributor Author

@capJavert capJavert Nov 26, 2024

Choose a reason for hiding this comment

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

So yeah, this adds another re-render top level to boot but it allows us to avoid hydration issues due to user being differenet.

Let's look at the simple example in JSX:

const Component = ({ user }) => {
  return user ? 'a' : 'b'
}

What happens now:

  1. server side user is undefined so we render b
  2. client side user gets loaded inside useMemo default value function from local storage
  3. client side renders a because user is defined (from local storage)
  4. hydration error because b does not match a
  5. react has to re-render everything client side (affects performance)

What happens with this change of adding a top level re-render

  1. server side user is undefined so we render b
  2. client side user is still undefined, useEffect run only after first client side render
  3. hydration passes b matches b
  4. useEffect executes, user is no longer undefined
  5. app re-renders
  6. react only re-paints to DOM elements related to user change

So even though top level optimization makes sense, it actually adds more work in the end.

@@ -182,7 +182,7 @@ PostPage.layoutProps = {
export default PostPage;

export async function getStaticPaths(): Promise<GetStaticPathsResult> {
return { paths: [], fallback: true };
return { paths: [], fallback: 'blocking' };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will make sure we see hydration issues in development, only caveat is that first ever request in production will now stale for the amount of time it takes to fetch data from our API, and now it would show empty screen.

@@ -46,4 +46,17 @@ const useViewSize = (size: ViewSize): boolean => {
return reversedEvaluatedSizes.includes(size) ? !check : check;
}, [check, size]);
};

export const useViewSizeClient = (size: ViewSize): boolean => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Introduced this to not break current implementations but basically due to same explanation like in https://github.com/dailydotdev/apps/pull/3880/files#r1858486018, having useState default value different in SSR and client side causes hydration issues.

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

Successfully merging this pull request may close these issues.

1 participant