Skip to content

Commit 1bf112e

Browse files
authored
fix(gatsby): reduce page-renderer-size (#33051)
1 parent 0aa96d3 commit 1bf112e

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

packages/gatsby/cache-dir/static-entry.js

+30-18
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const {
66
pipeToNodeWritable,
77
} = require(`react-dom/server`)
88
const { ServerLocation, Router, isRedirect } = require(`@gatsbyjs/reach-router`)
9-
const { merge, flattenDeep, replace } = require(`lodash`)
9+
const merge = require(`deepmerge`)
1010
const { StaticQueryContext } = require(`gatsby`)
1111
const fs = require(`fs`)
12+
const { WritableAsPromise } = require(`./server-utils/writable-as-promise`)
1213

1314
const { RouteAnnouncerProps } = require(`./route-announcer-props`)
1415
const { apiRunner, apiRunnerAsync } = require(`./api-runner-ssr`)
@@ -65,29 +66,40 @@ const getAppDataUrl = () =>
6566
const createElement = React.createElement
6667

6768
export const sanitizeComponents = components => {
68-
const componentsArray = ensureArray(components)
69+
const componentsArray = [].concat(components).flat(Infinity).filter(Boolean)
70+
6971
return componentsArray.map(component => {
7072
// Ensure manifest is always loaded from content server
7173
// And not asset server when an assetPrefix is used
7274
if (__ASSET_PREFIX__ && component.props.rel === `manifest`) {
7375
return React.cloneElement(component, {
74-
href: replace(component.props.href, __ASSET_PREFIX__, ``),
76+
href: component.props.href.replace(__ASSET_PREFIX__, ``),
7577
})
7678
}
7779
return component
7880
})
7981
}
8082

81-
const ensureArray = components => {
82-
if (Array.isArray(components)) {
83-
// remove falsy items and flatten
84-
return flattenDeep(
85-
components.filter(val => (Array.isArray(val) ? val.length > 0 : val))
86-
)
87-
} else {
88-
// we also accept single components, so we need to handle this case as well
89-
return components ? [components] : []
83+
function deepMerge(a, b) {
84+
const combineMerge = (target, source, options) => {
85+
const destination = target.slice()
86+
87+
source.forEach((item, index) => {
88+
if (typeof destination[index] === `undefined`) {
89+
destination[index] = options.cloneUnlessOtherwiseSpecified(
90+
item,
91+
options
92+
)
93+
} else if (options.isMergeableObject(item)) {
94+
destination[index] = merge(target[index], item, options)
95+
} else if (target.indexOf(item) === -1) {
96+
destination.push(item)
97+
}
98+
})
99+
return destination
90100
}
101+
102+
return merge(a, b, { arrayMerge: combineMerge })
91103
}
92104

93105
export default async function staticPage({
@@ -149,11 +161,13 @@ export default async function staticPage({
149161
}
150162

151163
const setHtmlAttributes = attributes => {
152-
htmlAttributes = merge(htmlAttributes, attributes)
164+
// TODO - we should remove deep merges
165+
htmlAttributes = deepMerge(htmlAttributes, attributes)
153166
}
154167

155168
const setBodyAttributes = attributes => {
156-
bodyAttributes = merge(bodyAttributes, attributes)
169+
// TODO - we should remove deep merges
170+
bodyAttributes = deepMerge(bodyAttributes, attributes)
157171
}
158172

159173
const setPreBodyComponents = components => {
@@ -169,7 +183,8 @@ export default async function staticPage({
169183
}
170184

171185
const setBodyProps = props => {
172-
bodyProps = merge({}, bodyProps, props)
186+
// TODO - we should remove deep merges
187+
bodyProps = deepMerge({}, bodyProps, props)
173188
}
174189

175190
const getHeadComponents = () => headComponents
@@ -266,9 +281,6 @@ export default async function staticPage({
266281
try {
267282
// react 18 enabled
268283
if (pipeToNodeWritable) {
269-
const {
270-
WritableAsPromise,
271-
} = require(`./server-utils/writable-as-promise`)
272284
const writableStream = new WritableAsPromise()
273285
const { startWriting } = pipeToNodeWritable(
274286
bodyComponent,

packages/gatsby/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"css.escape": "^1.5.1",
5252
"date-fns": "^2.14.0",
5353
"debug": "^3.2.7",
54+
"deepmerge": "^4.2.2",
5455
"del": "^5.1.0",
5556
"detect-port": "^1.3.0",
5657
"devcert": "^1.1.3",

packages/gatsby/src/internal-plugins/bundle-optimisations/gatsby-node.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ exports.onCreateWebpackConfig = function onCreateWebpackConfig({
1111
stage,
1212
actions,
1313
}) {
14+
const objectAssignStub = path.join(__dirname, `polyfills/object-assign.js`)
15+
1416
if (stage === `build-html` || stage === `develop-html`) {
17+
actions.setWebpackConfig({
18+
resolve: {
19+
alias: {
20+
// These files are already polyfilled so these should return in a no-op
21+
// Stub Package: object.assign & object-assign
22+
"object.assign": objectAssignStub,
23+
"object-assign$": objectAssignStub,
24+
"@babel/runtime/helpers/extends.js$": objectAssignStub,
25+
},
26+
},
27+
})
1528
return
1629
}
1730

1831
const noOp = path.join(__dirname, `polyfills/no-op.js`)
19-
const objectAssignStub = path.join(__dirname, `polyfills/object-assign.js`)
2032
const fetchStub = path.join(__dirname, `polyfills/fetch.js`)
2133
const whatwgFetchStub = path.join(__dirname, `polyfills/whatwg-fetch.js`)
2234

packages/gatsby/src/utils/webpack.config.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ module.exports = async (
302302

303303
function getMode() {
304304
switch (stage) {
305-
case `build-javascript`:
306-
return `production`
307305
case `develop`:
308306
case `develop-html`:
307+
return `development`
308+
case `build-javascript`:
309309
case `build-html`:
310-
return `development` // So we don't uglify the html bundle
310+
return `production`
311311
default:
312312
return `production`
313313
}
@@ -531,6 +531,7 @@ module.exports = async (
531531
}
532532

533533
const isCssModule = module => module.type === `css/mini-extract`
534+
534535
if (stage === `develop`) {
535536
config.optimization = {
536537
splitChunks: {
@@ -564,7 +565,13 @@ module.exports = async (
564565
},
565566
},
566567
},
567-
minimizer: [],
568+
minimize: false,
569+
}
570+
}
571+
572+
if (stage === `build-html`) {
573+
config.optimization = {
574+
minimize: false,
568575
}
569576
}
570577

0 commit comments

Comments
 (0)