diff --git a/sdk/nextjs/src/server/bucketing.ts b/sdk/nextjs/src/server/bucketing.ts index db691b01e..57453426d 100644 --- a/sdk/nextjs/src/server/bucketing.ts +++ b/sdk/nextjs/src/server/bucketing.ts @@ -6,7 +6,7 @@ import { BucketedConfigWithAdditionalFields, DevCycleNextOptions, } from '../common/types' -import { BucketedUserConfig, ConfigBody, ConfigSource } from '@devcycle/types' +import { ConfigBody, ConfigSource } from '@devcycle/types' const getPopulatedUser = cache((user: DevCycleUser, userAgent?: string) => { return new DVCPopulatedUser( @@ -67,18 +67,14 @@ class CDNConfigSource extends ConfigSource { } async getConfig(sdkKey: string, kind: string, obfuscated: boolean) { // this request will be cached by Next - const cdnConfig = await fetchCDNConfig( + const { config, headers } = await fetchCDNConfig( sdkKey, this.clientSDKKey, obfuscated, ) - if (!cdnConfig.ok) { - const responseText = await cdnConfig.text() - throw new Error('Could not fetch config: ' + responseText) - } return { - config: (await cdnConfig.json()) as ConfigBody, - lastModified: cdnConfig.headers.get('last-modified'), + config: config, + lastModified: headers.get('last-modified'), metaData: {}, } } diff --git a/sdk/nextjs/src/server/requests.ts b/sdk/nextjs/src/server/requests.ts index f9e2df7f0..5bb5f61b0 100644 --- a/sdk/nextjs/src/server/requests.ts +++ b/sdk/nextjs/src/server/requests.ts @@ -1,29 +1,40 @@ import { DVCPopulatedUser } from '@devcycle/js-client-sdk' import { serializeUserSearchParams } from '../common/serializeUser' import { cache } from 'react' -import { BucketedUserConfig } from '@devcycle/types' +import { BucketedUserConfig, ConfigBody } from '@devcycle/types' const getFetchUrl = (sdkKey: string, obfuscated: boolean) => `https://config-cdn.devcycle.com/config/v2/server/bootstrap/${ obfuscated ? 'obfuscated/' : '' }${sdkKey}.json` -export const fetchCDNConfig = async ( - sdkKey: string, - clientSDKKey: string, - obfuscated: boolean, -): Promise => { - return await fetch( - getFetchUrl(sdkKey, obfuscated), - // only store for 60 seconds - { - next: { - revalidate: 60, - tags: [sdkKey, clientSDKKey], +export const fetchCDNConfig = cache( + async ( + sdkKey: string, + 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], + }, }, - }, - ) -} + ) + + if (!response.ok) { + const responseText = await response.text() + throw new Error('Could not fetch config: ' + responseText) + } + return { + config: (await response.json()) as ConfigBody, + headers: response.headers, + } + }, +) const getSDKAPIUrl = ( sdkKey: string,