Skip to content

Commit

Permalink
Add clientSDKKey as a tag on the config request so it can be invalida…
Browse files Browse the repository at this point in the history
…ted from the client side
  • Loading branch information
elliotCamblor committed Jul 4, 2024
1 parent 04d5e71 commit d3ef952
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,7 @@ export const InternalDevCycleClientsideProvider = ({
context

const revalidateConfig = (lastModified?: number) => {
invalidateConfig(
clientSDKKey,
!!context.options.enableObfuscation,
lastModified,
).finally(() => {
router.refresh()
})
invalidateConfig(clientSDKKey)
}

let resolvedServerData = serverData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const useRerenderOnVariableChange = (key?: string): void => {
const client = useDevCycleClient()

const variableKey = key ?? '*'

useEffect(() => {
client.subscribe(
`variableUpdated:${variableKey}`,
Expand Down
34 changes: 3 additions & 31 deletions sdk/nextjs/src/common/invalidateConfig.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
'use server'
import { fetchCDNConfig } from '../server/requests'
import { revalidateTag } from 'next/cache'

export const invalidateConfig = async (
sdkToken: string,
obfuscated: boolean,
lastModified?: number,
): Promise<void> => {
export const invalidateConfig = async (sdkToken: string): Promise<void> => {
if (typeof window != 'undefined') {
console.error(
'DevCycle realtime updates are only available in Next.js 14.0.5 and above. Please update your version ' +
'or disable realtime updates.',
)
return
}
await invalidateConfigCache(sdkToken, obfuscated, lastModified)
}

export const invalidateConfigCache = async (
sdkKey: string,
obfuscated: boolean,
lastModified?: number,
): Promise<void> => {
const response = await fetchCDNConfig(sdkKey, {
enableObfuscation: obfuscated,
})

const lastModifiedHeader = response.headers.get('last-modified')

const lastModifiedCache = new Date(lastModifiedHeader ?? 0)
const lastModifiedClient = new Date(lastModified ?? 0)

if (
lastModifiedHeader &&
lastModified &&
lastModifiedClient > lastModifiedCache
) {
console.log('Invalidating old DevCycle cached config')
revalidateTag(sdkKey)
}
console.log('Invalidating old DevCycle cached config')
revalidateTag(sdkToken)
}
1 change: 0 additions & 1 deletion sdk/nextjs/src/pages/appWithDevCycle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const appWithDevCycle = <Props extends NextJsAppProps>(
._devcycleSSR as SSRProps['_devcycleSSR']

const onServerside = typeof window === 'undefined'

if (!devcycleSSR) {
return <WrappedComponent {...props} />
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/nextjs/src/server/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const generateBucketedConfigCached = cache(
undefined,
userAgent ?? undefined,
)

return {
config: {
...generateBucketedConfig({ user: populatedUser, config }),
Expand All @@ -44,12 +43,13 @@ const generateBucketedConfigCached = cache(
*/
export const getBucketedConfig = async (
sdkKey: string,
clientSDKKey: string,
user: DevCycleUser,
options: DevCycleNextOptions,
userAgent?: string,
): Promise<BucketedConfigWithAdditionalFields> => {
// this request will be cached by Next
const cdnConfig = await fetchCDNConfig(sdkKey, options)
const cdnConfig = await fetchCDNConfig(sdkKey, clientSDKKey, options)
if (!cdnConfig.ok) {
const responseText = await cdnConfig.text()
throw new Error('Could not fetch config: ' + responseText)
Expand Down
9 changes: 8 additions & 1 deletion sdk/nextjs/src/server/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const cachedUserGetter = cache(

export const initialize = async (
sdkKey: string,
clientSDKKey: string,
userGetter: () => DevCycleUser | Promise<DevCycleUser>,
options: DevCycleNextOptions = {},
): Promise<DevCycleServerData> => {
Expand All @@ -51,7 +52,13 @@ export const initialize = async (

let config = null
try {
config = await getBucketedConfig(sdkKey, user, options, userAgent)
config = await getBucketedConfig(
sdkKey,
clientSDKKey,
user,
options,
userAgent,
)
} catch (e) {
console.error('Error fetching DevCycle config', e)
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/nextjs/src/server/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getFetchUrl = (sdkKey: string, obfuscated: boolean) =>

export const fetchCDNConfig = async (
sdkKey: string,
clientSDKKey: string,
options: DevCycleNextOptions,
): Promise<Response> => {
return await fetch(
Expand All @@ -15,7 +16,7 @@ export const fetchCDNConfig = async (
{
next: {
revalidate: 60,
tags: [sdkKey],
tags: [sdkKey, clientSDKKey],
},
},
)
Expand Down
13 changes: 9 additions & 4 deletions sdk/nextjs/src/server/setupDevCycle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ export const setupDevCycle = ({
key,
defaultValue,
) => {
await initialize(serverSDKKey, userGetter, options)
await initialize(serverSDKKey, clientSDKKey, userGetter, options)
return getVariableValue(key, defaultValue)
}

const _getAllVariables: typeof getAllVariables = async () => {
await initialize(serverSDKKey, userGetter, options)
await initialize(serverSDKKey, clientSDKKey, userGetter, options)
return getAllVariables()
}

const _getAllFeatures: typeof getAllFeatures = async () => {
await initialize(serverSDKKey, userGetter, options)
await initialize(serverSDKKey, clientSDKKey, userGetter, options)
return getAllFeatures()
}

const _getClientContext = () => {
const serverDataPromise = initialize(serverSDKKey, userGetter, options)
const serverDataPromise = initialize(
serverSDKKey,
clientSDKKey,
userGetter,
options,
)

const { enableStreaming, enableObfuscation, ...otherOptions } = options

Expand Down

0 comments on commit d3ef952

Please sign in to comment.