Skip to content

Commit

Permalink
Merge pull request #44 from nabla-studio/DavideSegullo/improve-next_ssr
Browse files Browse the repository at this point in the history
Davide segullo/improve next ssr
  • Loading branch information
DavideSegullo authored Jan 3, 2024
2 parents d256c8d + 9361b3c commit 9889538
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
9 changes: 4 additions & 5 deletions examples/nextjs/components/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { osmosis, osmosisAssetList } from '@nabla-studio/chain-registry';
import { QuirksConfig, QuirksNextProvider } from '@quirks/react';
import { type Config, ssrPersistOptions } from '@quirks/store';
import { type Config } from '@quirks/store';
import {
xdefiExtension,
keplrExtension,
Expand All @@ -22,7 +22,6 @@ const config: Config = {
],
chains: [osmosis /* bitsong */],
assetsLists: [osmosisAssetList /* bitsongAssetList */],
persistOptions: ssrPersistOptions,
walletConnectOptions: {
providerOpts: {
logger: 'info',
Expand All @@ -39,8 +38,8 @@ const config: Config = {

export const Provider = ({ children }: PropsWithChildren<unknown>) => {
return (
<QuirksConfig config={config}>
<QuirksNextProvider>{children}</QuirksNextProvider>
</QuirksConfig>
<QuirksNextProvider>
<QuirksConfig config={config}>{children}</QuirksConfig>
</QuirksNextProvider>
);
};
32 changes: 11 additions & 21 deletions packages/react/src/providers/next.tsx
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;
};
1 change: 0 additions & 1 deletion packages/store/src/configs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ export const defaultPersistOptions: PersistOptions<AppState> = {
([key]) => !excludedKeys.includes(key as keyof AppState),
),
) as AppState,
skipHydration: true,
};
22 changes: 15 additions & 7 deletions packages/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,24 @@ export const createConfig = (config: Config) => {
* We should inject wallet when walletName, is necessary because 'Wallet' is a class,
* when it is serialized it loses methods, so we could not call wallet functionality.
*/
const unsub = store.persist?.onFinishHydration((state) => {
if (!store.persist.getOptions().skipHydration) {
const state = store.getState();

if (state.walletName && !state.wallet && autoConnect) {
store.getState().reconnect(state.walletName);

/**
* Do once, cleanup onFinishHydration to avoid useless event subscription.
*/
unsub();
}
});
} else {
const unsub = store.persist?.onFinishHydration((state) => {
if (state.walletName && !state.wallet && autoConnect) {
store.getState().reconnect(state.walletName);

/**
* Do once, cleanup onFinishHydration to avoid useless event subscription.
*/
unsub();
}
});
}

return store;
};

0 comments on commit 9889538

Please sign in to comment.