Skip to content

Commit 2cf1662

Browse files
committed
feat: add env vars for disabling api/ssr/dsg individually
1 parent e7e7065 commit 2cf1662

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

plugin/src/helpers/config.ts

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -186,58 +186,62 @@ export function mutateConfig({
186186
/* eslint-enable no-underscore-dangle, no-param-reassign */
187187
}
188188

189-
function shouldEnableFunctions(cacheDir: string) {
190-
if (
191-
process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' ||
192-
process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1'
193-
) {
194-
console.log(
195-
'Skipping Gatsby Functions and SSR/DSG support because the environment variable NETLIFY_SKIP_GATSBY_FUNCTIONS is set to true',
196-
)
197-
return false
198-
}
189+
export async function getNeededFunctions(
190+
cacheDir: string,
191+
): Promise<FunctionList> {
192+
if (!existsSync(path.join(cacheDir, 'functions'))) return []
199193

200-
if (!existsSync(path.join(cacheDir, 'functions'))) {
201-
console.log(
202-
`Skipping Gatsby Functions and SSR/DSG support because the site's Gatsby version does not support them`,
203-
)
204-
return false
194+
const neededFunctions = overrideNeededFunctions(
195+
await readFunctionSkipFile(cacheDir),
196+
)
197+
198+
const functionList = Object.keys(neededFunctions).filter(
199+
(name) => neededFunctions[name] === true,
200+
) as FunctionList
201+
202+
if (functionList.length === 0) {
203+
console.log('Skipping Gatsby Functions and SSR/DSG support')
204+
} else {
205+
console.log(`Enabling Gatsby ${functionList.join('/')} support`)
205206
}
206207

207-
return true
208+
return functionList
208209
}
209210

210-
export async function getNeededFunctions(
211-
cacheDir: string,
212-
): Promise<FunctionList> {
213-
if (shouldEnableFunctions(cacheDir)) {
214-
try {
215-
// read skip file from gatsby-plugin-netlify
216-
const funcObj = await fs.readJson(
217-
path.join(cacheDir, '.nf-skip-gatsby-functions'),
218-
)
219-
// filter out true values into an array
220-
const funcArr = Object.keys(funcObj).filter(
221-
(name) => funcObj[name] === true,
222-
) as FunctionList
223-
// if functions are needed, return the list
224-
if (funcArr.length !== 0) {
225-
console.log(`Enabling support for ${funcArr.join('/')}`)
226-
return funcArr
227-
}
228-
} catch (error) {
229-
if (error.code === 'ENOENT') {
230-
// no skip file
231-
console.log(`Enabling support for Gatsby Functions and SSR/DSG.`)
232-
return ['API', 'SSR', 'DSG']
233-
}
234-
console.log(
235-
// empty skip file (old plugin version)
236-
`Skipping Gatsby Functions and SSR/DSG support because gatsby-plugin-netlify reported that this site does not use them.`,
237-
)
238-
}
211+
async function readFunctionSkipFile(cacheDir: string) {
212+
try {
213+
// read skip file from gatsby-plugin-netlify
214+
return await fs.readJson(path.join(cacheDir, '.nf-skip-gatsby-functions'))
215+
} catch (error) {
216+
// missing skip file = all functions needed
217+
// empty or invalid skip file = no functions needed
218+
return error.code === 'ENOENT' ? { API: true, SSR: true, DSG: true } : {}
219+
}
220+
}
221+
222+
// eslint-disable-next-line complexity
223+
function overrideNeededFunctions(neededFunctions) {
224+
const skipAll =
225+
process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === 'true' ||
226+
process.env.NETLIFY_SKIP_GATSBY_FUNCTIONS === '1'
227+
228+
const skipAPI =
229+
process.env.NETLIFY_SKIP_API_FUNCTION === 'true' ||
230+
process.env.NETLIFY_SKIP_API_FUNCTION === '1'
231+
232+
const skipSSR =
233+
process.env.NETLIFY_SKIP_SSR_FUNCTION === 'true' ||
234+
process.env.NETLIFY_SKIP_SSR_FUNCTION === '1'
235+
236+
const skipDSG =
237+
process.env.NETLIFY_SKIP_DSG_FUNCTION === 'true' ||
238+
process.env.NETLIFY_SKIP_DSG_FUNCTION === '1'
239+
240+
return {
241+
API: skipAll || skipAPI ? false : neededFunctions.API,
242+
SSR: skipAll || skipSSR ? false : neededFunctions.SSR,
243+
DSG: skipAll || skipDSG ? false : neededFunctions.DSG,
239244
}
240-
return []
241245
}
242246

243247
export function getGatsbyRoot(publish: string): string {

0 commit comments

Comments
 (0)