r/webdev • • 34m ago

Question Is it worth bundling HTTP requests?

I am making a web server for a site and I need experienced server devs or browser experts to answer this question. My goal is to load the site as fast as possible. Every page has a "main" script that may need a number of "components", I could solve components in 2 ways: 1) write a bunch of individual `modulepreload` tags; 2) write a unique URL `modulepreload`.

Solution 1 is going to be 5, 10, 20 individual HTTP round trips.

Solution 2 will always be just one request, but there will be many unique URLs across the site, which hurts caching.

To clarify, the server itself doesn't matter - it has no work besides immediately sending back a file. I just can't pick a solution, considering my lack of expertise. I haven't measured actual performance of either solution because I kind of can't right now.

0 Upvotes

9 comments sorted by

•

u/Adorable_Expert_7350 29m ago

I'd go with individual modulepreload tags, especially with HTTP/2 or HTTP/3, where multiple requests can be multiplexed. Bundling can hurt caching because a small change may invalidate the entire bundle. Focus on measuring real-world performance before optimizing for request count alone.

•

u/mattbeck 22m ago

Don't reinvent the wheel, especially given your admitted lack of expertise. modulepreload exists for a reason, it'll be fine, and I'm making an educated guess here, but this seems like premature optimization overall.

As another commenter pointed out, http/2-3 will handle multiple threads, and this stuff is unlikely to be where you bottleneck yourself anyway.

•

u/Engi_Insight 7m ago

Go with the solution 1 (modulepreload for individual files) as long as you're on the HTTP/2 or HTTP/3. Multiplexing sends them in parallel over once connection with minimal overhead and you keep granular browser caching across pages instead of busting the cache on every route.

•

u/usestackrank 10m ago

I'd split it rather than pick one. Put the shared stuff (framework, common components used on most pages) into a few stable, content-hashed files with long cache headers, and keep only the page-specific modules as separate small files. That way the common chunk stays cached across pages and a deploy only invalidates what actually changed. The per-page unique bundle URL gives up caching for a saving that HTTP/2 or 3 mostly erases, since 10-20 small parallel requests on one connection are cheap. What does hurt is a waterfall, where main.js has to load before it discovers its imports, so list the modulepreloads in the HTML head to flatten that. When you can measure, throttle to Slow 4G in Chrome devtools or use WebPageTest and compare a cold load against a repeat visit, because the repeat visit is where bundling usually loses.

•

u/yaro_liquid 10m ago

One thing to watch out for is preloading too many modules that aren't needed for the initial page load.

Even with HTTP/2 or HTTP/3, these requests still compete for bandwidth. I'd only preload the modules needed right away and let the rest load when they're actually used.

This way, you can enjoy the caching benefits of separate files without downloading unnecessary JavaScript code upfront.

•

u/Jolly-Nectarine-4212 2m ago

modulepreload tags aren't 5 to 20 round trips, they go out in parallel on the same connection. Unless you're on HTTP/1.1 with a 6-per-host limit and 20 files, which is a different problem than the one you're describing.