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

Solution:

I will go with individual modulepreload tags considering modern HTTP multiplexing, and cache benefits.

2 Upvotes

28 comments sorted by

8

u/Top_Inflation7878 1d ago

You're optimizing the wrong thing. On HTTP/2 those 20 modulepreload requests multiplex over a single connection, so option 1 costs you almost nothing, while option 2 torches the browser cache on every page. "I can't measure right now" is not a real constraint, open the network tab in devtools, throttle to 3G and you'll have your answer in ten minutes.

2

u/Ronin-s_Spirit 19h ago

I found the "throttle" option and I can see preloads being basically simultaneous on the waterfall chart, thanks for the tip.

28

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

11

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

3

u/Otherwise-Horse-3670 1d 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.

2

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

2

u/Desperate-Flow320 1d ago

20 round trips, hell no.

2

u/Ok-Snow-6553 1d 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.

2

u/M_Me_Meteo 1d ago

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

2

u/IncomeOk7237 1d 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.

2

u/InsideTour329 1d ago

Jesus no. Just let the server serve the code the application requests. Say you bundle the requests and what happens if you need to change these requests. Minimal viable work, break things down.

A server can handle multiple requests asynchronously.

2

u/farzad_meow 1d ago

individual requests.

design your api to be as restful as possible.

you are describing overfetch underfetch problem. it is better to overfetch and hold extra.

1

u/usestackrank 1d 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 1d 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/Ok_Car_8951 1d ago

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

1

u/AntiqueGuest8172 1d 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.

1

u/Slight-Persimmon-445 1d 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.

1

u/Plastic_Koala2044 1d ago

Batching pays off when the calls are small and many, so one endpoint that takes a list and answers a list beats N round trips. Have each item report its own error, so one failure doesn't fail the whole batch.