<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Igwe Chinonso</title>
    <description>The latest articles on DEV Community by Igwe Chinonso (@chiboycalix).</description>
    <link>https://dev.to/chiboycalix</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F199188%2F68c4b90a-5065-48a4-ad52-01f53ceb4745.JPG</url>
      <title>DEV Community: Igwe Chinonso</title>
      <link>https://dev.to/chiboycalix</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chiboycalix"/>
    <language>en</language>
    <item>
      <title>Client Side Caching</title>
      <dc:creator>Igwe Chinonso</dc:creator>
      <pubDate>Thu, 04 May 2023 14:11:00 +0000</pubDate>
      <link>https://dev.to/chiboycalix/client-side-caching-48l8</link>
      <guid>https://dev.to/chiboycalix/client-side-caching-48l8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Client-Side Caching
&lt;/h2&gt;

&lt;p&gt;Client-side caching is a technique used to store frequently accessed web content on the client's machine, reducing the need for server round-trips and improving website performance. This technique is needed because network round-trips can cause latency and impact website performance. &lt;/p&gt;

&lt;p&gt;Let's say you have a favourite book that you like to read every day. Instead of going to the library every time you want to read the book, you keep a copy of it on your bedside table. This way, you can pick up the book and start reading it right away, without having to go to the library every time.&lt;/p&gt;

&lt;p&gt;In this scenario, the library is like the server where the book is stored, and your bedside table is like the cache where you've stored a copy of the book. Just like caching a website or app on your device, caching the book on your bedside table makes it much easier and faster to access, since you don't have to go all the way to the library every time you want to read it. &lt;/p&gt;

&lt;p&gt;The primary advantage of client-side caching is faster website performance, which can lead to improved user experience, increased engagement, and higher conversion rates. Caching can also help reduce server load, reduce network traffic, and improve scalability.&lt;/p&gt;

&lt;p&gt;Here is a code snippet to better explain how caching works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cache = {
    '1-4': 1
}
function cachedComputation(a, b) {
    const key = `${a}-${b}`;
    if (cache[key]) {
      console.log("from cache");
      cache[key] = a + b;
      return cache[key];
    }
    for(let i = 0; i &amp;lt; 10000000000; i++) {}
    console.log("from database");
    cache[key] = a + b;
    return cache[key];
}
cachedComputation(1, 7); // console prints from database
cachedComputation(1, 4); // console prints from cache
cachedComputation(1, 7); // console prints from cache since we have already ran the function with 1, 7 as arguments and stored in the cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code demonstrates how to use caching to speed up function computations. The &lt;code&gt;cache&lt;/code&gt; object stores the results of previous function calls, so when the function is called with the same arguments, it can return the result from the cache instead of recomputing it. If the result is not in the cache, the function performs the computation and stores the result in the cache for future use.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Client-Side Caching Works
&lt;/h2&gt;

&lt;p&gt;Caching works by storing a copy of a web resource on the client's machine and reusing it when the resource is requested again. HTTP caching uses caching headers to control caching behaviour and determine whether a client can use a cached copy or must fetch a fresh copy from the server.&lt;/p&gt;

&lt;p&gt;Browsers can also cache web resources automatically using built-in caching algorithms. Caching headers, such as Cache-Control, Expires, and ETag, provide granular control over caching behaviour and can be set by the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Techniques for Implementing Client-Side Caching
&lt;/h2&gt;

&lt;p&gt;Cache-Control is a caching header used to set cache expiration and validation rules. It provides granular control over caching behaviour and can be used to set a maximum age for cached resources, control cache revalidation, and prevent caching altogether.&lt;/p&gt;

&lt;p&gt;Cache-busting is a technique used to force clients to fetch fresh copies of resources by changing the resource URL or version. This technique can be used to invalidate caches when the content has changed, ensuring that clients see the latest version.&lt;/p&gt;

