From DNS request to JavaScript execution: discover every step of page loading and how to optimize it.
When you load a web page by typing a URL into your browser, a specific process kicks in to retrieve and display that page. This process involves several steps that progressively render the page, execute JavaScript, and load data via APIs if necessary.
In this article, we detail each step, highlighting the role of the server and APIs in generating web pages.
When a user types a URL in their browser (e.g., https://example.com), the following happens:
Server vs. CDN: What’s the Difference?
Example:
A site hosted in the United States might serve a European user via a local CDN copy instead of waiting for a response from the American server.
Before sending the HTML to the browser, the server might need to generate dynamic content.
There are two types of pages:
How Does a Dynamic Page Work?
Example:
PHP Code Block Placeholder
Advantage:
Content is already embedded in the HTML, so no additional client-side API call is needed.
Disadvantage:
It might be slower because each request must be processed by the server before being sent.
Once the server response is received, the browser parses the HTML file and starts building the page structure.
What Does the HTML File Contain?
<head>, <body>, etc.).<link rel="stylesheet" href="styles.css">).<script src="app.js"></script>).What Happens at This Stage?
Next, the browser downloads the CSS files referenced in the HTML to apply visual styling.
CSS Functions:
Why Can CSS Slow Down Loading?
A large CSS file might block rendering until it is fully loaded.
Possible Optimizations:
The browser also begins to retrieve images and font files used on the page.
Why Do Some Fonts Take Time to Appear?
Possible Optimizations:
font-display: swap; to show a temporary font until the custom font loads.<link rel="preload" as="font" href="font.woff2">.The browser then retrieves JavaScript files, which add interactivity to the page.
JavaScript Functions:
Why Can JavaScript Slow Down a Page?
Blocking JavaScript can delay the rendering of the rest of the site.
Possible Optimizations:
async) or defer their execution (defer).After the JavaScript executes, the page becomes fully interactive.
What Does "Interactive" Mean?
onclick or addEventListener).If the page requires additional data, client-side API calls are made.
Examples of API Calls:
How Are These API Calls Made?
JavaScript Code Block Placeholder
Impact on User Experience:
These requests often display a loader while waiting for the server response.
Understanding these steps is key to optimizing both loading times and overall user experience.
Clear insights to make the right technical decisions, even if you're not a dev.