Advance Web Development
In this, we will understand some advance web development topics.
Async and Defer questions
- A regular script, that does not have async or defer, it blocks the HTML parsing to fetch and execute the script, and then continues with the HTML parasing. It is render blocking.
- When I do
<script async>
it means, download the script parallel to the HTML parsing BUT execute the script only when the script is donwload. There is no gurantee of execution. - When we do
<script defer>
it means that download the script parallel to HTML parsing and execute the script ONCE the HTML parsing is done. Guarantees the execution of the scripts.
Rendering Pipeline and Composting
- First the browser, parses the HTML and creates a DOM tree, which is a tree representation of the markup. Along with the HTML, if there are any CSS files in the markup then a CSSOM tree is created.
- Using the DOM tree and CSSOM tree, a render tree is created. Render tree consists of all the VISUAL elements that are there on the screen.
- Post that the layout process kicks in. It takes the render tree and figures out the geometry and layout of each element.
- After that it paints it on the screen.
- After paint it performs the compositing process. Compositing process takes help of the GPU and if the styles are absolutely or relatively positioned, it takes that into account and repaints. Stacking Context decides where to render stuff.
Resolving Domain Requests
This is how the browser resolved domain requests:
- Browser sends request to Recursive DNS Resolver.
- Recursive DNS Resolver queries the Root Name Server.
- Root Server responsds with Top Level Domain Name Server's IP address.
- TLDNS responsds with Authoratative Name Serve's IP address.
- ReDNS queries ANS.
- ANS responds with the website's IP address.
Call Stack and Event Loop
The JS processor has the following components:
- Call Stack: It keeps a stack of all the execution contexts
- Web API: All the Web API like timers, dom manipulations etc.
- Microtask queue
- Macrostask queue
- Event Loop: Checks for the queues to see what needs to be finished first
Resource Hints
These are hints that we add to our resources to modify the behaviour of fetching the resource:
- dns-prefetch: performs domain name resolution in the background.
- preconnect: proactively performs DNS resolution and TCP/TLS handshake.
- prefetch: Requets non critical resources in the background. They can be skipped by the browser.
- preload: Prioritizes fetching of critcal resources needed for the current navigation.
Whenever we send a request, its not just the HTTP request and response times, but also DNS resoltion and TCL, TLS handshake.
Object Reference and Destructure
Whenever we use the spread operator for an object, only the top level keys are copied. Everything else is referenced back to the original object.
PeformanceNavigationTimings
We use the PeformanceNavigationTimings API to get detailed information around the different performance metrics. Let us go through the basic events that we can capture:
- domainLookupStart -> DNS Resolution started
- domainLookupEnd -> DNS Resolution ended
- connectStart -> TCP Handshake started
- secureConnectionStart -> TLS check started
- connectEnd -> TCP and TLS has ended
- requestStart -> HTTP Request has started
- responseStart -> Response start
- responseEnd -> Response ended
- domLoading -> Time it takes for the document to load
- domInteractive -> Time where HTML parsing is done
- domContentLoadedEventStart -> When the deferred and async scripts run
- domComplete -> DOM Done
- loadEventStart -> Loaded
- loadEventEnd -> When all listeners have finished
Cache Directives
- no-cache: