Skip to content

Commit

Permalink
fix: skip purgeCache in local dev (#472)
Browse files Browse the repository at this point in the history
**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>
  • Loading branch information
ascorbic and mraerino committed Feb 13, 2024
1 parent 83e4750 commit 9236053
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/purge_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/purge_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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)
})

0 comments on commit 9236053

Please sign in to comment.