Speeding up Storybook with Brotli

Use brotli compression to improve Storybook load times.

Storybook recently introduced an on-demand architecture for 3x smaller builds and faster load times in the 6.4 release. I won't dive into the data or details. At a high-level, Storybook achieves this performance improvement by loading each story independently rather than packing them into a single monolithic bundle.

Now that we can bundle and lazy-load stories can be independently, I was curious if we could take one more step in optimizing our static story assets. 🤔

My friend, Dustin Younse, recommended I look into brotli, a generic-purpose lossless compression algorithm developed by Google, as a possible solution for further file shrinking.

Dustin and his team found that using brotli compression for their design system Storybook significantly improved load speeds for teams in Tokyo and Singapore (APAC). This may not be an issue if using a low-latency CDN for your teams Storybook. However, brotli compression is an easy integration that might be worth experimenting! 🧪

A quick note on compatibility. Not all static servers support delivering brotli compressed files. At the time of writing this, GitHub Pages automatically compresses and serves assets using gzip. Check your static service provider's compression compatibility or enable serving brotli if you are using your own server.

Adding Brotli

Adding brotli compression to our Storybook configuration is made convenient thanks to the to the brotli-webpack-plugin. First, install the plugin package as a dev dependency.

Then, add the plugin to the main.js webpack config for Storybook.

Build a production-ready version of the Storybook. This is usually the build-storybook script when using Storybook's default setup:

The output will include *.(js|css|html|svg) files as usual. With the plugin installed, it should now also generate *.(js|css|html|svg).br assets which will be preferred and served by brotli compatible static servers.

One way we can test the Storybook output is by using http-server static HTTP server with the -b or --brotli flag enabled. Preview it locally by running the following command:

You can verify the brotli encoding in the the Chrome DevTools Network tab.

Network tab to inspect brotli content encoding.

Subscribe to the newsletter

Be the first to know when I post something new! Thoughts about code, design, startups and other interesting things.

© 2022 Sam Rose
All Rights Reserved.