Replies: 4 comments 3 replies
|
I found another option 3. It was inspired by this article https://arunoda.me/blog/ssr-and-server-only-modules |
0 replies
|
Also, I build 2 sites to check if it is worth it: Sorry, no images due to local Sitecore. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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/sitecore-jss@21.5.2 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. (
debugwe 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-jsslibrary 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.jsonand movegraphqltopeerDependenciesnode_modules\@sitecore-jss\sitecore-jss\node_modules\graphqlandnode_modules\@sitecore-jss\sitecore-jss\node_modules\graphql-requestfolder to make sure that this libraries are not usedpeerDependenciesby@sitecore-jss\sitecore-jsspackage 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_modulesfolder 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_modulesusing some additional npm tasks@sitecore-jss/sitecore-jsswith no extra dependenciesnode_modulesfolder content by adding it to GITAdditional information
If you need some extra information - I am open for a call.
All reactions