Skip to content

Commit 1d5feee

Browse files
committed
fix: handle custom canary environment
1 parent 577c10e commit 1d5feee

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

config/env.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ export const gitBranch = process.env.BRANCH || process.env.VERCEL_GIT_COMMIT_REF
4444
* Whether this is the canary environment (main.npmx.dev).
4545
*
4646
* Detected as any non-PR Vercel deploy from the `main` branch
47-
* (which may receive `VERCEL_ENV === 'production'` or `'preview'`
48-
* depending on the project's production branch configuration).
47+
* (which may receive `VERCEL_ENV === 'production'`, `'preview'`, or a
48+
* custom `'canary'` environment depending on the project configuration).
4949
*
5050
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_ENV}
5151
*/
5252
export const isCanary =
53-
(process.env.VERCEL_ENV === 'production' || process.env.VERCEL_ENV === 'preview') &&
53+
(process.env.VERCEL_ENV === 'production' ||
54+
process.env.VERCEL_ENV === 'preview' ||
55+
process.env.VERCEL_ENV === 'canary') &&
5456
gitBranch === 'main' &&
5557
!isPR
5658

test/unit/config/env.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ describe('isCanary', () => {
4343
expect(isCanary).toBe(true)
4444
})
4545

46+
it('returns true when VERCEL_ENV is custom "canary" and branch is "main"', async () => {
47+
vi.stubEnv('VERCEL_ENV', 'canary')
48+
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
49+
const { isCanary } = await import('../../../config/env')
50+
51+
expect(isCanary).toBe(true)
52+
})
53+
4654
it('returns false when VERCEL_ENV is "preview", branch is "main", but is a PR', async () => {
4755
vi.stubEnv('VERCEL_ENV', 'preview')
4856
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
@@ -97,6 +105,15 @@ describe('getEnv', () => {
97105
expect(result.env).toBe('canary')
98106
})
99107

108+
it('returns "canary" for custom Vercel "canary" environment on main branch', async () => {
109+
vi.stubEnv('VERCEL_ENV', 'canary')
110+
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
111+
const { getEnv } = await import('../../../config/env')
112+
const result = await getEnv(false)
113+
114+
expect(result.env).toBe('canary')
115+
})
116+
100117
it('returns "preview" for Vercel preview PR deploys', async () => {
101118
vi.stubEnv('VERCEL_ENV', 'preview')
102119
vi.stubEnv('VERCEL_GIT_PULL_REQUEST_ID', '123')

0 commit comments

Comments
 (0)