r/webdev • • 1h 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.

1 Upvotes

19 comments sorted by

6

u/Adorable_Expert_7350 1h 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.

4

u/mattbeck 1h 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.

3

u/Engi_Insight 1h 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.

2

u/yaro_liquid 1h 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.

1

u/usestackrank 1h 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.

1

u/Jolly-Nectarine-4212 1h 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.

1

u/Desperate-Flow320 57m ago

20 round trips, hell no.

1

u/Otherwise-Horse-3670 53m ago

Small thing, but those aren't round trips, they're requests. With HTTP/2 the 20 modulepreload tags go out over the same connection and mostly in parallel, so counting them as 20 round trips makes solution 1 look far worse than it is.

1

u/Ok-Snow-6553 42m ago

Solution 1 is the modern way. Bundling everything into one URL means you lose granular caching. Just make sure your server is on HTTP/2 so the multiple requests don't cause head-of-line blocking.

1

u/M_Me_Meteo 41m ago

Profile it. Load testing a server isn't hard and can be cone locally with a powerful enough machine.

•

u/IncomeOk7237 22m ago

Since your server is just sending files, the first thing I'd check is whether it actually speaks HTTP/2 over TLS. On HTTP/1.1 the browser opens about 6 connections per host, so 20 preloads really do queue up. On h2 they all share one connection and the cost of 20 small files mostly goes away. Easy to check in devtools, turn on the Protocol column in the network tab.

If you go with individual files, put a content hash in each filename and serve them with Cache-Control: public, max-age=31536000, immutable. Then a repeat visit costs zero requests for anything that didn't change, which a per page bundle can never give you since every page has its own unique URL.

Middle ground that's worked for me: one shared chunk for the stuff every page needs, plus small per page modules. And measure with throttling on (Fast 3G or so), on localhost the difference is basically noise.

•

u/Ok_Car_8951 10m ago

modulepreload tags all start together on http2, so it is not 20 round trips one by one, you check network tab once

•

u/AntiqueGuest8172 8m ago

with 20 components you're looking at roughly a million combonations for that single URL, so your CDN basically misses every time and you've traded caching for nothing. 20 immutable files that never change sound a lot better to me.

•

u/Slight-Persimmon-445 8m ago

Small thing: 20 modulepreload tags isn't 20 round trips. They get fetched in parallel over the same connection, so it's one round. The round trips come from the depth of the import chain, not from the number of tags.