-
Notifications
You must be signed in to change notification settings - Fork 230
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
base: main
Are you sure you want to change the base?
fix: post page hydration #3880
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { useMemo } from 'react'; | ||
import { useMedia } from './useMedia'; | ||
import { useMedia, useMediaClient } from './useMedia'; | ||
import { | ||
desktop, | ||
desktopL, | ||
|
@@ -46,4 +46,17 @@ const useViewSize = (size: ViewSize): boolean => { | |
return reversedEvaluatedSizes.includes(size) ? !check : check; | ||
}, [check, size]); | ||
}; | ||
|
||
export const useViewSizeClient = (size: ViewSize): boolean => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
const check = useMediaClient( | ||
[viewSizeToQuery[size].replace('@media ', '')], | ||
[true], | ||
false, | ||
); | ||
|
||
return useMemo(() => { | ||
return reversedEvaluatedSizes.includes(size) ? !check : check; | ||
}, [check, size]); | ||
}; | ||
|
||
export { useViewSize }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,7 +182,7 @@ PostPage.layoutProps = { | |
export default PostPage; | ||
|
||
export async function getStaticPaths(): Promise<GetStaticPathsResult> { | ||
return { paths: [], fallback: true }; | ||
return { paths: [], fallback: 'blocking' }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
export async function getStaticProps({ | ||
|
There was a problem hiding this comment.
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:
What happens now:
undefined
so we renderb
a
because user is defined (from local storage)b
does not matcha
What happens with this change of adding a top level re-render
undefined
so we renderb
undefined
,useEffect
run only after first client side renderb
matchesb
useEffect
executes, user is no longerundefined
user
changeSo even though top level optimization makes sense, it actually adds more work in the end.