1
1
import _ from "lodash"
2
2
import { createWriteStream , existsSync } from "fs-extra"
3
- import { parse , posix } from "path"
4
- import kebabHash from "kebab-hash"
5
- import { fixedPagePath } from "gatsby-core-utils"
6
3
import { IMMUTABLE_CACHING_HEADER } from "./constants"
7
4
8
5
import {
9
- COMMON_BUNDLES ,
10
6
SECURITY_HEADERS ,
11
7
CACHING_HEADERS ,
12
8
LINK_REGEX ,
13
9
HEADERS_FILENAME ,
14
- PAGE_DATA_DIR ,
15
10
} from "./constants"
16
11
import { emitHeaders } from "./ipc"
17
12
@@ -20,131 +15,10 @@ function getHeaderName(header) {
20
15
return matches && matches [ 1 ]
21
16
}
22
17
23
- function validHeaders ( headers , reporter ) {
24
- if ( ! headers || ! _ . isObject ( headers ) ) {
25
- return false
26
- }
27
-
28
- return _ . every (
29
- headers ,
30
- ( headersList , path ) =>
31
- _ . isArray ( headersList ) &&
32
- _ . every ( headersList , header => {
33
- if ( _ . isString ( header ) ) {
34
- if ( ! getHeaderName ( header ) ) {
35
- // TODO panic on builds on v3
36
- reporter . warn (
37
- `[gatsby-plugin-gatsby-cloud] ${ path } contains an invalid header (${ header } ). Please check your plugin configuration`
38
- )
39
- }
40
-
41
- return true
42
- }
43
-
44
- return false
45
- } )
46
- )
47
- }
48
-
49
- function linkTemplate ( assetPath , type = `script` ) {
50
- return `Link: <${ assetPath } >; rel=preload; as=${ type } ${
51
- type === `fetch` ? `; crossorigin` : ``
52
- } ; nopush`
53
- }
54
-
55
- function pathChunkName ( path ) {
56
- const name = path === `/` ? `index` : kebabHash ( path )
57
- return `path---${ name } `
58
- }
59
-
60
- function getPageDataPath ( path ) {
61
- return posix . join ( `page-data` , fixedPagePath ( path ) , `page-data.json` )
62
- }
63
-
64
- function getScriptPath ( file , manifest ) {
65
- const chunk = manifest [ file ]
66
-
67
- if ( ! chunk ) {
68
- return [ ]
69
- }
70
-
71
- // convert to array if it's not already
72
- const chunks = _ . isArray ( chunk ) ? chunk : [ chunk ]
73
-
74
- return chunks . filter ( script => {
75
- const parsed = parse ( script )
76
- // handle only .js, .css content is inlined already
77
- // and doesn't need to be pushed
78
- return parsed . ext === `.js`
79
- } )
80
- }
81
-
82
- function linkHeaders ( files , pathPrefix , assetPrefix ) {
83
- const linkHeaders = [ ]
84
- for ( const resourceType in files ) {
85
- files [ resourceType ] . forEach ( file => {
86
- linkHeaders . push (
87
- linkTemplate (
88
- `${ assetPrefix ? assetPrefix + `/` : `` } ${ pathPrefix } /${ file } ` ,
89
- resourceType
90
- )
91
- )
92
- } )
93
- }
94
-
95
- return linkHeaders
96
- }
97
-
98
18
function headersPath ( pathPrefix , path ) {
99
19
return `${ pathPrefix } ${ path } `
100
20
}
101
21
102
- function preloadHeadersByPage ( {
103
- pages,
104
- manifest,
105
- pathPrefix,
106
- publicFolder,
107
- assetPrefix,
108
- } ) {
109
- const linksByPage = { }
110
-
111
- const appDataPath = publicFolder ( PAGE_DATA_DIR , `app-data.json` )
112
- const hasAppData = existsSync ( appDataPath )
113
-
114
- pages . forEach ( page => {
115
- const scripts = _ . flatMap ( COMMON_BUNDLES , file =>
116
- getScriptPath ( file , manifest )
117
- )
118
- scripts . push ( ...getScriptPath ( pathChunkName ( page . path ) , manifest ) )
119
- scripts . push ( ...getScriptPath ( page . componentChunkName , manifest ) )
120
-
121
- const json = [ ]
122
- if ( hasAppData ) {
123
- json . push ( posix . join ( PAGE_DATA_DIR , `app-data.json` ) )
124
- }
125
-
126
- // page-data gets inline for SSR, so we won't be doing page-data request
127
- // and we shouldn't add preload link header for it.
128
- if ( page . mode !== `SSR` ) {
129
- json . push ( getPageDataPath ( page . path ) )
130
- }
131
-
132
- const filesByResourceType = {
133
- script : scripts . filter ( Boolean ) ,
134
- fetch : json ,
135
- }
136
-
137
- const pathKey = headersPath ( pathPrefix , page . path )
138
- linksByPage [ pathKey ] = linkHeaders (
139
- filesByResourceType ,
140
- pathPrefix ,
141
- assetPrefix
142
- )
143
- } )
144
-
145
- return linksByPage
146
- }
147
-
148
22
function defaultMerge ( ...headers ) {
149
23
function unionMerge ( objValue , srcValue ) {
150
24
if ( _ . isArray ( objValue ) ) {
@@ -200,21 +74,6 @@ function transformLink(manifest, publicFolder, pathPrefix) {
200
74
} )
201
75
}
202
76
203
- function stringifyHeaders ( headers ) {
204
- return _ . reduce (
205
- headers ,
206
- ( text , headerList , path ) => {
207
- const headersString = _ . reduce (
208
- headerList ,
209
- ( accum , header ) => `${ accum } ${ header } \n` ,
210
- ``
211
- )
212
- return `${ text } ${ path } \n${ headersString } `
213
- } ,
214
- ``
215
- )
216
- }
217
-
218
77
// program methods
219
78
220
79
const mapUserLinkHeaders =
@@ -247,26 +106,6 @@ const mapUserLinkAllPageHeaders =
247
106
return defaultMerge ( headers , duplicateHeadersByPage )
248
107
}
249
108
250
- const applyLinkHeaders =
251
- ( pluginData , { mergeLinkHeaders } ) =>
252
- headers => {
253
- if ( ! mergeLinkHeaders ) {
254
- return headers
255
- }
256
-
257
- const { pages, manifest, pathPrefix, publicFolder, assetPrefix } =
258
- pluginData
259
- const perPageHeaders = preloadHeadersByPage ( {
260
- pages,
261
- manifest,
262
- pathPrefix,
263
- publicFolder,
264
- assetPrefix,
265
- } )
266
-
267
- return defaultMerge ( headers , perPageHeaders )
268
- }
269
-
270
109
const applySecurityHeaders =
271
110
( { mergeSecurityHeaders } ) =>
272
111
headers => {
@@ -371,7 +210,6 @@ export default function buildHeadersProgram(pluginData, pluginOptions) {
371
210
applySecurityHeaders ( pluginOptions ) ,
372
211
applyCachingHeaders ( pluginData , pluginOptions ) ,
373
212
mapUserLinkAllPageHeaders ( pluginData , pluginOptions ) ,
374
- applyLinkHeaders ( pluginData , pluginOptions ) ,
375
213
applyTransfromHeaders ( pluginOptions ) ,
376
214
saveHeaders ( pluginData )
377
215
) ( pluginOptions . headers )
0 commit comments