简体   繁体   中英

NextJS reduce First Load of shared JS

In a bigger project, I have a fairly high amount of used third-party libraries, including firebase, redux, etc and some more specific ones (but only used on several pages) like konvaJS, jimp, .... I just migrated recently to nextJS to speed up the website & maybe allow SSR. However, after migrating, the Lighthouse Speedtest dropped compared to the Pure React Version. The main problem seems to be the shared JS first Load bundles.

After some optimizing, including lazy loading bigger Components with dynamic() & modules with await import() , I managed to compress the shared first load JS bundles by half, but they are still around 400KB which is way too heavy. I guess heavy modules like firebase are included there as well, because it is needed basically everywhere in the App.

I also tried to analyze the dependencies with @next/bundle-analyzer . But the Visualization is not really easy to interpret. Is it true that it also lists modules that are lazy loaded? And in addition, I have some dependency packed multiple times in different bundles. Last but not least, the bundles visualized by the analyzer do not match the names of the build output.

在此处输入图像描述

Any help to reduce or understand the process better is well appreciated. I am using current React & Next.js versions.

Edit: This is the composition of the shared JS bundles after build: 在此处输入图像描述

Edit2: I am still confused about the output of bundle-analyzer. The module jspdf for instance, is only used in one page / component and lazy loaded. Is it correct that it is still visible in the analyzer? It does not have any impact on the shared JS bundle size.

Edit3: I managed to lazy load my firebase modules (which are crucial for my App), so I saved over 200KB shared JS size. Still hoping to cut down the remaining. Btw, the bundle analyzers output did not really change. 在此处输入图像描述

Are you using FontAwesome? We were able to reduce our "First Load JS shared by all" from 504kb down to 276kb by removing FontAwesome dependency and downloading the individual.svg icons directly.

Before

FirstLoadJS 之前

After

后

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM