Unverified Commit 41675c07 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

edge: use JSON parse instead of inlining the manifests objects (#50960)

Manifests tend to be really big and our strategy is to inline them in the edge function script itself and we're hitting some constraints this way. 

This PR switches this behaviour via inlining the script and creating a JS object at runtime instead, with the hope that the parsing time will be faster as JSON.parse is very well optimised in V8.

The end goal ofc is to have smaller manifests but this should be a good stopgap.



link NEXT-1273
parent 8370e52a
...@@ -2930,7 +2930,9 @@ export default async function build( ...@@ -2930,7 +2930,9 @@ export default async function build(
) )
await promises.writeFile( await promises.writeFile(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'), path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
`self.__PRERENDER_MANIFEST=${JSON.stringify(prerenderManifest)}`, `self.__PRERENDER_MANIFEST=${JSON.stringify(
JSON.stringify(prerenderManifest)
)}`,
'utf8' 'utf8'
) )
await generateClientSsgManifest(prerenderManifest, { await generateClientSsgManifest(prerenderManifest, {
...@@ -2953,7 +2955,9 @@ export default async function build( ...@@ -2953,7 +2955,9 @@ export default async function build(
) )
await promises.writeFile( await promises.writeFile(
path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'), path.join(distDir, PRERENDER_MANIFEST).replace(/\.json$/, '.js'),
`self.__PRERENDER_MANIFEST=${JSON.stringify(prerenderManifest)}`, `self.__PRERENDER_MANIFEST=${JSON.stringify(
JSON.stringify(prerenderManifest)
)}`,
'utf8' 'utf8'
) )
} }
......
...@@ -137,16 +137,20 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -137,16 +137,20 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
: 'const incrementalCacheHandler = null' : 'const incrementalCacheHandler = null'
} }
const maybeJSONParse = (str) => str ? JSON.parse(str) : undefined
const buildManifest = self.__BUILD_MANIFEST const buildManifest = self.__BUILD_MANIFEST
const prerenderManifest = self.__PRERENDER_MANIFEST const prerenderManifest = maybeJSONParse(self.__PRERENDER_MANIFEST)
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = maybeJSONParse(self.__REACT_LOADABLE_MANIFEST)
const rscManifest = self.__RSC_MANIFEST const rscManifest = maybeJSONParse(self.__RSC_MANIFEST)
const rscCssManifest = self.__RSC_CSS_MANIFEST const rscCssManifest = maybeJSONParse(self.__RSC_CSS_MANIFEST)
const rscServerManifest = self.__RSC_SERVER_MANIFEST const rscServerManifest = maybeJSONParse(self.__RSC_SERVER_MANIFEST)
const subresourceIntegrityManifest = ${ const subresourceIntegrityManifest = ${
sriEnabled ? 'self.__SUBRESOURCE_INTEGRITY_MANIFEST' : 'undefined' sriEnabled
? 'maybeJSONParse(self.__SUBRESOURCE_INTEGRITY_MANIFEST)'
: 'undefined'
} }
const nextFontManifest = self.__NEXT_FONT_MANIFEST const nextFontManifest = maybeJSONParse(self.__NEXT_FONT_MANIFEST)
const render = getRender({ const render = getRender({
pagesType, pagesType,
......
...@@ -585,7 +585,7 @@ export class ClientReferenceEntryPlugin { ...@@ -585,7 +585,7 @@ export class ClientReferenceEntryPlugin {
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
assets[`${this.assetPrefix}${FLIGHT_SERVER_CSS_MANIFEST}.js`] = assets[`${this.assetPrefix}${FLIGHT_SERVER_CSS_MANIFEST}.js`] =
new sources.RawSource( new sources.RawSource(
'self.__RSC_CSS_MANIFEST=' + manifest `self.__RSC_CSS_MANIFEST=${JSON.stringify(manifest)}`
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
} }
) )
...@@ -990,7 +990,7 @@ export class ClientReferenceEntryPlugin { ...@@ -990,7 +990,7 @@ export class ClientReferenceEntryPlugin {
assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.js`] = assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.js`] =
new sources.RawSource( new sources.RawSource(
'self.__RSC_SERVER_MANIFEST=' + json `self.__RSC_SERVER_MANIFEST=${JSON.stringify(json)}`
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.json`] = assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.json`] =
new sources.RawSource(json) as unknown as webpack.sources.RawSource new sources.RawSource(json) as unknown as webpack.sources.RawSource
......
...@@ -396,7 +396,7 @@ export class ClientReferenceManifestPlugin { ...@@ -396,7 +396,7 @@ export class ClientReferenceManifestPlugin {
pluginState.ASYNC_CLIENT_MODULES = [] pluginState.ASYNC_CLIENT_MODULES = []
assets[file + '.js'] = new sources.RawSource( assets[file + '.js'] = new sources.RawSource(
'self.__RSC_MANIFEST=' + json `self.__RSC_MANIFEST=${JSON.stringify(json)}`
// Work around webpack 4 type of RawSource being used // Work around webpack 4 type of RawSource being used
// TODO: use webpack 5 type by default // TODO: use webpack 5 type by default
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
......
...@@ -144,10 +144,10 @@ export class NextFontManifestPlugin { ...@@ -144,10 +144,10 @@ export class NextFontManifestPlugin {
} }
} }
const manifest = JSON.stringify(nextFontManifest, null, 2) const manifest = JSON.stringify(nextFontManifest, null)
// Create manifest for edge // Create manifest for edge
assets[`server/${NEXT_FONT_MANIFEST}.js`] = new sources.RawSource( assets[`server/${NEXT_FONT_MANIFEST}.js`] = new sources.RawSource(
`self.__NEXT_FONT_MANIFEST=${manifest}` `self.__NEXT_FONT_MANIFEST=${JSON.stringify(manifest)}`
) )
// Create manifest for server // Create manifest for server
assets[`server/${NEXT_FONT_MANIFEST}.json`] = new sources.RawSource( assets[`server/${NEXT_FONT_MANIFEST}.json`] = new sources.RawSource(
......
...@@ -179,7 +179,9 @@ export class ReactLoadablePlugin { ...@@ -179,7 +179,9 @@ export class ReactLoadablePlugin {
) )
if (this.runtimeAsset) { if (this.runtimeAsset) {
assets[this.runtimeAsset] = new sources.RawSource( assets[this.runtimeAsset] = new sources.RawSource(
`self.__REACT_LOADABLE_MANIFEST=${JSON.stringify(manifest)}` `self.__REACT_LOADABLE_MANIFEST=${JSON.stringify(
JSON.stringify(manifest)
)}`
) )
} }
return assets return assets
......
...@@ -48,7 +48,7 @@ export class SubresourceIntegrityPlugin { ...@@ -48,7 +48,7 @@ export class SubresourceIntegrityPlugin {
const json = JSON.stringify(hashes, null, 2) const json = JSON.stringify(hashes, null, 2)
const file = 'server/' + SUBRESOURCE_INTEGRITY_MANIFEST const file = 'server/' + SUBRESOURCE_INTEGRITY_MANIFEST
assets[file + '.js'] = new sources.RawSource( assets[file + '.js'] = new sources.RawSource(
'self.__SUBRESOURCE_INTEGRITY_MANIFEST=' + json `self.__SUBRESOURCE_INTEGRITY_MANIFEST=${JSON.stringify(json)}`
// Work around webpack 4 type of RawSource being used // Work around webpack 4 type of RawSource being used
// TODO: use webpack 5 type by default // TODO: use webpack 5 type by default
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment