Skip to content

Commit 983dbd9

Browse files
mihaiblaga89pieh
authored andcommitted
fix(gatsby-plugin-netlify): add all .js files from webpack.stats.json to _headers (#12521)
## Description The issue was that only the first file was taken from each key from webpack.stats.json. Modified it to get all of them and render them accordingly. ## Related Issues Fixes to #9828
1 parent 414bc61 commit 983dbd9

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

packages/gatsby-plugin-netlify/src/build-headers-program.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from "lodash"
22
import { writeFile, existsSync } from "fs-extra"
3+
import { parse } from "path"
34
import kebabHash from "kebab-hash"
45
import { HEADER_COMMENT } from "./constants"
56

@@ -41,8 +42,18 @@ function createScriptHeaderGenerator(manifest, pathPrefix) {
4142
return null
4243
}
4344

44-
// Always add starting slash, as link entries start with slash as relative to deploy root
45-
return linkTemplate(`${pathPrefix}/${chunk}`)
45+
// convert to array if it's not already
46+
const chunks = _.isArray(chunk) ? chunk : [chunk]
47+
48+
return chunks
49+
.filter(script => {
50+
const parsed = parse(script)
51+
// handle only .js, .css content is inlined already
52+
// and doesn't need to be pushed
53+
return parsed.ext === `.js`
54+
})
55+
.map(script => linkTemplate(`${pathPrefix}/${script}`))
56+
.join(`\n `)
4657
}
4758
}
4859

packages/gatsby-plugin-netlify/src/plugin-data.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
import _ from "lodash"
21
import path from "path"
32

43
export function buildPrefixer(prefix, ...paths) {
54
return (...subpaths) => path.join(prefix, ...paths, ...subpaths)
65
}
76

8-
// Webpack stats map to an array if source maps are enabled.
9-
// We normalize to make direct map.
10-
function normalizeStats(stats) {
11-
return _.mapValues(stats.assetsByChunkName, script =>
12-
_.isArray(script) ? script[0] : script
13-
)
14-
}
15-
167
// This function assembles data across the manifests and store to match a similar
178
// shape of `static-entry.js`. With it, we can build headers that point to the correct
189
// hashed filenames and ensure we pull in the componentChunkName.
1910
export default function makePluginData(store, assetsManifest, pathPrefix) {
2011
const { program, pages: storePages } = store.getState()
2112
const publicFolder = buildPrefixer(program.directory, `public`)
2213
const stats = require(publicFolder(`webpack.stats.json`))
23-
const chunkManifest = normalizeStats(stats)
14+
// Get all the files, not just the first
15+
const chunkManifest = stats.assetsByChunkName
2416
const pages = storePages
2517

2618
// We combine the manifest of JS and the manifest of assets to make a lookup table.

0 commit comments

Comments
 (0)