From 92360530e9edd9685c0416f379caa6913e413cfa Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 13 Feb 2024 12:42:53 +0000 Subject: [PATCH] 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 --- src/lib/purge_cache.ts | 7 +++++++ test/unit/purge_cache.js | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/lib/purge_cache.ts b/src/lib/purge_cache.ts index bb9067f5..af7b74aa 100644 --- a/src/lib/purge_cache.ts +++ b/src/lib/purge_cache.ts @@ -29,6 +29,7 @@ interface PurgeAPIPayload { site_slug?: string } +// eslint-disable-next-line complexity export const purgeCache = async (options: PurgeCacheOptions = {}) => { if (globalThis.fetch === undefined) { throw new Error( @@ -42,6 +43,12 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => { } const token = env.NETLIFY_PURGE_API_TOKEN || options.token + if (env.NETLIFY_LOCAL && !token) { + const scope = options.tags?.length ? ` for tags ${options.tags?.join(', ')}` : '' + console.log(`Skipping purgeCache${scope} in local development.`) + return + } + if ('siteSlug' in options) { payload.site_slug = options.siteSlug } else if ('domain' in options) { diff --git a/test/unit/purge_cache.js b/test/unit/purge_cache.js index d3b97dfc..6fd0ef7f 100644 --- a/test/unit/purge_cache.js +++ b/test/unit/purge_cache.js @@ -13,6 +13,7 @@ const hasFetchAPI = semver.gte(process.version, '18.0.0') test.beforeEach(() => { delete process.env.NETLIFY_PURGE_API_TOKEN delete process.env.SITE_ID + delete process.env.NETLIFY_LOCAL }) test.afterEach(() => { @@ -90,3 +91,28 @@ test.serial('Throws if the API response does not have a successful status code', 'Cache purge API call returned an unexpected status code: 500', ) }) + +test.serial('Ignores purgeCache if in local dev with no token or site', async (t) => { + if (!hasFetchAPI) { + console.warn('Skipping test requires the fetch API') + + return t.pass() + } + + process.env.NETLIFY_LOCAL = '1' + + const mockAPI = new MockFetch().post({ + body: () => { + t.fail() + } + }) + const myFunction = async () => { + await purgeCache() + } + + globalThis.fetch = mockAPI.fetcher + + const response = await invokeLambda(myFunction) + + t.is(response, undefined) +})