Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit a93b1af

Browse files
committed
fix: throw an error if we are passing an ambiguous set of options to the purge api
Prior to this commit, a call to `purgeCache` could supply any combination of siteID, siteSlug, and domain. If that does happen then the purge api endpoint as of today will do the following: - If the request passed in SiteID and either SiteSlug or Domain, then the SiteID would be used - If the request passed in SiteSlug and Domain, then the SiteSlug would be used This is likely not what the user is wanting. I've opened a pull-request in netlify-server to make it return an error response instead, but I think we can also improve the situation in this package by throwing an earlier error, one which they the user can spot when developing locally
1 parent 364fa59 commit a93b1af

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/lib/purge_cache.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => {
3838
)
3939
}
4040

41+
const { siteID } = options as PurgeCacheOptionsWithSiteID
42+
const { siteSlug } = options as PurgeCacheOptionsWithSiteSlug
43+
const { domain } = options as PurgeCacheOptionsWithDomain
44+
45+
if ((siteID && siteSlug) || (siteID && domain) || (siteSlug && domain)) {
46+
throw new Error('Can only pass one of either "siteID", "siteSlug", or "domain"')
47+
}
48+
4149
const payload: PurgeAPIPayload = {
4250
cache_tags: options.tags,
4351
deploy_alias: options.deployAlias,
@@ -50,22 +58,20 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => {
5058
return
5159
}
5260

53-
if ('siteSlug' in options) {
54-
payload.site_slug = options.siteSlug
55-
} else if ('domain' in options) {
56-
payload.domain = options.domain
61+
if (siteSlug) {
62+
payload.site_slug = siteSlug
63+
} else if (domain) {
64+
payload.domain = domain
5765
} else {
5866
// The `siteID` from `options` takes precedence over the one from the
5967
// environment.
60-
const siteID = options.siteID || env.SITE_ID
68+
payload.site_id = siteID || env.SITE_ID
6169

62-
if (!siteID) {
70+
if (!payload.site_id) {
6371
throw new Error(
6472
'The Netlify site ID was not found in the execution environment. Please supply it manually using the `siteID` property.',
6573
)
6674
}
67-
68-
payload.site_id = siteID
6975
}
7076

7177
if (!token) {

0 commit comments

Comments
 (0)