Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions sdk/nextjs/src/server/bucketing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { fetchCDNConfig, sdkConfigAPI } from './requests'
import {
fetchCDNConfig,
getOptInUsersFromConfigApi,
sdkConfigAPI,
} from './requests'
import { generateBucketedConfig } from '@devcycle/bucketing'
import { cache } from 'react'
import { DevCycleUser, DVCPopulatedUser } from '@devcycle/js-client-sdk'
Expand Down Expand Up @@ -31,7 +35,13 @@ const generateBucketedConfigCached = cache(
// clientSDKKey is always defined for bootstrap config
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const clientSDKKey = config.clientSDKKey!
if (config.debugUsers?.includes(user.user_id ?? '')) {
const optInUsers = config.project.settings.optIn?.enabled
? await getOptInUsersFromConfigApi(clientSDKKey)
: []
if (
config.debugUsers?.includes(user.user_id ?? '') ||
optInUsers.includes(user.user_id ?? '')
) {
const bucketedConfigResponse = await sdkConfigAPI(
clientSDKKey,
obfuscated,
Expand Down
29 changes: 19 additions & 10 deletions sdk/nextjs/src/server/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ export const fetchCDNConfig = cache(
clientSDKKey: string,
obfuscated: boolean,
): Promise<{ config: ConfigBody; headers: Headers }> => {
const response = await fetch(
getFetchUrl(sdkKey, obfuscated),
// only store for 60 seconds
{
next: {
revalidate: 60,
tags: [sdkKey, clientSDKKey],
},
const url = getFetchUrl(sdkKey, obfuscated)
const response = await fetch(url, {
next: {
revalidate: 60,
tags: [sdkKey, clientSDKKey],
},
)
})

if (!response.ok) {
const responseText = await response.text()
Expand All @@ -37,6 +34,8 @@ export const fetchCDNConfig = cache(
},
)

const getAPIUrl = (path: string) => `https://sdk-api.devcycle.com/v1/${path}`

const getSDKAPIUrl = (
sdkKey: string,
obfuscated: boolean,
Expand All @@ -50,7 +49,7 @@ const getSDKAPIUrl = (
}
searchParams.set('sdkPlatform', 'nextjs')
searchParams.set('sse', '1')
return `https://sdk-api.devcycle.com/v1/sdkConfig?${searchParams.toString()}`
return getAPIUrl(`sdkConfig?${searchParams.toString()}`)
}

export const sdkConfigAPI = cache(
Expand All @@ -69,3 +68,13 @@ export const sdkConfigAPI = cache(
return (await response.json()) as BucketedUserConfig
},
)

export const getOptInUsersFromConfigApi = cache(
async (sdkKey: string): Promise<string[]> => {
const response = await fetch(getAPIUrl(`optIns/users?sdkKey=${sdkKey}`))
if (!response.ok) {
return []
}
return (await response.json()).users
},
)
Loading