knocklabs / javascript

Official JavaScript packages for interacting with Knock
https://knock.app/
MIT License
13 stars 3 forks source link

The bundle size. #18

Closed Kevnz closed 7 months ago

Kevnz commented 1 year ago

We are a new customer to Knock have noticed that the bundle size is rather large. As listed on bundlephobia the size is listed as 740k, and 84% of that is in date-fns. This feels very heavy. Are you looking at removing that anytime soon or is bundle size not an important requirement?

cjbell commented 1 year ago

@Kevnz Thank you for raising this issue – bundle size is absolutely a concern and something we'll look to address given how much recent features have increased it (we added i18n support, which introduced date-fns/locales which is where the increase has come from). We'll prioritize this work over the next week and let you know when a fix is out.

ztsmith commented 1 year ago

@cjbell I was curious as to where the big bundle size increase was coming from so did a little digging... I can see that it increased by ~600kb (~100 kb gzip) from version 0.8.4: Bundlephobia gives a pretty good view of this: https://bundlephobia.com/package/@knocklabs/react-notification-feed@0.8.4

As you mentioned it was from adding i18n support in this PR. Specifically this code block where all the date-fns locales are being iterated over and included in the bundle.

There seems to be a semi-standard way of doing a dynamic import of date-fns locales, which would then allow package consumers to remove unwanted locales via some webpack magic:

That seems viable but a bit ugly. Seeing as date-fns is fairly ubiquitous I wonder if it makes more sense to expose it such that package consumers can load date-fns locales themselves and provide the notification feed package with them.

Hope that helps. For now it seems the best option is stay on version 0.8.3 if you want to avoid bundling all the locales.

connorlindsey commented 7 months ago

Hey @Kevnz and @ztsmith, thanks for calling out this issue and your suggestions. We've updated the package to use Intl for relative timestamp formatting which greatly reduced the bundle size. Upgrade to @knocklabs/react@0.1.5 for those changes Screenshot 2024-02-05 at 10 19 10 AM