Skip to content

Commit d4ed977

Browse files
authored
feat: disable Netlify functions if not needed (#65)
1 parent 6e4cc7e commit d4ed977

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/gatsby-node.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// https://www.netlify.com/docs/headers-and-basic-auth/
22

3+
import { promises as fs } from 'fs'
4+
import { join } from 'path'
5+
36
import { generatePageDataPath } from 'gatsby-core-utils'
47
import WebpackAssetsManifest from 'webpack-assets-manifest'
58

@@ -59,31 +62,39 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }, userPluginOpt
5962
const pluginData = makePluginData(store, assetsManifest, pathPrefix)
6063
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions }
6164

62-
const { redirects, pages } = store.getState()
65+
const { redirects, pages, functions = [], program } = store.getState()
6366
if (pages.size > PAGE_COUNT_WARN && (pluginOptions.mergeCachingHeaders || pluginOptions.mergeLinkHeaders)) {
6467
reporter.warn(
6568
`[gatsby-plugin-netlify] Your site has ${pages.size} pages, which means that the generated headers file could become very large. Consider disabling "mergeCachingHeaders" and "mergeLinkHeaders" in your plugin config`,
6669
)
6770
}
6871
reporter.info(`[gatsby-plugin-netlify] Creating SSR redirects...`)
72+
6973
let count = 0
7074
const rewrites = []
75+
76+
let needsFunctions = functions.length !== 0
77+
7178
;[...pages.values()].forEach((page) => {
7279
const { mode, matchPath, path } = page
80+
if (mode === 'SSR' || mode === 'DSG') {
81+
needsFunctions = true
82+
}
7383
if (mode === `SSR`) {
84+
const fromPath = matchPath ?? path
7485
count++
7586
rewrites.push(
7687
{
77-
fromPath: matchPath ?? path,
88+
fromPath,
7889
toPath: `/.netlify/functions/__ssr`,
7990
},
8091
{
81-
fromPath: generatePageDataPath(`/`, matchPath ?? path),
92+
fromPath: generatePageDataPath(`/`, fromPath),
8293
toPath: `/.netlify/functions/__ssr`,
8394
},
8495
)
8596
}
86-
if (pluginOptions.generateMatchPathRewrites && matchPath !== path) {
97+
if (pluginOptions.generateMatchPathRewrites && matchPath && matchPath !== path) {
8798
rewrites.push({
8899
fromPath: matchPath,
89100
toPath: path,
@@ -92,6 +103,11 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }, userPluginOpt
92103
})
93104
reporter.info(`[gatsby-plugin-netlify] Created ${count} SSR redirect${count === 1 ? `` : `s`}...`)
94105

106+
if (!needsFunctions) {
107+
reporter.info(`[gatsby-plugin-netlify] No Netlify functions needed. Skipping...`)
108+
await fs.writeFile(join(program.directory, `.cache`, `.nf-skip-gatsby-functions`), '')
109+
}
110+
95111
await Promise.all([
96112
buildHeadersProgram(pluginData, pluginOptions, reporter),
97113
createRedirects(pluginData, redirects, rewrites),

0 commit comments

Comments
 (0)