&lt;p&gt;Here's an example of how to implement cache-busting in a web application using a unique file name or query string parameter.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unique file name approach:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;Cache-Busting Example&amp;lt;/title&amp;gt;
  &amp;lt;link rel="stylesheet" href="/styles.css?v=1.0"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;h1&amp;gt;Cache-Busting Example&amp;lt;/h1&amp;gt;
  &amp;lt;script src="/script.js?version=1.0"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we've added a query string parameter to the file name for both the stylesheet and script tags. By changing the value of the &lt;code&gt;version&lt;/code&gt; parameter, we force the client to download a new version of the file, even if it is already cached.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Query string parameter approach:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;Cache-Busting Example&amp;lt;/title&amp;gt;
  &amp;lt;link rel="stylesheet" href="/styles.css?v=&amp;lt;%= Date.now() %&amp;gt;"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;h1&amp;gt;Cache-Busting Example&amp;lt;/h1&amp;gt;
  &amp;lt;script src="/script.js?version=&amp;lt;%= Date.now() %&amp;gt;"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're using a server-side template engine like EJS to dynamically generate a new value for the &lt;code&gt;version&lt;/code&gt; parameter based on the current timestamp. This has the same effect as the previous example, but with the added benefit of being able to generate unique cache-busting values on the server-side.&lt;/p&gt;

&lt;p&gt;By using either of these techniques, you can invalidate caches and ensure that clients always receive the latest version of your files. It's important to note that cache-busting can impact performance, as clients will need to download new files every time the cache is invalidated. Therefore, it's important to use cache-busting sparingly and only when necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Client-Side Caching
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Setting Maximum Age and Expiration vs Validation&lt;br&gt;
When setting cache headers, it's important to balance the benefits of caching with the need for fresh content. Setting a maximum age for cached resources can help ensure that clients get fresh content regularly. Expiration-based caching is useful for resources that don't change frequently, while validation-based caching is useful for resources that change frequently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoiding Common Pitfalls&lt;br&gt;
Common pitfalls of client-side caching include cache poisoning, cache inconsistency, and cache invalidation. To avoid these issues, it's important to set cache headers correctly, use cache-busting techniques, and ensure that cached resources are consistent and up-to-date.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Examples of Client-Side Caching Implementation
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;CSS and JavaScript Files&lt;br&gt;
CSS and JavaScript files are critical to website performance, and caching these resources can help reduce page load times and improve user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Images and Other Resources&lt;br&gt;
Images and other resources, such as videos, can benefit greatly from client-side caching. By caching these resources, clients can load them faster, reducing page load times and improving performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Testing and Measuring Client-Side Caching Performance
&lt;/h2&gt;

&lt;p&gt;Tools such as Chrome DevTools, Firebug, and Fiddler can be used to test caching performance by analyzing the caching headers and checking whether resources are being cached correctly. These tools can also be used to simulate different network conditions and test caching behaviour under different scenarios.&lt;/p&gt;

&lt;p&gt;Network latency and time to first byte are critical factors in website performance and can be measured using tools such as Pingdom, GTmetrix, and WebPageTest.&lt;/p&gt;

&lt;p&gt;In conclusion, as the web evolves, new caching techniques and technologies are likely to emerge. The use of content delivery networks (CDNs), progressive web apps (PWAs), and service workers are likely to play a larger role in client-side caching and website performance optimisation in the future. It's important for developers to stay up-to-date on the latest caching best practices and tools to ensure that their websites are fast, reliable, and engaging.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Closures</title>
      <dc:creator>Igwe Chinonso</dc:creator>
      <pubDate>Wed, 03 May 2023 07:38:03 +0000</pubDate>
      <link>https://dev.to/chiboycalix/closures-fad</link>
      <guid>https://dev.to/chiboycalix/closures-fad</guid>
      <description>&lt;p&gt;What is a closure?&lt;br&gt;
An act or process of closing something, especially an institution, thoroughfare, or frontier, or of being closed.&lt;/p&gt;

&lt;p&gt;I know you must be wondering are my here for closures in javascript or closures as regards our general english language communications? You are right to ask this question because the definition above is a google searched definition. That is not what we would be discussing today. We would be talking about closures in Programming using Javascript as language of instruction for code samples.&lt;/p&gt;

&lt;p&gt;A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment) &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;(MDN)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'll try to explain this with an example. Take a look at this code below. It is just a plain javascript function that logs the value of count.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kkl1h1i9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cv084pdbb1uowu7yr1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kkl1h1i9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cv084pdbb1uowu7yr1i.png" alt="function without closure" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What do you think would be the output of invoking the function?.&lt;br&gt;
The first invocation would print out the value 1 for count. The second invocation would also print out the value 1. This is because of the way javascript works in relation to execution context creation. when we run our the javascript code above, the first thing javascript does is to save the function declaration in memory and then skip to line 8 and invoke the function(First invocation). Invoking or calling a function means we should step into the code defined in the function line by line and exit when we meet a return statement.(When no return statement is found, function automatically returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On line 4, we are defining a variable &lt;code&gt;count&lt;/code&gt; and setting it's initial value to &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
On line 5, we are incrementing the value of the variable &lt;code&gt;count&lt;/code&gt; by &lt;code&gt;1&lt;/code&gt;. So the new value of count becomes &lt;code&gt;1&lt;/code&gt; on line 5.&lt;br&gt;
On line 6, we are logging the value of &lt;code&gt;count&lt;/code&gt; to the console.&lt;br&gt;
So ones we hit line 6 and there are no other codes to be ran by the function, the function returns and value 1 is printed on the console. This brings an end to the first function call.&lt;/p&gt;

&lt;p&gt;On line 9, we have another invocation of the function and we follow through the same process again.&lt;br&gt;
Count get's initialised to &lt;code&gt;0&lt;/code&gt; on line 4, we increment it by one on line 5 and print the value to console on line 6. Value one is still printed to the console.&lt;/p&gt;

&lt;p&gt;What if we want to find a way to prevent the count variable to always start from 0 but rather, it should remember it's previous value and keep incrementing by 1?. That is where the need for closure arises.&lt;/p&gt;

&lt;p&gt;We can implement this with ease using closure as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R3QgM0BZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot0cjtltod6piozkod4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R3QgM0BZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot0cjtltod6piozkod4s.png" alt="Closure functions" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you may have noticed, we have a similar function as before but this time with an inner function called closure which remembers the value of count from previous invocations.&lt;/p&gt;

&lt;p&gt;We first assign the value of the function invocation of the outer function &lt;code&gt;outerWithClosure&lt;/code&gt; to a variable called &lt;code&gt;myFunction&lt;/code&gt;. Then we run the first myFunction and this increment the value of count from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;. Since myFunction is a closure, it remembers the previous value of count which is now &lt;code&gt;1&lt;/code&gt; so on the second invocation, count begins at value &lt;code&gt;1&lt;/code&gt; and get's incremented to &lt;code&gt;2&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;As a bonus, we can create a closure function which returns is a factory function.This would help us create increment and decrement closure functions as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GkWrCVaq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frw63qzoi428wn5njp6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GkWrCVaq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frw63qzoi428wn5njp6t.png" alt="closures with factory function" width="800" height="645"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closure</category>
      <category>programming</category>
    </item>
    <item>
      <title>Fix Page not found error when visiting a route directly in react</title>
      <dc:creator>Igwe Chinonso</dc:creator>
      <pubDate>Tue, 09 Aug 2022 09:22:10 +0000</pubDate>
      <link>https://dev.to/chiboycalix/fix-page-not-found-error-when-visiting-a-route-directly-in-react-57k6</link>
      <guid>https://dev.to/chiboycalix/fix-page-not-found-error-when-visiting-a-route-directly-in-react-57k6</guid>
      <description>&lt;p&gt;Have you ever gotten the dreaded "sorry page not found" error when visiting a particular route by entering its url directly in your browser tab after pushing your react app to production(netlify for instance)?.&lt;/p&gt;

&lt;p&gt;There is a quick fix you can use to resolve the issue.&lt;/p&gt;

&lt;p&gt;create a file called &lt;code&gt;_redirects&lt;/code&gt; in your public folder and add this line of code&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/*  /index.html  200&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This should fix your error.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>router</category>
      <category>pagenotfound</category>
      <category>production</category>
    </item>
    <item>
      <title>Pitch me on Python or Go</title>
      <dc:creator>Igwe Chinonso</dc:creator>
      <pubDate>Tue, 17 May 2022 07:34:43 +0000</pubDate>
      <link>https://dev.to/chiboycalix/pitch-me-on-python-or-go-23d7</link>
      <guid>https://dev.to/chiboycalix/pitch-me-on-python-or-go-23d7</guid>
      <description></description>
      <category>pitch</category>
      <category>python</category>
      <category>go</category>
    </item>
  </channel>
</rss>
