Replies: 4 comments 3 replies
-
PR was created |
Beta Was this translation helpful? Give feedback.
-
I found another option 3.
It was inspired by this article https://arunoda.me/blog/ssr-and-server-only-modules |
Beta Was this translation helpful? Give feedback.
-
Also, I build 2 sites to check if it is worth it: Sorry, no images due to local Sitecore. |
Beta Was this translation helpful? Give feedback.
-
Hi @Antonytm, thanks for your detailed investigation, all of this makes sense :) I've created a task in our back log to discuss internally your suggestions (also included the pr re moving axios and graphql to peer dependencies #1705 ) |
Beta Was this translation helpful? Give feedback.
-
Is your suggestion related to a problem? Please describe.
@sitecore-jss/sitecore-jss has quite a big size.
According to bundlephobia https://bundlephobia.com/package/@sitecore-jss/[email protected] it is 31 KiB minified.
Self-code is only 7.3%. The other big parts are
graphql
,graphql-request
,axios
,cross-fetch
,debug
, andurl-parse
.These parts should be included in the client-side bundle only if we make requests on the client side. Otherwise, we don't need them in the client bundle. (
debug
we don't need at all, but that is just 4%).Let's focus on GraphQL. (The case with other libraries is similar, but it will make the explanation more clear.)
It is my feeling, but usage of GraphQL requests on the client side is an rather exception than the rule. The major part of Next.js implementations uses Integrated GraphQL + Connected GraphQL in
GetStaticProps
. See discussions in #headless channel in Sitecore Slack. But even if my feeling is wrong, even if GraphQL is used by 90% of implementations, you should not be forced to include this library in the client bundle.Test setup:
src/components/graphql/
folderIn other words, we emulate the case, when connected GraphQL is not used at all. And there is no reason to include it in the client bundle.
Analyzing bundle
I will use two approaches:
Commands to run
npm run start:production
. (npm-run-all --serial bootstrap next:build next:start
)Bundle review
Lighthouse results:
Next Bundle Analyzer:
(GraphQL is in red, and other potential gains are in yellow)
Describe the solution you'd like
Option 1 (harder, but probably the right way from architectural point of view):
I assume that the problem had happened because there were React, Vue, and Angular implementations. Then the the same core library was applied to Next.js. But Next.js is a full-stack framework. (Or backend framework with React on the client side if you wish). And if we use some library that has both server-side and client-side code then all code is added to the client bundle. Probably there is not enough magic during Next.js/Webpack bundle optimization. But it is, what it is.
@sitecore-jss
library could be split into 2 parts:In this case, the usage of the same library for React/Vue/Angular might look strange
Or another way to split:
That might be not the best for Next.js. But it will make more sense for all frameworks together. Data fetching related part. And the other.
I tried to make a prototype, but GraphQL is too tightly coupled with
sitecore-jss
, so it may take months to make a proper split.Option 2 (easy):
Use peer dependencies instead of dependencies in package.json.
Below is the scenario that I have tried to check that this approach may work.
node_modules\@sitecore-jss\sitecore-jss\package.json
and movegraphql
topeerDependencies
node_modules\@sitecore-jss\sitecore-jss\node_modules\graphql
andnode_modules\@sitecore-jss\sitecore-jss\node_modules\graphql-request
folder to make sure that this libraries are not usedpeerDependencies
by@sitecore-jss\sitecore-jss
package without changes in the package itself.npm run start:production
. (npm-run-all --serial bootstrap next:build next:start
)Lighthouse report shows a decrease in 9KiB compared to the baseline:
Next Bundle Analyzer shows a decrease in 9KiB compared to the baseline and the absence of the GraphQL library in the final bundle.
(There are some other GraphQL-related libraries. It will require an additional look, at why they are bundled as dependent.)
Of course, all these changes should be done in the package itself. But not in the
node_modules
folder with the existing package.Is there a way to achieve this functionality with exisiting tools?
All other ways that I am aware are "hacks":
node_modules
using some additional npm tasks@sitecore-jss/sitecore-jss
with no extra dependenciesnode_modules
folder content by adding it to GITAdditional information
If you need some extra information - I am open for a call.
Beta Was this translation helpful? Give feedback.
All reactions