1
1
// https://www.netlify.com/docs/headers-and-basic-auth/
2
2
3
3
import WebpackAssetsManifest from "webpack-assets-manifest"
4
-
4
+ import { generatePageDataPath } from "gatsby-core-utils"
5
5
import makePluginData from "./plugin-data"
6
6
import buildHeadersProgram from "./build-headers-program"
7
7
import createRedirects from "./create-redirects"
8
- import { DEFAULT_OPTIONS , BUILD_HTML_STAGE , BUILD_CSS_STAGE } from "./constants"
8
+ import {
9
+ DEFAULT_OPTIONS ,
10
+ BUILD_HTML_STAGE ,
11
+ BUILD_CSS_STAGE ,
12
+ PAGE_COUNT_WARN ,
13
+ } from "./constants"
9
14
10
15
const assetsManifest = { }
11
16
@@ -14,7 +19,6 @@ exports.onCreateWebpackConfig = ({ actions, stage }) => {
14
19
if ( stage !== BUILD_HTML_STAGE && stage !== BUILD_CSS_STAGE ) {
15
20
return
16
21
}
17
-
18
22
actions . setWebpackConfig ( {
19
23
plugins : [
20
24
new WebpackAssetsManifest ( {
@@ -32,20 +36,48 @@ exports.onPostBuild = async (
32
36
const pluginData = makePluginData ( store , assetsManifest , pathPrefix )
33
37
const pluginOptions = { ...DEFAULT_OPTIONS , ...userPluginOptions }
34
38
35
- const { redirects } = store . getState ( )
36
-
37
- let rewrites = [ ]
38
- if ( pluginOptions . generateMatchPathRewrites ) {
39
- const { pages } = store . getState ( )
40
- rewrites = Array . from ( pages . values ( ) )
41
- . filter ( page => page . matchPath && page . matchPath !== page . path )
42
- . map ( page => {
43
- return {
44
- fromPath : page . matchPath ,
45
- toPath : page . path ,
39
+ const { redirects, pages } = store . getState ( )
40
+ if (
41
+ pages . size > PAGE_COUNT_WARN &&
42
+ ( pluginOptions . mergeCachingHeaders || pluginOptions . mergeLinkHeaders )
43
+ ) {
44
+ reporter . warn (
45
+ `[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`
46
+ )
47
+ }
48
+ reporter . info ( `[gatsby-plugin-netlify] Creating SSR redirects...` )
49
+ let count = 0
50
+ const rewrites = [ ]
51
+ Array . from ( pages . values ( ) ) . forEach ( page => {
52
+ const { mode, matchPath, path } = page
53
+ if ( mode === `SSR` ) {
54
+ count ++
55
+ rewrites . push (
56
+ {
57
+ fromPath : matchPath ?? path ,
58
+ toPath : `/.netlify/functions/__ssr` ,
59
+ } ,
60
+ {
61
+ fromPath : generatePageDataPath ( `/` , matchPath ?? path ) ,
62
+ toPath : `/.netlify/functions/__ssr` ,
46
63
}
64
+ )
65
+ }
66
+ if (
67
+ pluginOptions . generateMatchPathRewrites &&
68
+ page . matchPath !== page . path
69
+ ) {
70
+ rewrites . push ( {
71
+ fromPath : page . matchPath ,
72
+ toPath : page . path ,
47
73
} )
48
- }
74
+ }
75
+ } )
76
+ reporter . info (
77
+ `[gatsby-plugin-netlify] Created ${ count } SSR redirect${
78
+ count === 1 ? `` : `s`
79
+ } ...`
80
+ )
49
81
50
82
await Promise . all ( [
51
83
buildHeadersProgram ( pluginData , pluginOptions , reporter ) ,
@@ -66,21 +98,21 @@ const pluginOptionsSchema = function ({ Joi }) {
66
98
. items ( Joi . string ( ) )
67
99
. description ( `Add more headers to all the pages` ) ,
68
100
mergeSecurityHeaders : Joi . boolean ( ) . description (
69
- `When set to true , turns off the default security headers`
101
+ `When set to false , turns off the default security headers`
70
102
) ,
71
103
mergeLinkHeaders : Joi . boolean ( ) . description (
72
- `When set to true , turns off the default gatsby js headers`
104
+ `When set to false , turns off the default gatsby js headers`
73
105
) ,
74
106
mergeCachingHeaders : Joi . boolean ( ) . description (
75
- `When set to true , turns off the default caching headers`
107
+ `When set to false , turns off the default caching headers`
76
108
) ,
77
109
transformHeaders : Joi . function ( )
78
110
. maxArity ( 2 )
79
111
. description (
80
112
`Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array<string> }`
81
113
) ,
82
114
generateMatchPathRewrites : Joi . boolean ( ) . description (
83
- `When set to true , turns off automatic creation of redirect rules for client only paths`
115
+ `When set to false , turns off automatic creation of redirect rules for client only paths`
84
116
) ,
85
117
} )
86
118
}
0 commit comments