Skip to content

Commit

Permalink
feat: do not pre-warm lambdas if DEPLOY_PRIME_URL is not set (#435)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ericapisani committed Jul 13, 2022
1 parent b73b5b5 commit 9c0e8ba
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
5 changes: 4 additions & 1 deletion plugin/src/helpers/config.ts
Expand Up @@ -22,7 +22,10 @@ import type { FunctionList } from './functions'
* Checks to see if GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled
*/
export function shouldSkipBundlingDatastore(): boolean {
return isEnvSet('GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE')
return (
isEnvSet('GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE') &&
Boolean(process.env.DEPLOY_PRIME_URL)
)
}

export async function spliceConfig({
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/index.ts
Expand Up @@ -102,7 +102,7 @@ export async function onSuccess() {
}, FETCH_TIMEOUT)

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

try {
Expand Down
8 changes: 8 additions & 0 deletions plugin/test/helpers.js
@@ -1,5 +1,8 @@
const Chance = require('chance')
const execa = require('execa')

const chance = new Chance()

module.exports.buildSite = async () => {
const { exitCode, stdout, stderr } = await execa(
'netlify',
Expand All @@ -12,3 +15,8 @@ module.exports.buildSite = async () => {
severityCode: exitCode,
}
}

module.exports.enableGatsbyExcludeDatastoreFromBundle = () => {
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
process.env.DEPLOY_PRIME_URL = chance.url()
}
16 changes: 15 additions & 1 deletion plugin/test/unit/helpers/config.spec.ts
@@ -1,6 +1,8 @@
/* eslint-disable ava/no-import-test-files */
import { resolve, join } from 'path'
import process from 'process'

import Chance from 'chance'
import { remove, copy, readJSON, readdir } from 'fs-extra'
import { dir as getTmpDir } from 'tmp-promise'
import { validate } from 'uuid'
Expand All @@ -10,7 +12,9 @@ import {
mutateConfig,
shouldSkipBundlingDatastore,
} from '../../../src/helpers/config'
import { enableGatsbyExcludeDatastoreFromBundle } from '../../helpers'

const chance = new Chance()
const SAMPLE_PROJECT_DIR = `${__dirname}/../../../../demo`
const TEST_TIMEOUT = 60_000

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

describe('shouldSkipBundlingDatastore', () => {
beforeEach(() => {
process.env.DEPLOY_PRIME_URL = chance.url()
})
afterEach(() => {
delete process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE
delete process.env.DEPLOY_PRIME_URL
})

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

delete process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE
expect(shouldSkipBundlingDatastore()).toEqual(false)

// Commonly occurs in a CI context as a DEPLOY_PRIME_URL is not set
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
delete process.env.DEPLOY_PRIME_URL
expect(shouldSkipBundlingDatastore()).toEqual(false)
})
})

Expand Down Expand Up @@ -85,7 +98,7 @@ describe('mutateConfig', () => {
})

it('includes the dataMetadata file containing gatsby datastore info when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled', () => {
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
enableGatsbyExcludeDatastoreFromBundle()
mutateConfig(defaultArgs)

expect(netlifyConfig.functions.__api).toStrictEqual({
Expand Down Expand Up @@ -259,3 +272,4 @@ describe('createMetadataFileAndCopyDatastore', () => {
TEST_TIMEOUT,
)
})
/* eslint-enable ava/no-import-test-files */
27 changes: 18 additions & 9 deletions plugin/test/unit/index.spec.ts
@@ -1,4 +1,4 @@
/* eslint-disable max-nested-callbacks */
/* eslint-disable max-nested-callbacks, ava/no-import-test-files */
import process from 'process'

import {
Expand All @@ -8,6 +8,7 @@ import {
import Chance from 'chance'

import { onBuild, onSuccess } from '../../src/index'
import { enableGatsbyExcludeDatastoreFromBundle } from '../helpers'

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

it('creates the metadata file for the Gatsby datastore when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled', async () => {
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
enableGatsbyExcludeDatastoreFromBundle()

createMetadataFileAndCopyDatastore.mockImplementation(() =>
Promise.resolve(),
)
Expand Down Expand Up @@ -211,31 +213,30 @@ describe('plugin', () => {

beforeEach(() => {
fetch.mockImplementation(mockFetchMethod)
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
process.env.URL = chance.url()
enableGatsbyExcludeDatastoreFromBundle()
})

afterEach(() => {
delete process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE
delete process.env.URL
delete process.env.DEPLOY_PRIME_URL
})

it('makes requests to pre-warm the lambdas if GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled', async () => {
await onSuccess()
const controller = new AbortController()
expect(fetch).toHaveBeenNthCalledWith(
1,
`${process.env.URL}/.netlify/functions/__api`,
`${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__api`,
{ signal: controller.signal },
)
expect(fetch).toHaveBeenNthCalledWith(
2,
`${process.env.URL}/.netlify/functions/__dsg`,
`${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__dsg`,
{ signal: controller.signal },
)
expect(fetch).toHaveBeenNthCalledWith(
3,
`${process.env.URL}/.netlify/functions/__ssr`,
`${process.env.DEPLOY_PRIME_URL}/.netlify/functions/__ssr`,
{ signal: controller.signal },
)
})
Expand All @@ -255,6 +256,14 @@ describe('plugin', () => {

expect(fetch).toBeCalledTimes(0)
})

it('does not make requests to pre-warm the lambdas if process.env.DEPLOY_PRIME_URL is not defined', async () => {
delete process.env.DEPLOY_PRIME_URL

await onSuccess()

expect(fetch).toBeCalledTimes(0)
})
})
})
/* eslint-enable max-nested-callbacks */
/* eslint-enable max-nested-callbacks, ava/no-import-test-files */
7 changes: 5 additions & 2 deletions plugin/test/unit/templates/utils.spec.ts
@@ -1,3 +1,4 @@
/* eslint-disable ava/no-import-test-files */
import { tmpdir } from 'os'
import { resolve, join, dirname } from 'path'

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

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

const chance = new Chance()
const SAMPLE_PROJECT_DIR = `${__dirname}/../../../../demo`
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('prepareFilesystem', () => {
it(
'downloads file from the CDN when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled',
async () => {
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
enableGatsbyExcludeDatastoreFromBundle()
await moveGatsbyDir()

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

process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
enableGatsbyExcludeDatastoreFromBundle()
await moveGatsbyDir()

const cacheDir = resolve('.cache')
Expand Down Expand Up @@ -157,3 +159,4 @@ describe('downloadFile', () => {
)
})
})
/* eslint-enable ava/no-import-test-files */

0 comments on commit 9c0e8ba

Please sign in to comment.