Skip to content

Commit 5561d8d

Browse files
authored
fix: memoize config CDN call (#1000)
1 parent d5f148a commit 5561d8d

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

sdk/nextjs/src/server/bucketing.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
BucketedConfigWithAdditionalFields,
77
DevCycleNextOptions,
88
} from '../common/types'
9-
import { BucketedUserConfig, ConfigBody, ConfigSource } from '@devcycle/types'
9+
import { ConfigBody, ConfigSource } from '@devcycle/types'
1010

1111
const getPopulatedUser = cache((user: DevCycleUser, userAgent?: string) => {
1212
return new DVCPopulatedUser(
@@ -67,18 +67,14 @@ class CDNConfigSource extends ConfigSource {
6767
}
6868
async getConfig(sdkKey: string, kind: string, obfuscated: boolean) {
6969
// this request will be cached by Next
70-
const cdnConfig = await fetchCDNConfig(
70+
const { config, headers } = await fetchCDNConfig(
7171
sdkKey,
7272
this.clientSDKKey,
7373
obfuscated,
7474
)
75-
if (!cdnConfig.ok) {
76-
const responseText = await cdnConfig.text()
77-
throw new Error('Could not fetch config: ' + responseText)
78-
}
7975
return {
80-
config: (await cdnConfig.json()) as ConfigBody,
81-
lastModified: cdnConfig.headers.get('last-modified'),
76+
config: config,
77+
lastModified: headers.get('last-modified'),
8278
metaData: {},
8379
}
8480
}

sdk/nextjs/src/server/requests.ts

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
import { DVCPopulatedUser } from '@devcycle/js-client-sdk'
22
import { serializeUserSearchParams } from '../common/serializeUser'
33
import { cache } from 'react'
4-
import { BucketedUserConfig } from '@devcycle/types'
4+
import { BucketedUserConfig, ConfigBody } from '@devcycle/types'
55

66
const getFetchUrl = (sdkKey: string, obfuscated: boolean) =>
77
`https://config-cdn.devcycle.com/config/v2/server/bootstrap/${
88
obfuscated ? 'obfuscated/' : ''
99
}${sdkKey}.json`
1010

11-
export const fetchCDNConfig = async (
12-
sdkKey: string,
13-
clientSDKKey: string,
14-
obfuscated: boolean,
15-
): Promise<Response> => {
16-
return await fetch(
17-
getFetchUrl(sdkKey, obfuscated),
18-
// only store for 60 seconds
19-
{
20-
next: {
21-
revalidate: 60,
22-
tags: [sdkKey, clientSDKKey],
11+
export const fetchCDNConfig = cache(
12+
async (
13+
sdkKey: string,
14+
clientSDKKey: string,
15+
obfuscated: boolean,
16+
): Promise<{ config: ConfigBody; headers: Headers }> => {
17+
const response = await fetch(
18+
getFetchUrl(sdkKey, obfuscated),
19+
// only store for 60 seconds
20+
{
21+
next: {
22+
revalidate: 60,
23+
tags: [sdkKey, clientSDKKey],
24+
},
2325
},
24-
},
25-
)
26-
}
26+
)
27+
28+
if (!response.ok) {
29+
const responseText = await response.text()
30+
throw new Error('Could not fetch config: ' + responseText)
31+
}
32+
return {
33+
config: (await response.json()) as ConfigBody,
34+
headers: response.headers,
35+
}
36+
},
37+
)
2738

2839
const getSDKAPIUrl = (
2940
sdkKey: string,

0 commit comments

Comments
 (0)