11// https://www.netlify.com/docs/headers-and-basic-auth/
22
3+ import { promises as fs } from 'fs'
4+ import { join } from 'path'
5+
36import { generatePageDataPath } from 'gatsby-core-utils'
47import 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