1
1
// https://www.netlify.com/docs/headers-and-basic-auth/
2
2
3
+ import { promises as fs } from 'fs'
4
+ import { join } from 'path'
5
+
3
6
import { generatePageDataPath } from 'gatsby-core-utils'
4
7
import WebpackAssetsManifest from 'webpack-assets-manifest'
5
8
@@ -59,31 +62,39 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }, userPluginOpt
59
62
const pluginData = makePluginData ( store , assetsManifest , pathPrefix )
60
63
const pluginOptions = { ...DEFAULT_OPTIONS , ...userPluginOptions }
61
64
62
- const { redirects, pages } = store . getState ( )
65
+ const { redirects, pages, functions = [ ] , program } = store . getState ( )
63
66
if ( pages . size > PAGE_COUNT_WARN && ( pluginOptions . mergeCachingHeaders || pluginOptions . mergeLinkHeaders ) ) {
64
67
reporter . warn (
65
68
`[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` ,
66
69
)
67
70
}
68
71
reporter . info ( `[gatsby-plugin-netlify] Creating SSR redirects...` )
72
+
69
73
let count = 0
70
74
const rewrites = [ ]
75
+
76
+ let needsFunctions = functions . length !== 0
77
+
71
78
; [ ...pages . values ( ) ] . forEach ( ( page ) => {
72
79
const { mode, matchPath, path } = page
80
+ if ( mode === 'SSR' || mode === 'DSG' ) {
81
+ needsFunctions = true
82
+ }
73
83
if ( mode === `SSR` ) {
84
+ const fromPath = matchPath ?? path
74
85
count ++
75
86
rewrites . push (
76
87
{
77
- fromPath : matchPath ?? path ,
88
+ fromPath,
78
89
toPath : `/.netlify/functions/__ssr` ,
79
90
} ,
80
91
{
81
- fromPath : generatePageDataPath ( `/` , matchPath ?? path ) ,
92
+ fromPath : generatePageDataPath ( `/` , fromPath ) ,
82
93
toPath : `/.netlify/functions/__ssr` ,
83
94
} ,
84
95
)
85
96
}
86
- if ( pluginOptions . generateMatchPathRewrites && matchPath !== path ) {
97
+ if ( pluginOptions . generateMatchPathRewrites && matchPath && matchPath !== path ) {
87
98
rewrites . push ( {
88
99
fromPath : matchPath ,
89
100
toPath : path ,
@@ -92,6 +103,11 @@ export const onPostBuild = async ({ store, pathPrefix, reporter }, userPluginOpt
92
103
} )
93
104
reporter . info ( `[gatsby-plugin-netlify] Created ${ count } SSR redirect${ count === 1 ? `` : `s` } ...` )
94
105
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
+
95
111
await Promise . all ( [
96
112
buildHeadersProgram ( pluginData , pluginOptions , reporter ) ,
97
113
createRedirects ( pluginData , redirects , rewrites ) ,
0 commit comments