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: attach sync call directly to promise #989

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
'use client'
import React, {
Suspense,
use,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react'
import React, { use, useRef } from 'react'
import { DevCycleClient, initializeDevCycle } from '@devcycle/js-client-sdk'
import { invalidateConfig } from '../../common/invalidateConfig'
import { DevCycleNextOptions, DevCycleServerData } from '../../common/types'
Expand All @@ -31,49 +23,6 @@

const isServer = typeof window === 'undefined'

/**
* Component which renders nothing, but runs code to keep client state in sync with server
* Also waits for the server's data promise with the `use` hook. This triggers the nearest suspense boundary,
* so this component is being rendered inside of a Suspense by the DevCycleClientsideProvider.
* @param serverDataPromise
* @constructor
*/
export const SuspendedProviderInitialization = ({
serverDataPromise,
}: Pick<DevCycleClientContext, 'serverDataPromise'>): React.ReactNode => {
const serverData = use(serverDataPromise)
const [previousContext, setPreviousContext] = useState<
DevCycleServerData | undefined
>()
const context = useContext(DevCycleProviderContext)

const synchronizeData = useCallback(() => {
;(context.client as DevCycleClient).synchronizeBootstrapData(
serverData.config,
serverData.user,
serverData.userAgent,
)
}, [context.client, serverData])

if (!previousContext) {
synchronizeData()
}

useEffect(() => {
if (previousContext !== serverData) {
// change user and config data to match latest server data
// if the data has changed since the last invocation
// assert this is a DevCycleClient, not a DevCycleNextClient, because it is. We expose a more limited type
// to the end user
if (previousContext) {
synchronizeData()
}
setPreviousContext(serverData)
}
}, [synchronizeData, serverData, previousContext])
return null
}

export const InternalDevCycleClientsideProvider = ({
context,
children,
Expand Down Expand Up @@ -145,6 +94,17 @@
resolvedServerData.user,
resolvedServerData.userAgent,
)
} else {
// if the promise isnt resolved yet, schedule it to synchronize when it is
// we check the above condition first to make sure we can use the resolved data on the first render pass
// since the `.then` here is only evaluated after this render pass is finished
serverDataPromise.then((serverData) => {
clientRef.current!.synchronizeBootstrapData(

Check warning on line 102 in sdk/nextjs/src/client/internal/InternalDevCycleClientsideProvider.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Forbidden non-null assertion
serverData.config,
serverData.user,
serverData.userAgent,
)
})
}
}

Expand All @@ -157,11 +117,6 @@
serverDataPromise,
}}
>
<Suspense>
<SuspendedProviderInitialization
serverDataPromise={serverDataPromise}
/>
</Suspense>
{children}
</DevCycleProviderContext.Provider>
)
Expand Down
Loading