gatsbyjs/gatsby

decrease size of produced DSG/SSR engines

pieh opened this issue · 6 comments

pieh commented

Recently we made some improvements to decrease size

But engines can still get really big. There is not much we can do to minimize lmdb snapshot, but we have opportunity to minimize:

  1. sq-content: right now we generate seperate context json file for each template with all the needed static queries. This mean we potentially repeat same static query in multiple context files. We can instead just save individual static queries and have just list of hashes for each template saved so pretty much "normalize" how we store static query results
  2. We are not split chunking html gen bundle, which means that each template chunk can contain copy of same thing. We should explore split chunk config for html gen bundle - it doesn't need to produce ideal bundle, with no code duplication (that's more important for frontend perf/bandwidth) but we could at very least put shared layouts in shared chunks

References

Hi!
I can confirm builds are failing on Vercel too due to SSR having too big of a bundle.

Recently enabled SSR on our Gatsby 4 site and can also confirm that it fails in Vercel for exceeding the serverless function size limit.

pieh commented

Ideas from this issue were implemented and both are available in gatsby@5.10.0-next.1 now.

I will close this ticket as concrete ideas were finalized. Future engine size decresases ideas will have their own issues

Is this release including an SSR reducing? I thought it was for SSG but I may be wrong!

pieh commented

@ZeldOcarina

We use same html generation bundle for SSG and DSG/SSR and we (potentially*) decreased size of that in #37961 and by extension - the size of produced DSG/SSR engines. So it affect "both" SSG and DSG/SSR (as they are same thing on this level), but generally SSG aspect of it is much less important than DSG/SSR one as that one has more limitations on it

(potentially*):
This will only have effect if there was code duplication happening already in produced bundles (for example more than 1 page template using same npm package / json blob / shared components etc)

For initial testing that just used a big npm package ( https://bundlephobia.com/package/slug@2.1.1 if someone is looking for one :D) on 2 templates before those changes we were getting:

> du -sh .cache/page-ssr
 11M    .cache/page-ssr
> ls -al .cache/page-ssr/routes
total 21696
drwxr-xr-x@ 8 misiek  staff      256 Apr 19 17:12 .
drwxr-xr-x@ 5 misiek  staff      160 Apr 19 17:12 ..
-rw-r--r--@ 1 misiek  staff  2211806 Apr 19 17:12 component---src-pages-index-js.js
-rw-r--r--@ 1 misiek  staff  2724729 Apr 19 17:12 component---src-pages-index-js.js.map
-rw-r--r--@ 1 misiek  staff  2211808 Apr 19 17:12 component---src-pages-second-js.js
-rw-r--r--@ 1 misiek  staff  2724732 Apr 19 17:12 component---src-pages-second-js.js.map
-rw-r--r--@ 1 misiek  staff   550799 Apr 19 17:12 render-page.js
-rw-r--r--@ 1 misiek  staff   675251 Apr 19 17:12 render-page.js.map

and after those:

> du -sh .cache/page-ssr
5.9M    .cache/page-ssr
> ls -al .cache/page-ssr/routes
total 12072
drwxr-xr-x@ 10 misiek  staff      320 Apr 19 17:11 .
drwxr-xr-x@  5 misiek  staff      160 Apr 19 17:11 ..
-rw-r--r--@  1 misiek  staff  2209127 Apr 19 17:11 811.js
-rw-r--r--@  1 misiek  staff  2723392 Apr 19 17:11 811.js.map
-rw-r--r--@  1 misiek  staff     2760 Apr 19 17:11 component---src-pages-index-js.js
-rw-r--r--@  1 misiek  staff     1437 Apr 19 17:11 component---src-pages-index-js.js.map
-rw-r--r--@  1 misiek  staff     2762 Apr 19 17:11 component---src-pages-second-js.js
-rw-r--r--@  1 misiek  staff     1440 Apr 19 17:11 component---src-pages-second-js.js.map
-rw-r--r--@  1 misiek  staff   550896 Apr 19 17:11 render-page.js
-rw-r--r--@  1 misiek  staff   675264 Apr 19 17:11 render-page.js.map

So this change just moved the duplicated code from page templates to shared split chunk (811.js in this case) allowing for size reduction

@pieh amazing thanks for the clarification!