Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created September 3, 2020 03:00
Show Gist options
  • Select an option

  • Save jasonLaster/7436fb9fdb1c86921f2f7661f6533ffb to your computer and use it in GitHub Desktop.

Select an option

Save jasonLaster/7436fb9fdb1c86921f2f7661f6533ffb to your computer and use it in GitHub Desktop.
diff --git a/lib/user.tsx b/lib/user.tsx
index a576a75..954f03d 100755
--- a/lib/user.tsx
+++ b/lib/user.tsx
@@ -6,6 +6,19 @@ import { UserState, UserType, UserProviderProps } from '../types/user';
// Use a global to save the user, so we don't have to fetch it again after page navigations
let userState: UserType;
+function sendUserToBrowser(user) {
+ if (typeof window == 'object') {
+ window.dispatchEvent(
+ new window.CustomEvent('WebChannelMessageToChrome', {
+ detail: JSON.stringify({
+ id: 'record-replay',
+ user,
+ }),
+ })
+ );
+ }
+}
+
const User = React.createContext<UserState>({ user: null, loading: false });
export const fetchUser = async (): Promise<UserType> => {
@@ -27,6 +40,7 @@ export const UserProvider = ({
// If the user was fetched in SSR add it to userState so we don't fetch it again
React.useEffect(() => {
if (!userState && user) {
+ sendUserToBrowser(user);
userState = user;
}
}, []);
@@ -52,6 +66,7 @@ export const useFetchUser = (): UserState => {
fetchUser().then((user) => {
// Only set the user if the component is still mounted
if (isMounted) {
+ sendUserToBrowser(user);
setUser({ user, loading: false });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment