<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Timothy Kyadondo on Medium]]></title>
        <description><![CDATA[Stories by Timothy Kyadondo on Medium]]></description>
        <link>https://medium.com/@timothy.kyadondo?source=rss-e3d55f3f75bf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9I44wvg-BXO9GOOUOyFwFw.jpeg</url>
            <title>Stories by Timothy Kyadondo on Medium</title>
            <link>https://medium.com/@timothy.kyadondo?source=rss-e3d55f3f75bf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 17:53:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@timothy.kyadondo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Leveraging Browser Caching]]></title>
            <link>https://medium.com/@timothy.kyadondo/leveraging-browser-caching-ba2193275262?source=rss-e3d55f3f75bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba2193275262</guid>
            <category><![CDATA[browsers]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[cache]]></category>
            <dc:creator><![CDATA[Timothy Kyadondo]]></dc:creator>
            <pubDate>Mon, 02 Mar 2020 18:04:37 GMT</pubDate>
            <atom:updated>2020-03-02T18:04:37.847Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3PYqz-MkD2RQxzMcAfdh3Q.jpeg" /></figure><p>Over the years, different browsers have provided us with tools that make our web pages load faster, and one of these tools is Cache. Leveraging browser caching can drastically improve your page speeds, which will increase user traffic. Someone might ask, what is Browser Caching?</p><h3>What is Browser Caching?</h3><p>Browser caching is a way of keeping website resources like images, Cascading Style Sheets (CSS) and Javascript in the browser’s memory without retrieving them from the server every time a page is loaded. With browser caching, a user visits a webpage for the first time and these resources are cached. On visiting the page again, the resources are retrieved from the browser’s memory and not the server, increasing the page load speeds. Resources will only be retrieved from the server when the cache expiration time has passed or when someone <a href="https://support.google.com/accounts/answer/9098093?co=GENIE.Platform=Desktop&amp;hl=en&amp;visit_id=636836879653475486-2800211621&amp;rd=1">clears cache in the browser</a>.</p><h3>Why is Browser Caching Important?</h3><p>Every time a user visits a webpage for the first time, multiple requests are sent to the server to fetch different static files. This process takes up a lot of bandwidth since these files have to be downloaded for the web page to become fully functional.</p><p>When a user navigates to a new page, the browser downloads the HTML page from the server and while the page is read, and constructed the browser sends out more requests to the server to fetch static files (like images, Javascript, CSS files) which are the building blocks of the webpage.</p><p>Enabling cache speeds up browsing through its ability to store all static files created when a user visits a webpage for the first time. When the user reloads or navigates back to the webpage, the static files are picked from the cache and not the remote server. This makes the webpage faster, saves a lot of bandwidth, and improves user experience.</p><p>Configuring the remote server with the right cache directives can help us utilize the browser cache feature. For example, in this screenshot, different cache headers are set in the response which, will make the browser store the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*ZNe5u0HFbtcwZYFXhC-C0g.png" /></figure><p>The screen I have shared above can be accessed by opening <strong>Developer Tools &gt; Network &gt; select any request</strong>. The arrows show different headers and directives set to cache the response. A<strong> cache-control </strong>header holds different directives; <strong>private</strong>, <strong>max-age=86400</strong>, <strong>stale-while-revalidate=604800</strong>. I talk more about these directives in <strong>how to configure </strong>section.</p><h3>How can we use Browser Caching?</h3><p>We can use Browser caching by updating or adding cache configurations in the .htaccess file on the Apache Web Server. It is strongly advised to make a copy of the .htaccess file before modifying it. The root directory of the Apache Web Server contains the .htaccess file. If you can’t see it, a special command ls -a can be run in the terminal to display it.</p><h4>How to configure .htaccess to cache files</h4><p>Apache requires one of these modules to be installed <a href="http://httpd.apache.org/docs/2.4/mod/mod_expires.html">mod_expires</a> or <a href="http://httpd.apache.org/docs/2.4/mod/mod_headers.html">mod_headers</a> to set configurations for caching.</p><p><strong>mod_expires</strong> module is an extension of apache that controls the setting of the Expires HTTP header and the max-age directive of the cache-control HTTP header in the server responses. The source file for this module is mod_expires.c. The code between the tags will only be executed if the module, mod_expires is installed on the server.</p><pre># BEGIN Expire Headers<br>1. &lt;ifModule mod_expires.c&gt;<br>2.   ExpiresActive On<br>3.   ExpiresDefault “access plus 1 year”<br>4.   ExpiresByType text/css “access plus 1 year”<br>5.   ExpiresByType text/javascript “access plus 1 year”<br>6. &lt;/ifModule&gt;<br># END Expire Headers</pre><p>In the above illustration,</p><p><strong>Line 1</strong>. We check if mod_expires module exists on the Apache server.</p><p><strong>Line 2</strong>. We have to set the ExpiresActive to On so that Apache can start caching. It enables or disables the generation of the Expires and Cache-Control headers for the document. It has to options which are On|Off</p><p><strong>Line 3</strong>. Sets a default to expire period if none is set for the files. TheExpiresDefault directive sets the default algorithm for calculating the expiration time for all documents. The syntax is like this.</p><pre>ExpiresDefault “&lt;base&gt; [plus] {&lt;num&gt; &lt;type&gt;}*”<br>ExpiresByType type/encoding “&lt;base&gt; [plus] {&lt;num&gt; &lt;type&gt;}*”</pre><p>where &lt;base&gt; is one of access, now(equivalent to access), modification<br>The plus keyword is optional. <br>&lt;num&gt; is a positive integer.<br>&lt;type&gt; is one of years, months, weeks, days, hours, minutes, seconds.</p><p>Line 4 and 5. Specify which files to cache and also the expiration period.</p><p><a href="http://httpd.apache.org/docs/2.2/mod/mod_headers.html"><strong>mod_headers</strong></a>. This module provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed.<br>Here is an example.</p><pre># BEGIN EXPIRE HEADERS<br>1. &lt;ifModule mod_headers.c&gt;<br>2.   //Caching for images<br>3.   &lt;FilesMatch “\.(jpg|png|gifs)$”&gt;<br>4.     Header set Cache-Control “public, max-age=86400”<br>5.   &lt;/FilesMatch&gt;<br>6. &lt;/ifModule&gt;</pre><p>In the above illustration,</p><p><strong>Line 1</strong>. We check if Apache has the mod_header module installed.</p><p><strong>Line 3</strong>. We use the <a href="https://www.who.int/manual/mod/core.html#filesmatch">FilesMatch</a> tag to target multiple image files using regular expressions.</p><p><strong>Line 4</strong>. We use the mod_header <a href="http://httpd.apache.org/docs/current/mod/mod_headers.html">Header</a> directive to set the configurations for caching.</p><h3>Gotchas of Browser Caching</h3><p>Below are some of the issues a user might experience when browser caching</p><ul><li>A page/script in a cache remains stale, not until the expiration period passes.</li><li>Someone with no experience setting up cache configurations in the .htaccess file can mess up the whole website setup.</li><li>Exposure of confidential and sensitive information like authentication data, session tokens, if cache directives are not set properly.</li></ul><p>These are some of the ways to fix the issues listed above.</p><ul><li>Adding a fingerprint to the file, for example, a file like main.css has to be changed to main_1547640726345.css.</li><li>Adding a version to the file, for example, main.css can be changed to main-v2.css</li><li>Clearing cache in the browser, this will delete all the old cache in memory.</li></ul><h4>Cache-Control</h4><p>This is a general-header field that is used to specify directives for caching mechanisms for both requests and responses. Here are some of the commonly used directives that can be set with this header:-</p><p><strong>public</strong> — This means that a response can be cached by any cache.</p><p><strong>private</strong> — This means that the response is intended for a single user and must not be stored by a shared cache.</p><p><strong>no-cache — </strong>This normally confuses people, it doesn’t mean, don’t cache at all. When this directive is set, it simply tells the browser to first validate the resource with the server before using the cache. So a request is made to the server and if the resource changed, a new copy is downloaded and cached. This directive is normally used on HTML files.</p><p><strong>no-store — </strong>This disables the cache from storing anything about the request or response.</p><p><strong>max-age &lt;seconds&gt; — </strong>This defines how long the file will be cached for. The time is set in seconds.</p><p><strong>state-while-revalidate &lt;seconds&gt;</strong> — The client will accept a stale response, while asynchronously checking in the background for a fresh one. The <strong>seconds’</strong> value indicates how long the client will accept a stale response.</p><h3>Conclusion</h3><p>Browser caching can make your website fast or destroy it if not configured properly.</p><h4>The pros include:-</h4><ul><li>Better bandwidth usage</li><li>Reduced load on website servers</li><li>Fast retrieval of static files on the page from the cache</li><li>Improves the performance and speed of the page</li></ul><h4>The cons include:-</h4><ul><li>Inconsistent results due to stale data</li><li>Exposure of confidential and sensitive information</li></ul><h4>Resources</h4><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching">MDN Docs: HTTP Caching</a></li><li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching">Google Docs: HTTP Caching</a></li></ul><p>If you have anything to add please let me know, thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba2193275262" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>