Web Development

Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork

Lecture Objectives

  • learn some simple techniques for improving performance in the case of external resources such as images

External Resources

Typically, images in web pages are external resources
— stored separately, in their own files.

Typically, the following are also external resources:
favicons
videos
audio files
CSS stylesheets
fonts
JavaScript programs

External Resources

Suppose a web page references several external resources (stylesheets, images, etc.).

When a client requests the web page, the server does not send all the content in one go.

The client receives the web page and then sends a separate request for each external resource.

External Resources

Suppose this web page is hosted on www.example.org


<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animal News!</title>
    <link rel="stylesheet" href="styles.css" />
</head>
<body>
    <figure>
        <img src="images/logo.gif" alt="" />
    </figure>
    <main>       
        <h1>Welcome to Animal News!</h1>   
        <p>
            <img src="images/dog.jpg" alt="A dog" />
            Dog bites man. Man bites dog in return.
        </p>
        <p>
            <img src="images/cat.jpg" alt="A cat">
            Cat plays piano. Badly.
        </p>
        <p>
            <img src="http://www.other.org/images/wombat.jpg" alt="A wombat">
            Wombat doesn't play piano. Learns saxophone.
        </p>    
    </main>
</body>
</html>
    

Multiple Requests

A minimum of 5 additional requests:

The browser makes separate HTTP GET requests for the stylesheet and the four images, additional to fetching the web page itself.

External Resources

The example also illustrates the following ideas:

same-origin requests:
where a web page's external resources are requested from the same domain as the web page.
cross-origin requests:
where a web page's external resources are requested from a domain that differs from the web page's domain.

In the next module, we will see examples where there is a same-origin security policy, where cross-origin requests are not allowed.

Render-Blocking Resources

render-blocking resources
browsers will not display ('render') any of the web page until these external resources have arrived
non-blocking resources
browsers will start to display whatever they have, even though these resources have not yet arrived

Typically, CSS and JavaScript are render-blocking.
Images (including favicons), fonts, video and audio files are non-blocking.

Avoiding Multiple Requests

How can we avoid multiple requests in the case of images?

Instead of putting your image in a separate file, you can produce its corresponding data URI and then include this directly in your HTML or CSS.

Data URIs

There are tools for converting images to data URIs.

Example of a data URI:


data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4QAWRXhpZg…
    

Avoiding Multiple Requests

You can use data URIs for:

  • foreground bitmapped images (in your HTML);
  • background bitmapped images (in your CSS);
  • favicons (in your HTML);
  • fonts (in your CSS); and
  • video and audio (in your HTML).

Avoiding Multiple Requests

  • The code for SVG images can also be placed directly into your HTML.
  • To avoid multiple requests, we can also use inline styles or embedded stylesheets instead of external ones.
  • Similarly, JavaScript is similar to CSS: there are three ways of associating JavaScript with your web pages.

Comparison

external resource data URI/
embedded resource
several requests/responses one request/
one response (but bigger)
separate caching may reduce
subsequent requests
no separate caching

Conditional Loading and Lazy Loading


<img src="ucc.jpg" alt="..."
     loading="lazy" />        
    

loading="eager" (default):
browser requests the image, regardless of whether it will be visible in the viewport.

loading="lazy":
browser defers requesting the image until the user scrolls to a point where the image will soon need to be visible in the viewport.

Performance

Monitor continuously!

G'luck!