Skip to content

Commit 9236053

Browse files
ascorbicmraerino
andauthoredFeb 13, 2024
fix: skip purgeCache in local dev (#472)
**Which problem is this pull request solving?** Currently running `purgeCache` in local development throws an error. While I wouldn't expect it to actually purge anything (because there's no cache anyway), this prevents us running dev for any site that does use this. **Describe the solution you've chosen** Log a message and return if called in local dev without a token. If a token is passed I assume the user knows what they're doing and wants it to work for real. **Describe alternatives you've considered** Example: Another solution would be [...] **Checklist** Please add a `x` inside each checkbox: - [ ] I have read the [contribution guidelines](../blob/master/CONTRIBUTING.md). - [ ] The status checks are successful (continuous integration). Those can be seen below. --------- Co-authored-by: Marcus Weiner <marcus.weiner@gmail.com>
1 parent 83e4750 commit 9236053

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
 

‎src/lib/purge_cache.ts

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface PurgeAPIPayload {
2929
site_slug?: string
3030
}
3131

32+
// eslint-disable-next-line complexity
3233
export const purgeCache = async (options: PurgeCacheOptions = {}) => {
3334
if (globalThis.fetch === undefined) {
3435
throw new Error(
@@ -42,6 +43,12 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => {
4243
}
4344
const token = env.NETLIFY_PURGE_API_TOKEN || options.token
4445

46+
if (env.NETLIFY_LOCAL && !token) {
47+
const scope = options.tags?.length ? ` for tags ${options.tags?.join(', ')}` : ''
48+
console.log(`Skipping purgeCache${scope} in local development.`)
49+
return
50+
}
51+
4552
if ('siteSlug' in options) {
4653
payload.site_slug = options.siteSlug
4754
} else if ('domain' in options) {

‎test/unit/purge_cache.js

+26
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const hasFetchAPI = semver.gte(process.version, '18.0.0')
1313
test.beforeEach(() => {
1414
delete process.env.NETLIFY_PURGE_API_TOKEN
1515
delete process.env.SITE_ID
16+
delete process.env.NETLIFY_LOCAL
1617
})
1718

1819
test.afterEach(() => {
@@ -90,3 +91,28 @@ test.serial('Throws if the API response does not have a successful status code',
9091
'Cache purge API call returned an unexpected status code: 500',
9192
)
9293
})
94+
95+
test.serial('Ignores purgeCache if in local dev with no token or site', async (t) => {
96+
if (!hasFetchAPI) {
97+
console.warn('Skipping test requires the fetch API')
98+
99+
return t.pass()
100+
}
101+
102+
process.env.NETLIFY_LOCAL = '1'
103+
104+
const mockAPI = new MockFetch().post({
105+
body: () => {
106+
t.fail()
107+
}
108+
})
109+
const myFunction = async () => {
110+
await purgeCache()
111+
}
112+
113+
globalThis.fetch = mockAPI.fetcher
114+
115+
const response = await invokeLambda(myFunction)
116+
117+
t.is(response, undefined)
118+
})

0 commit comments

Comments
 (0)
Please sign in to comment.