How to Speed Up First-Page Load: DNS, TCP, TLS and Rendering Optimizations
This article explains how to reduce the time users wait for the first screen by optimizing DNS resolution, TCP and TLS handshakes, HTTP protocols, resource loading, and rendering processes, while also covering related network and security considerations.
1. DNS Domain Resolution
When a user enters a URL, the browser must map the domain name to an IP address using DNS. The resolution process involves multiple cache layers: browser cache, OS hosts file, local DNS cache, router cache, local DNS server, and upstream DNS servers, which may use recursive or iterative queries.
Optimization: reduce DNS lookup time by minimizing the number of distinct domains (typically 2‑4) and leveraging browser caching.
2. Establishing Connection
TCP Handshake
The client creates a socket and sends a SYN packet with a random sequence number. The server replies with SYN+ACK, and the client finalizes with ACK, establishing a TCP connection.
// client:
send({SYN: 1, seq: x, ...others})
// server:
send({SYN: 1, ACK: 1, ack: x + 1, seq: y, ...others})
// client:
ack === x + 1 ? send({ACK: 1, SYN: 0, ack: y + 1, ...others}) : 'error'
// server:
ack === y + 1 && ACK === 1 ? new Socket() : ''SSL/TLS Handshake
If HTTPS is used, an additional TLS handshake negotiates an encrypted channel. The client sends a ClientHello with random data and supported cipher suites; the server replies with ServerHello, its certificate, and may request a client certificate. The client validates the server certificate, generates a pre‑master secret encrypted with the server’s public key, and both sides derive a symmetric session key for subsequent encryption.
Key points: the session key is symmetric (fast), while the server’s public/private keys are asymmetric (slow). Reducing the certificate chain length and using session resumption can significantly cut handshake latency.
3. Getting Page Response
Redirects
Server‑issued redirects cause the browser to repeat DNS resolution and connection establishment for the new URL, so unnecessary redirects should be avoided.
Page Resource Response
After receiving the HTML, the browser parses it and begins requesting linked resources (JS, CSS, images), which also require DNS lookup and TCP/TLS handshakes.
4. Parsing and Rendering the Page
Resource Loading
Resources are fetched over HTTP/1.1 or HTTP/2. HTTP/1.1 uses separate TCP connections (or long‑lived connections) and can suffer from request serialization, while HTTP/2 multiplexes many streams over a single TCP connection.
Optimizations:
Enable HTTP/1.1 keep‑alive to reuse connections.
Use TLS session resumption.
Reduce the total number of requests (code merging, sprites, inline small images).
Distribute assets across multiple domains to bypass per‑domain connection limits (but not excessively).
Compress responses with Gzip, minimize cookies, and use HTTP/2 header compression.
Leverage caching headers (Last‑Modified/If‑Modified‑Since, ETag, Cache‑Control, Expires) to avoid unnecessary transfers.
HTML/CSS/JS Rendering
The browser builds a DOM tree from HTML, a CSSOM tree from CSS, merges them into a render tree, performs layout, and paints pixels. Certain resources block rendering: CSS blocks rendering (unless media‑only), and scripts block DOM construction until executed.
Optimizations:
Simplify the DOM and reduce element count.
Place scripts at the end of body or use defer to avoid blocking.
Minimize DOM manipulations, use DocumentFragment, and batch style changes.
Put critical CSS in head, reduce selector complexity, and prefer performant layout models (e.g., flex).
Enable compositing layers (3D transforms, opacity) to limit re‑flows.
Use absolute positioning to remove elements from normal flow when appropriate.
Closing TCP
After all resources are downloaded, the client sends a FIN packet, the server acknowledges, and the connection is gracefully terminated. This step offers little room for optimization.
5. Additional Optimizations
Adopt PWA techniques so users see a functional UI even without network data.
Cache AJAX responses in local storage.
Show loading progress, skeleton screens, or placeholder images to improve perceived performance.
Keep server software up‑to‑date to benefit from newer protocol features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
