Skip to content

Commit

Permalink
fix: caching of user and bucketed config (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Oct 24, 2024
1 parent 9adf98e commit c7e36a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
22 changes: 12 additions & 10 deletions sdk/nextjs/src/server/bucketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import {
} from '../common/types'
import { BucketedUserConfig, ConfigBody, ConfigSource } from '@devcycle/types'

const getPopulatedUser = cache((user: DevCycleUser, userAgent?: string) => {
return new DVCPopulatedUser(
user,
{},
undefined,
undefined,
userAgent ?? undefined,
)
})

// wrap this function in react cache to avoid redoing work for the same user and config
const generateBucketedConfigCached = cache(
async (
Expand All @@ -16,13 +26,7 @@ const generateBucketedConfigCached = cache(
config: ConfigBody,
userAgent?: string,
) => {
const populatedUser = new DVCPopulatedUser(
user,
{},
undefined,
undefined,
userAgent ?? undefined,
)
const populatedUser = getPopulatedUser(user, userAgent)

// clientSDKKey is always defined for bootstrap config
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -33,12 +37,10 @@ const generateBucketedConfigCached = cache(
obfuscated,
populatedUser,
)
const response =
(await bucketedConfigResponse.json()) as BucketedUserConfig

return {
bucketedConfig: {
...response,
...bucketedConfigResponse,
clientSDKKey,
},
}
Expand Down
30 changes: 18 additions & 12 deletions sdk/nextjs/src/server/requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DVCPopulatedUser } from '@devcycle/js-client-sdk'
import { serializeUserSearchParams } from '../common/serializeUser'
import { cache } from 'react'
import { BucketedUserConfig } from '@devcycle/types'

const getFetchUrl = (sdkKey: string, obfuscated: boolean) =>
`https://config-cdn.devcycle.com/config/v2/server/bootstrap/${
Expand Down Expand Up @@ -39,15 +41,19 @@ const getSDKAPIUrl = (
return `https://sdk-api.devcycle.com/v1/sdkConfig?${searchParams.toString()}`
}

export const sdkConfigAPI = async (
sdkKey: string,
obfuscated: boolean,
user: DVCPopulatedUser,
): Promise<Response> => {
return await fetch(getSDKAPIUrl(sdkKey, obfuscated, user), {
next: {
revalidate: 60,
tags: [sdkKey, user.user_id],
},
})
}
export const sdkConfigAPI = cache(
async (
sdkKey: string,
obfuscated: boolean,
user: DVCPopulatedUser,
): Promise<BucketedUserConfig> => {
const response = await fetch(getSDKAPIUrl(sdkKey, obfuscated, user), {
next: {
revalidate: 60,
tags: [sdkKey, user.user_id],
},
})

return (await response.json()) as BucketedUserConfig
},
)

0 comments on commit c7e36a0

Please sign in to comment.