Skip to content

Commit

Permalink
fix: next pages strict mode and options (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Jun 10, 2024
1 parent 2afe544 commit 02fc65b
Showing 1 changed file with 13 additions and 57 deletions.
70 changes: 13 additions & 57 deletions sdk/nextjs/src/pages/appWithDevCycle.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AppProps as NextJsAppProps } from 'next/app'
import hoistNonReactStatics from 'hoist-non-react-statics'
import { SSRProps } from './types'
import { DevCycleProvider, useDevCycleClient } from '@devcycle/react-client-sdk'
import React, { useEffect, useRef } from 'react'
import { DevCycleProvider } from '@devcycle/react-client-sdk'
import React from 'react'
import { DevCycleOptions } from '@devcycle/js-client-sdk'

type DevCycleNextOptions = Pick<
Expand All @@ -18,54 +18,6 @@ type DevCycleNextOptions = Pick<
| 'disableCustomEventLogging'
>

/**
* Component which runs a one-time sync of the server's boostrap data to the client's DevCycleClient
* @param devcycleSSR
* @param children
* @constructor
*/
const BootstrapSync = ({
devcycleSSR,
children,
}: {
devcycleSSR: SSRProps['_devcycleSSR']
children: React.ReactNode
}) => {
const client = useDevCycleClient()
const isInitializedRef = useRef(false)

if (!isInitializedRef.current) {
client.synchronizeBootstrapData(
devcycleSSR.bucketedConfig,
devcycleSSR.user,
devcycleSSR.userAgent ?? undefined,
)
isInitializedRef.current = true
}

useEffect(() => {
if (!isInitializedRef.current) {
client.synchronizeBootstrapData(
devcycleSSR.bucketedConfig,
devcycleSSR.user,
devcycleSSR.userAgent ?? undefined,
)
isInitializedRef.current = true
}
return () => {
isInitializedRef.current = false
}
}, [
devcycleSSR.bucketedConfig,
devcycleSSR.sdkKey,
devcycleSSR.user,
devcycleSSR.userAgent,
client,
])

return children
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const appWithDevCycle = <Props extends NextJsAppProps>(
WrappedComponent: React.ComponentType<Props>,
Expand All @@ -87,21 +39,25 @@ export const appWithDevCycle = <Props extends NextJsAppProps>(
<DevCycleProvider
config={{
sdkKey: devcycleSSR.sdkKey,
user: devcycleSSR.user,
options: {
...additionalOptions,
...(onServerside
? {
disableAutomaticEventLogging: true,
disableCustomEventLogging: true,
disableRealtimeUpdates: true,
}
: {}),
sdkPlatform: 'nextjs',
deferInitialization: true,
disableAutomaticEventLogging: onServerside,
disableCustomEventLogging: onServerside,
disableRealtimeUpdates: onServerside,
disableConfigCache: true,
bootstrapConfig:
devcycleSSR.bucketedConfig ?? undefined,
next: {},
},
}}
>
<BootstrapSync devcycleSSR={devcycleSSR}>
<WrappedComponent {...props} />
</BootstrapSync>
<WrappedComponent {...props} />
</DevCycleProvider>
)
}
Expand Down

0 comments on commit 02fc65b

Please sign in to comment.