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

4 Upvotes

28 comments sorted by

33

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

4

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

9

u/[deleted] 9d ago

[removed] — view removed comment

2

u/Ronin-s_Spirit 8d ago

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