Skip to content

Commit 0ce31e2

Browse files
authored
fix: reduce the usage of getSiteInfo if we already have a cachedConfig (#6154)
* fix: reduce the usage of getSiteInfo if we already have a cachedConfig * test: add tests for using cached config
1 parent f91761a commit 0ce31e2

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

packages/config/src/main.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,33 @@ export const resolveConfig = async function (opts) {
7474
featureFlags,
7575
} = await normalizeOpts(optsA)
7676

77-
const { siteInfo, accounts, addons, integrations } = await getSiteInfo({
78-
api,
79-
context,
80-
siteId,
81-
accountId,
82-
mode,
83-
siteFeatureFlagPrefix,
84-
offline,
85-
featureFlags,
86-
testOpts,
87-
token,
88-
extensionApiBaseUrl,
89-
})
77+
let { siteInfo, accounts, addons, integrations } = parsedCachedConfig || {}
78+
79+
// If we have cached site info, we don't need to fetch it again
80+
const useCachedSiteInfo = Boolean(
81+
featureFlags?.use_cached_site_info && siteInfo && accounts && addons && integrations,
82+
)
83+
84+
if (!useCachedSiteInfo) {
85+
const updatedSiteInfo = await getSiteInfo({
86+
api,
87+
context,
88+
siteId,
89+
accountId,
90+
mode,
91+
siteFeatureFlagPrefix,
92+
offline,
93+
featureFlags,
94+
testOpts,
95+
token,
96+
extensionApiBaseUrl,
97+
})
98+
99+
siteInfo = updatedSiteInfo.siteInfo
100+
accounts = updatedSiteInfo.accounts
101+
addons = updatedSiteInfo.addons
102+
integrations = updatedSiteInfo.integrations
103+
}
90104

91105
const { defaultConfig: defaultConfigA, baseRelDir: baseRelDirA } = parseDefaultConfig({
92106
defaultConfig,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
command = "echo command"

packages/config/tests/api/tests.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,50 @@ test('baseRelDir is true if build.base is overridden', async (t) => {
470470
.runConfigServer([SITE_INFO_BASE_REL_DIR, FETCH_INTEGRATIONS_EMPTY_RESPONSE])
471471
t.snapshot(normalizeOutput(output))
472472
})
473+
474+
test('It does not fetch site info if cachedConfig is provided, use_cached_site_info is true and there is siteInfo, accounts, addons and integrations on cachedConfig', async (t) => {
475+
const cachedConfig = await new Fixture('./fixtures/cached_config').runWithConfigAsObject()
476+
const { requests } = await new Fixture('./fixtures/cached_config')
477+
.withFlags({
478+
cachedConfig,
479+
siteId: 'test',
480+
mode: 'dev',
481+
token: 'test',
482+
accountId: 'account1',
483+
featureFlags: {
484+
use_cached_site_info: true,
485+
},
486+
})
487+
.runConfigServer([SITE_INFO_DATA, SITE_INTEGRATIONS_RESPONSE, TEAM_INSTALLATIONS_META_RESPONSE])
488+
489+
t.assert(requests.length === 0)
490+
})
491+
492+
test('It fetches site info if cachedConfig is provided, use_cached_site_info is true and there is no siteInfo, accounts, addons or integrations on cachedConfig', async (t) => {
493+
const cachedConfig = await new Fixture('./fixtures/cached_config').runWithConfigAsObject()
494+
const { requests } = await new Fixture('./fixtures/cached_config')
495+
.withFlags({
496+
cachedConfig,
497+
siteId: 'test',
498+
mode: 'dev',
499+
token: 'test',
500+
accountId: 'account1',
501+
featureFlags: {
502+
use_cached_site_info: true,
503+
},
504+
})
505+
.runConfigServer([SITE_INFO_DATA, SITE_INTEGRATIONS_RESPONSE, TEAM_INSTALLATIONS_META_RESPONSE])
506+
507+
t.assert(requests.length === 0)
508+
})
509+
510+
test('It fetches site info if cachedConfig is provided, use_cached_site_info is false', async (t) => {
511+
const cachedConfig = await new Fixture('./fixtures/cached_config').runWithConfigAsObject()
512+
const { requests } = await new Fixture('./fixtures/cached_config')
513+
.withFlags({
514+
cachedConfig,
515+
})
516+
.runConfigServer([SITE_INFO_DATA, SITE_INTEGRATIONS_RESPONSE, TEAM_INSTALLATIONS_META_RESPONSE])
517+
518+
t.assert(requests.length === 0)
519+
})

0 commit comments

Comments
 (0)