-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from nabla-studio/DavideSegullo/improve-next_ssr
Davide segullo/improve next ssr
- Loading branch information
Showing
4 changed files
with
30 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,15 @@ | ||
import { QuirksConfigContext } from './config'; | ||
import { type PropsWithChildren, useContext, useEffect, useState } from 'react'; | ||
import { type PropsWithChildren, useSyncExternalStore } from 'react'; | ||
|
||
export const QuirksNextProvider = (props: PropsWithChildren<unknown>) => { | ||
const { children } = props; | ||
const store = useContext(QuirksConfigContext); | ||
const [hydrated, setHydrated] = useState(false); | ||
export const emptySubscribe = () => () => {}; | ||
|
||
if (!store) { | ||
throw new Error( | ||
['`QuirksNextProvider` must be used within `QuirksConfig`.'].join('\n'), | ||
); | ||
} | ||
export const QuirksNextProvider = ({ | ||
children, | ||
}: PropsWithChildren<unknown>) => { | ||
const isServer = useSyncExternalStore( | ||
emptySubscribe, | ||
() => false, | ||
() => true, | ||
); | ||
|
||
useEffect(() => { | ||
store.persist.rehydrate(); | ||
setHydrated(true); | ||
}, [store.persist]); | ||
|
||
if (!hydrated) { | ||
return null; | ||
} | ||
|
||
return children; | ||
return isServer ? null : children; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters