Progressive Bundling
Tags: serverless, modules, bundling, javascript
Published: 2020-04-07 19:29:18

---
title: Progressive Bundling
published: true
description: A dynamic module bundling pattern for modern web development
tags: serverless, modules, bundling, JavaScript
---
#### A dynamic module bundling pattern for modern web development

Until recently, the use of JavaScript modules required a build step to make them run in the browser. Since the general availability of ES6 modules in browsers, it is possible to use JavaScript’s built-in module system. What this means is we no longer require a build step to be able to write modular code that runs in the browser natively.  Modules also can benefit from the addition of parallel loading made available with HTTP/2.

As we all know, with every development choice, there are new considerations that ultimately emerge over time.  

For instance, with a build-step based workflow, your build time can start to take longer, and your compiler configuration can become exceedingly more complex as your project’s source code grows.

Modules don’t require the overhead of a build step, which is great, but loading too many sub-modules at once can create a bottleneck that can increase the time it takes to load your web page completely.

![sub-modules loading in parallel](https://dev-to-uploads.s3.amazonaws.com/i/1nfnc42xx4tj4mun7yfq.jpg)

It is possible to get a long way without running into this issue, but if it does arise, cutting down on requests can help.  

#### Enter Progressive Bundling
This pattern uses a cloud function to optimize the amount of JavaScript sent to the browser by bundling together modules and their sub-modules, into a single request, on-demand. Bundling these files together cuts down on the number of files being loaded in parallel by the browser. This bundled file is then “fingerprinted,” or named with an appended id representing the file’s contents. We then cache this generated file with this fingerprinted name for subsequent requests. The request is then redirected with a 302 status code to the newly bundled, fingerprinted, and cached file.

![progressive bundling](https://dev-to-uploads.s3.amazonaws.com/i/qntq4l649w64p58u116u.jpg)

What makes this progressive is that you can start with stock JavaScript for development, which is easier to debug, then layer on bundling with a cloud function, and a standard HTTP redirect for an optimized build if you ever need it.

#### A delicate balance
It is always important to audit your site performance to understand when to employ optimization. It is possible to cut down on requests, but end-up loading too large a file that ultimately negatively impacts performance. The best bet is to always load as little JavaScript as possible.

#### Take it farther
Think about how you could apply this approach to delivering other payloads such as CSS or icons. You could even add a transpiler if you wanted.
 
#### [Check out the Progressive Bundling Example](https://dev.to/begin/progressive-bundling-example-4855)
Where we step through the code of an example app using this pattern. Deploy your own Progressive Bundling example app on [Begin](https://begin.com).
<br/>
<a style="position:absolute;left:0;" href="https://begin.com/apps/create?template=https://github.com/begin-examples/node-progressive-bundling" target="_blank"><img src="https://static.begin.com/deploy-to-begin.svg" alt="Deploy to Begin" id="deploy-to-begin"></a>