Skip to content

Commit 9c0e8ba

Browse files
authored
feat: do not pre-warm lambdas if DEPLOY_PRIME_URL is not set (#435)
* feat: do not pre-warm lambdas if url is not set * test: forgot to remove the focus on a test * refactor: move DEPLOY_PRIME_URL check into shouldSkipBundlingDatastore
1 parent b73b5b5 commit 9c0e8ba

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

plugin/src/helpers/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import type { FunctionList } from './functions'
2222
* Checks to see if GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled
2323
*/
2424
export function shouldSkipBundlingDatastore(): boolean {
25-
return isEnvSet('GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE')
25+
return (
26+
isEnvSet('GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE') &&
27+
Boolean(process.env.DEPLOY_PRIME_URL)
28+
)
2629
}
2730

2831
export async function spliceConfig({

plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export async function onSuccess() {
102102
}, FETCH_TIMEOUT)
103103

104104
for (const func of ['api', 'dsg', 'ssr']) {
105-
const url = `${process.env.URL}/.netlify/functions/__${func}`
105+
const url = `${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__${func}`
106106
console.log(`Sending pre-warm request to: ${url}`)
107107

108108
try {

plugin/test/helpers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
const Chance = require('chance')
12
const execa = require('execa')
23

4+
const chance = new Chance()
5+
36
module.exports.buildSite = async () => {
47
const { exitCode, stdout, stderr } = await execa(
58
'netlify',
@@ -12,3 +15,8 @@ module.exports.buildSite = async () => {
1215
severityCode: exitCode,
1316
}
1417
}
18+
19+
module.exports.enableGatsbyExcludeDatastoreFromBundle = () => {
20+
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
21+
process.env.DEPLOY_PRIME_URL = chance.url()
22+
}

plugin/test/unit/helpers/config.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/* eslint-disable ava/no-import-test-files */
12
import { resolve, join } from 'path'
23
import process from 'process'
34

5+
import Chance from 'chance'
46
import { remove, copy, readJSON, readdir } from 'fs-extra'
57
import { dir as getTmpDir } from 'tmp-promise'
68
import { validate } from 'uuid'
@@ -10,7 +12,9 @@ import {
1012
mutateConfig,
1113
shouldSkipBundlingDatastore,
1214
} from '../../../src/helpers/config'
15+
import { enableGatsbyExcludeDatastoreFromBundle } from '../../helpers'
1316

17+
const chance = new Chance()
1418
const SAMPLE_PROJECT_DIR = `${__dirname}/../../../../demo`
1519
const TEST_TIMEOUT = 60_000
1620

@@ -35,8 +39,12 @@ const moveGatsbyDir = async () => {
3539
}
3640

3741
describe('shouldSkipBundlingDatastore', () => {
42+
beforeEach(() => {
43+
process.env.DEPLOY_PRIME_URL = chance.url()
44+
})
3845
afterEach(() => {
3946
delete process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE
47+
delete process.env.DEPLOY_PRIME_URL
4048
})
4149

4250
it('returns true', () => {
@@ -56,6 +64,11 @@ describe('shouldSkipBundlingDatastore', () => {
5664

5765
delete process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE
5866
expect(shouldSkipBundlingDatastore()).toEqual(false)
67+
68+
// Commonly occurs in a CI context as a DEPLOY_PRIME_URL is not set
69+
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
70+
delete process.env.DEPLOY_PRIME_URL
71+
expect(shouldSkipBundlingDatastore()).toEqual(false)
5972
})
6073
})
6174

@@ -85,7 +98,7 @@ describe('mutateConfig', () => {
8598
})
8699

87100
it('includes the dataMetadata file containing gatsby datastore info when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled', () => {
88-
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
101+
enableGatsbyExcludeDatastoreFromBundle()
89102
mutateConfig(defaultArgs)
90103

91104
expect(netlifyConfig.functions.__api).toStrictEqual({
@@ -259,3 +272,4 @@ describe('createMetadataFileAndCopyDatastore', () => {
259272
TEST_TIMEOUT,
260273
)
261274
})
275+
/* eslint-enable ava/no-import-test-files */

plugin/test/unit/index.spec.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable max-nested-callbacks */
1+
/* eslint-disable max-nested-callbacks, ava/no-import-test-files */
22
import process from 'process'
33

44
import {
@@ -8,6 +8,7 @@ import {
88
import Chance from 'chance'
99

1010
import { onBuild, onSuccess } from '../../src/index'
11+
import { enableGatsbyExcludeDatastoreFromBundle } from '../helpers'
1112

1213
jest.mock('node-fetch', () => ({
1314
__esModule: true,
@@ -158,7 +159,8 @@ describe('plugin', () => {
158159
} = require('../../src/helpers/config')
159160

160161
it('creates the metadata file for the Gatsby datastore when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled', async () => {
161-
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
162+
enableGatsbyExcludeDatastoreFromBundle()
163+
162164
createMetadataFileAndCopyDatastore.mockImplementation(() =>
163165
Promise.resolve(),
164166
)
@@ -211,31 +213,30 @@ describe('plugin', () => {
211213

212214
beforeEach(() => {
213215
fetch.mockImplementation(mockFetchMethod)
214-
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
215-
process.env.URL = chance.url()
216+
enableGatsbyExcludeDatastoreFromBundle()
216217
})
217218

218219
afterEach(() => {
219220
delete process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE
220-
delete process.env.URL
221+
delete process.env.DEPLOY_PRIME_URL
221222
})
222223

223224
it('makes requests to pre-warm the lambdas if GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled', async () => {
224225
await onSuccess()
225226
const controller = new AbortController()
226227
expect(fetch).toHaveBeenNthCalledWith(
227228
1,
228-
`${process.env.URL}/.netlify/functions/__api`,
229+
`${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__api`,
229230
{ signal: controller.signal },
230231
)
231232
expect(fetch).toHaveBeenNthCalledWith(
232233
2,
233-
`${process.env.URL}/.netlify/functions/__dsg`,
234+
`${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__dsg`,
234235
{ signal: controller.signal },
235236
)
236237
expect(fetch).toHaveBeenNthCalledWith(
237238
3,
238-
`${process.env.URL}/.netlify/functions/__ssr`,
239+
`${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__ssr`,
239240
{ signal: controller.signal },
240241
)
241242
})
@@ -255,6 +256,14 @@ describe('plugin', () => {
255256

256257
expect(fetch).toBeCalledTimes(0)
257258
})
259+
260+
it('does not make requests to pre-warm the lambdas if process.env.DEPLOY_PRIME_URL is not defined', async () => {
261+
delete process.env.DEPLOY_PRIME_URL
262+
263+
await onSuccess()
264+
265+
expect(fetch).toBeCalledTimes(0)
266+
})
258267
})
259268
})
260-
/* eslint-enable max-nested-callbacks */
269+
/* eslint-enable max-nested-callbacks, ava/no-import-test-files */

plugin/test/unit/templates/utils.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable ava/no-import-test-files */
12
import { tmpdir } from 'os'
23
import { resolve, join, dirname } from 'path'
34

@@ -14,6 +15,7 @@ import { dir as getTmpDir } from 'tmp-promise'
1415

1516
// eslint-disable-next-line import/no-namespace
1617
import * as templateUtils from '../../../src/templates/utils'
18+
import { enableGatsbyExcludeDatastoreFromBundle } from '../../helpers'
1719

1820
const chance = new Chance()
1921
const SAMPLE_PROJECT_DIR = `${__dirname}/../../../../demo`
@@ -63,7 +65,7 @@ describe('prepareFilesystem', () => {
6365
it(
6466
'downloads file from the CDN when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled',
6567
async () => {
66-
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
68+
enableGatsbyExcludeDatastoreFromBundle()
6769
await moveGatsbyDir()
6870

6971
const cacheDir = resolve('.cache')
@@ -80,7 +82,7 @@ describe('prepareFilesystem', () => {
8082
const domain = chance.url({ path: '' })
8183
const url = `${domain}${chance.word()}/${chance.word()}`
8284

83-
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
85+
enableGatsbyExcludeDatastoreFromBundle()
8486
await moveGatsbyDir()
8587

8688
const cacheDir = resolve('.cache')
@@ -157,3 +159,4 @@ describe('downloadFile', () => {
157159
)
158160
})
159161
})
162+
/* eslint-enable ava/no-import-test-files */

0 commit comments

Comments
 (0)