Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

fix: only purge alias by default if the purgeCache function is called within a deployed function #576

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions 100 src/lib/purge_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,106 @@ test('Calls the purge API endpoint and returns `undefined` if the operation was
expect(mockAPI.fulfilled).toBeTruthy()
})

test('Does not default the deploy_alias field to process.env.NETLIFY_BRANCH if supplied in the options', async () => {
const mockSiteID = '123456789'
const mockToken = '1q2w3e4r5t6y7u8i9o0p'

process.env.NETLIFY_PURGE_API_TOKEN = mockToken
process.env.SITE_ID = mockSiteID
process.env.NETLIFY_BRANCH = 'main'

const mockAPI = new MockFetch().post({
body: (payload: string) => {
const data = JSON.parse(payload)

expect(data.site_id).toBe(mockSiteID)
expect(data.deploy_alias).toBe('test')
},
headers: { Authorization: `Bearer ${mockToken}` },
method: 'post',
response: new Response(null, { status: 202 }),
url: `https://api.netlify.com/api/v1/purge`,
})
// eslint-disable-next-line unicorn/consistent-function-scoping
const myFunction = async () => {
await purgeCache({ deployAlias: 'test' })
}

globalThis.fetch = mockAPI.fetcher

const response = await invokeLambda(myFunction)

expect(response).toBeUndefined()
expect(mockAPI.fulfilled).toBeTruthy()
})

test('Defaults the deploy_alias field to process.env.NETLIFY_BRANCH if not running locally', async () => {
const mockSiteID = '123456789'
const mockToken = '1q2w3e4r5t6y7u8i9o0p'

process.env.NETLIFY_PURGE_API_TOKEN = mockToken
process.env.SITE_ID = mockSiteID
process.env.NETLIFY_BRANCH = 'main'

const mockAPI = new MockFetch().post({
body: (payload: string) => {
const data = JSON.parse(payload)

expect(data.site_id).toBe(mockSiteID)
expect(data.deploy_alias).toBe(process.env.NETLIFY_BRANCH)
},
headers: { Authorization: `Bearer ${mockToken}` },
method: 'post',
response: new Response(null, { status: 202 }),
url: `https://api.netlify.com/api/v1/purge`,
})
// eslint-disable-next-line unicorn/consistent-function-scoping
const myFunction = async () => {
await purgeCache()
}

globalThis.fetch = mockAPI.fetcher

const response = await invokeLambda(myFunction)

expect(response).toBeUndefined()
expect(mockAPI.fulfilled).toBeTruthy()
})

test('Does not default the deploy_alias field to process.env.NETLIFY_BRANCH when running locally', async () => {
const mockSiteID = '123456789'
const mockToken = '1q2w3e4r5t6y7u8i9o0p'

process.env.NETLIFY_PURGE_API_TOKEN = mockToken
process.env.SITE_ID = mockSiteID
process.env.NETLIFY_LOCAL = 'true'
process.env.NETLIFY_BRANCH = 'main'

const mockAPI = new MockFetch().post({
body: (payload: string) => {
const data = JSON.parse(payload)

expect(data.site_id).toBe(mockSiteID)
expect(data.deploy_alias).toBeUndefined()
},
headers: { Authorization: `Bearer ${mockToken}` },
method: 'post',
response: new Response(null, { status: 202 }),
url: `https://api.netlify.com/api/v1/purge`,
})
// eslint-disable-next-line unicorn/consistent-function-scoping
const myFunction = async () => {
await purgeCache()
}

globalThis.fetch = mockAPI.fetcher

const response = await invokeLambda(myFunction)

expect(response).toBeUndefined()
expect(mockAPI.fulfilled).toBeTruthy()
})

test('Throws an error if the API response does not have a successful status code, using the response body as part of the error message', async () => {
if (!hasFetchAPI) {
console.warn('Skipping test requires the fetch API')
Expand Down
8 changes: 7 additions & 1 deletion 8 src/lib/purge_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ export const purgeCache = async (options: PurgeCacheOptions = {}) => {

const payload: PurgeAPIPayload = {
cache_tags: options.tags,
deploy_alias: options.deployAlias,
}

if ('deployAlias' in options) {
payload.deploy_alias = options.deployAlias
} else if (!env.NETLIFY_LOCAL) {
payload.deploy_alias = env.NETLIFY_BRANCH
}

const token = env.NETLIFY_PURGE_API_TOKEN || options.token

if (env.NETLIFY_LOCAL && !token) {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.