<?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: amythical</title>
    <description>The latest articles on DEV Community by amythical (@amythical).</description>
    <link>https://dev.to/amythical</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%2F1337437%2F967ca0ab-4856-46dc-8c3b-5f1ac645e3c1.jpg</url>
      <title>DEV Community: amythical</title>
      <link>https://dev.to/amythical</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amythical"/>
    <language>en</language>
    <item>
      <title>A rec, CNAME, TXT recs</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Tue, 02 Jul 2024 10:09:35 +0000</pubDate>
      <link>https://dev.to/amythical/a-rec-cname-txt-recs-4loo</link>
      <guid>https://dev.to/amythical/a-rec-cname-txt-recs-4loo</guid>
      <description>&lt;p&gt;I was always confused with the different kinds of records using for the domain pointing and configuration.&lt;/p&gt;

&lt;p&gt;Making a note so I remember them and also if it helps others :-&lt;/p&gt;

&lt;p&gt;A record - Point a domain/subdomain to an IP Address&lt;br&gt;
CNAME - create alias for domain or subdomain&lt;br&gt;
TXT Records - text used for configuration or validation (Usually SPF/DKIM records)&lt;br&gt;
MX - Used for mail server configuration&lt;/p&gt;

</description>
      <category>devops</category>
      <category>networking</category>
    </item>
    <item>
      <title>Troubleshooting slow page loads</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Wed, 22 May 2024 16:33:27 +0000</pubDate>
      <link>https://dev.to/amythical/dealing-with-slow-page-loads-3joa</link>
      <guid>https://dev.to/amythical/dealing-with-slow-page-loads-3joa</guid>
      <description>&lt;p&gt;Recently I encountered a slow loading page (ReactJS/webpack/node/express) ...&lt;br&gt;
I did manage to pull the load time to a decent time.&lt;/p&gt;

&lt;p&gt;I am sharing my findings on my page and the fixes I applied.&lt;/p&gt;

&lt;h2&gt;
  
  
  Know what is slowing it down
&lt;/h2&gt;

&lt;p&gt;Dont make assumptions, measure it.&lt;br&gt;
I loaded the page and used chrome developer tools to check what was slowing it down&lt;/p&gt;

&lt;h2&gt;
  
  
  My findings
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Too many requests to my express server - At a time Chrome will open about 6 connections to a domain/ip and the rest of the requests to the same domain will be queued&lt;/li&gt;
&lt;li&gt;Slow APIs (Adding to #1)&lt;/li&gt;
&lt;li&gt;Some CSS and Images served from the server (Adding to #1)&lt;/li&gt;
&lt;li&gt;Geography - I used browserstack and accessed it from different regions and I found some apis to be slower from geographies far away from my region&lt;/li&gt;
&lt;li&gt;Big chunk sizes (ReactJS + Webpack)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Steps I took to fix these
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Low hanging fruit move all CSS and images to a CDN like cloudfront or cloudflare&lt;/li&gt;
&lt;li&gt;Slow APIs - Some APIs were serving the same data to all users. This data was periodically updated. I created a static json for these and hosted these on the CDN. &lt;/li&gt;
&lt;li&gt;Slow APIs - Tweak my DB query which was serving some data unused by the clients.&lt;/li&gt;
&lt;li&gt;Big chunk sizes - Removed unused imports, components not needed on the first render imported them dynamically, applied a terser in the webpack, enabled compression in express server
(Separate post on this coming up soon)&lt;/li&gt;
&lt;li&gt;Slow APIs - It might be worth looking at moving some APIs to AWS Lamda so that they can be served well in all regions&lt;/li&gt;
&lt;li&gt;The smaller chunk sizes helped overall with the other geographies and also data on the CDN helped load the page faster on the other geographies&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Measure before you fix &lt;/li&gt;
&lt;li&gt;Cache where necessary&lt;/li&gt;
&lt;li&gt;Dont serve un-needed data in your APIs&lt;/li&gt;
&lt;li&gt;Apply compression to outbound data in express&lt;/li&gt;
&lt;li&gt;Look at webpack tuning (uglify, terser)&lt;/li&gt;
&lt;li&gt;Look at chunk sizes - refactor code &lt;/li&gt;
&lt;li&gt;Do not import anything that needs a user action or which doesnt need rendering on the initial load, dynamically load the components also remove unused imports.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/amythical/lazy-loading-components-in-reactjs-3aj"&gt;Link to my post&lt;/a&gt; on Dynamic loading components in ReactJS.&lt;br&gt;
Also a &lt;a href="https://dev.to/amythical/late-loading-images-using-js-2d1d"&gt;post on late loading images&lt;/a&gt; in Chrome.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Call C binary from javascript WITHOUT process.exec</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Tue, 21 May 2024 03:39:03 +0000</pubDate>
      <link>https://dev.to/amythical/call-c-binary-from-javascript-without-processexec-4251</link>
      <guid>https://dev.to/amythical/call-c-binary-from-javascript-without-processexec-4251</guid>
      <description>&lt;p&gt;&lt;strong&gt;What's this about?&lt;/strong&gt;&lt;br&gt;
I am going to directly start with an example calling a C binary from javascript and it is NOT a process.exec call.&lt;/p&gt;

&lt;p&gt;Emscripten is what we will use, it helps us create supporting glue code to call the C binary from javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about emscripten installation?&lt;/strong&gt;&lt;br&gt;
I will make a following post on the emscripten installation.&lt;br&gt;
But here is all the action, assuming we have emscripten installed and configured on our box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write C program&lt;/li&gt;
&lt;li&gt;Compile using emscripten&lt;/li&gt;
&lt;li&gt;Use generated JS file in React code&lt;/li&gt;
&lt;li&gt;Run the program and smile &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  helloworld.c
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The famous hello world in C
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;

int main(){
  printf("Hello World from C\n");
  fflush(stdin);
  return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Compiling using emcc
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;emcc helloworld.c -o helloworld.js -s WASM=0 -s ENVIRONMENT=web  -s MODULARIZE=1 -s "EXPORT_NAME='mymodule'"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Files generated by emcc
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;helloworld.js&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Using helloworld.js in React
&lt;/h3&gt;

&lt;p&gt;Copy helloworld.js to a folder in your React src/&lt;br&gt;
eg utils&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp helloworld.js utils/ #Linux command line
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import mymodule from '../utils/helloworld.js';

async callCProgram(){
    mymodule()
    .then(module =&amp;gt; {
      console.log('module loaded!');
  });
}
// Call this function on componentDidMount or on a button click
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Run the program
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Open the browser &amp;amp; call the url to your React code&lt;/li&gt;
&lt;li&gt;Open Developer Tools, console&lt;/li&gt;
&lt;li&gt;You will see "Hello World from C" printed in your console&lt;/li&gt;
&lt;li&gt;SMILE :), youve called a C program from your React Code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow up post on installation coming up soon and may make a series of short posts for emscripten.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Unresponsive UI and a busy main thread?</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Sun, 19 May 2024 16:25:09 +0000</pubDate>
      <link>https://dev.to/amythical/yield-control-when-too-busy-1mkb</link>
      <guid>https://dev.to/amythical/yield-control-when-too-busy-1mkb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Situation&lt;/strong&gt;&lt;br&gt;
Say you are in a situation where you have a Popup with a close button and a loader  asking you users to wait while you process a lot of data before you display it to them. &lt;br&gt;
You have to carry out computation on a list of items before displaying them.&lt;br&gt;
&lt;strong&gt;UX Problem&lt;/strong&gt;&lt;br&gt;
If we carry this processing out in the main thread, if the users click the close button - it may not respond as the main thread is busy and not listening for the close button event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WebWorkers may not work if dom elements&lt;/strong&gt;&lt;br&gt;
My first goto would be Web Workers but sometimes we cannot use a worker because we may need to use DOM Elements with the values in the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
So we need to yield control from time to time to process other events in the main thread.&lt;br&gt;
But there is no native sleep method so I am using the following custom function, 'fakeSleep'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  fakeSleep(pTimeSec) {

    return new Promise((resolve, reject) =&amp;gt; {
      setTimeout(() =&amp;gt; {
        resolve();
      }, pTimeMillisec);// 16 ms is a good value (60 fps/1 sec ~ 16 ms per frame)
    });
  }

  processingFunction1();
   fakeSleep(0.1);// or 0.05 anything you wish 
  processingFunction2();
   fakeSleep(0.1);// or 0.05 anything you wish 
  processingFunction3();
   fakeSleep(0.1);// or 0.05 anything you wish 
  processingFunction4();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By yielding control even for a small amount of time in between our heavy processing, we can make the UI responsive.&lt;/p&gt;

&lt;p&gt;This is a solution for extreme situations but I have had the need to use this while processing a lot of data on the client.&lt;/p&gt;

&lt;p&gt;Cheers.&lt;br&gt;
Happy to hear how you handle such situations.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>My mistake with resolve and return resolve</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Sun, 19 May 2024 03:22:45 +0000</pubDate>
      <link>https://dev.to/amythical/my-mistake-with-resolve-and-return-resolve-4a7b</link>
      <guid>https://dev.to/amythical/my-mistake-with-resolve-and-return-resolve-4a7b</guid>
      <description>&lt;p&gt;TLDR : Just calling resolve does not stop further code execution, return resolve is the right way to stop further code execution.&lt;/p&gt;

&lt;p&gt;Lets see this example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fun1(){
  returns new Promise(){
      console.log("Before calling fun2");
      fun2().then(response){
          if(response == 1234){
             resolve(response);
          }
          console.log("After resolve");
      }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you run this, the console prints&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Before calling fun2
After resolve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But ... I had called resolve so the code at "After resolve" should not have been reached.&lt;br&gt;
WRONG!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Correct use&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun2().then(response){
          if(response == 1234){
             return resolve(response);
          }
          console.log("After resolve");
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it works as expected and returns where intended stopping further code execution.&lt;/p&gt;

&lt;p&gt;This was a small oversight but it leads to unexpected behaviour in the logic flow if ignored.&lt;/p&gt;

&lt;p&gt;Just sharing my experiences, small things that will save time, sweat and hair on the head.&lt;/p&gt;

&lt;p&gt;Cheers.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>AWS Cognito terms that confused me</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Sun, 19 May 2024 03:11:55 +0000</pubDate>
      <link>https://dev.to/amythical/aws-cognito-terms-that-confused-me-3d7h</link>
      <guid>https://dev.to/amythical/aws-cognito-terms-that-confused-me-3d7h</guid>
      <description>&lt;p&gt;I made a list of terms that confused me while referring to documentation on AWS Cognito.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;APIG : Api Gateway&lt;/li&gt;
&lt;li&gt;Authentication : Verified the identity of the user eg are you a valid used with a proper email and password registered with the system?&lt;/li&gt;
&lt;li&gt;Authorization : determines their access rights eg Does this use have the privileges/rights to delete this file?&lt;/li&gt;
&lt;li&gt;CF : CloudFront&lt;/li&gt;
&lt;li&gt;Federated users : An authorised user with a single token to obtain access to other systems in a network&lt;/li&gt;
&lt;li&gt;Federated Identities - Identities in your AWS system having temporary access to AWS Resources like S3&lt;/li&gt;
&lt;li&gt;IAM : Identity and Access Management&lt;/li&gt;
&lt;li&gt;Identity pools : Provide temporary creds to guests or authenticated users to access AWS resources like S3, under the Cognito Service&lt;/li&gt;
&lt;li&gt;Policy Variables : Default or Custom mappings in the Identity pool in AWS Cognito mapping the User Pool attributes to variables that are available for use in an IAM Policy. Without these the user pool attributes are not available for any Conditional Statements in the IAM Policy&lt;/li&gt;
&lt;li&gt;sub : short for subject is a UUID for each Cognito User in the Cognito user Pool&lt;/li&gt;
&lt;li&gt;User pool : Is a user directory provided by AWS to manage user profiles under the Cognito Service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Originally posted on my &lt;a href="https://github.com/amythical/aws/blob/main/cognito/confusing-terms.md"&gt;github&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Sources referred - &lt;a href="https://beabetterdev.com/2022/06/26/amazon-cognito-a-complete-beginner-guide/"&gt;beabetterdev.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>beginners</category>
      <category>cloudcomputing</category>
      <category>learning</category>
    </item>
    <item>
      <title>Nodejs axios certificate error</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Sat, 18 May 2024 17:54:13 +0000</pubDate>
      <link>https://dev.to/amythical/axios-certificate-error-4l3i</link>
      <guid>https://dev.to/amythical/axios-certificate-error-4l3i</guid>
      <description>&lt;p&gt;TLDR - Solution for axios certificate error if you are using node 8.10.0 or an older node version.&lt;/p&gt;

&lt;p&gt;I ran into a certificate error/CERT_HAS_EXPIRED error when running a simple axios get request in nodejs (calling pexels api).&lt;br&gt;
Surprisingly I checked with a CURL call and the request went through fine.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Tip - For debugging api/network calls using &lt;a href="https://www.geeksforgeeks.org/curl-command-in-linux-with-examples/"&gt;curl&lt;/a&gt; from the linux instance is very helpful&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;br&gt;
I happened to be using an archaic node version 8.10.0 on that particular software stack. &lt;br&gt;
Apparently some certificate in letsencrypt expired and that has led to this error when calling axios using node 8.10.0 versions.&lt;/p&gt;

&lt;p&gt;Here is the link to the stackoverflow thread that helped me solve it - &lt;a href="https://stackoverflow.com/questions/69414479/giving-axios-letsencrypts-new-root-certificate-on-old-version-of-node"&gt;https://stackoverflow.com/questions/69414479/giving-axios-letsencrypts-new-root-certificate-on-old-version-of-node&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagnosis&lt;/strong&gt;&lt;br&gt;
I had assumed there was an issue with the endpoint of the api, but the api owners confirmed there wasn't an error.&lt;br&gt;
When I cleaned the npm cache sometimes the error went away.&lt;br&gt;
The curl call from the same instance helped me isolate the issue to something on the instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;br&gt;
Create a https agent with the SSL certificate and use it with the axios request.&lt;br&gt;
I found the solution in the &lt;a href="https://stackoverflow.com/questions/69414479/giving-axios-letsencrypts-new-root-certificate-on-old-version-of-node"&gt;stack overflow link&lt;/a&gt;. &lt;br&gt;
The link has the code snippet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation for the error&lt;/strong&gt;&lt;br&gt;
With the letsencrypt certificate expiring, the openssl library bundled with the older nodejs installation was failing. &lt;/p&gt;

&lt;p&gt;Hope this helps someone.&lt;/p&gt;

&lt;p&gt;Cover photo credits - Photo by Vie Studio from Pexels: &lt;a href="https://www.pexels.com/photo/word-error-on-white-surface-4439425/"&gt;https://www.pexels.com/photo/word-error-on-white-surface-4439425/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ssl</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Webpack4 fix for .mjs files</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Sat, 18 May 2024 05:50:54 +0000</pubDate>
      <link>https://dev.to/amythical/webpack4-fix-for-mjs-files-3ggh</link>
      <guid>https://dev.to/amythical/webpack4-fix-for-mjs-files-3ggh</guid>
      <description>&lt;p&gt;Using CRA and Webpack4 and find .mjs files giving an issue? (Yes im using CRA and Webpack4 in 2024 FML!)&lt;br&gt;
Here are webpack config settings that will help -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  reScript = /\.(js|jsx|mjs)$/;
resolve: {

// Allow absolute paths in imports, e.g. import Button from 'components/Button'
// Keep in sync with .flowconfig and .eslintrc
modules: ['node_modules', 'src'],
extensions: ['.js', '.jsx','.mjs']
},

module: {
// Make missing exports an error instead of warning
strictExportPresence:  true,
rules: [
{ 
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto'
},

// Rules for JS / JSX
{
test:  reScript,
include: [SRC_DIR, resolvePath('tools')],
loader:  'babel-loader',
options: {} 
}
}],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cheers. Happy hacking!&lt;/p&gt;

&lt;p&gt;Cover image credits - &lt;a href="https://unsplash.com/photos/text-KZcWygxZ_J4"&gt;https://unsplash.com/photos/text-KZcWygxZ_J4&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webpack</category>
      <category>mjs</category>
    </item>
    <item>
      <title>Lazy loading components in ReactJS</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Sat, 18 May 2024 03:13:12 +0000</pubDate>
      <link>https://dev.to/amythical/lazy-loading-components-in-reactjs-3aj</link>
      <guid>https://dev.to/amythical/lazy-loading-components-in-reactjs-3aj</guid>
      <description>&lt;h2&gt;
  
  
  Why should you lazy load components?
&lt;/h2&gt;

&lt;p&gt;TLDR - For optimised chunk sizes and page load time.&lt;/p&gt;

&lt;p&gt;I'll share my experience. My chunk sizes in the React production build were huge, much larger than the usual sizes.&lt;br&gt;
React 16/Webpack 4 so unfortunately the good utilities like bundle analyzer would not work, so I glanced through the chunks' javascript code.&lt;br&gt;
All the static imports were included in them, bloating the size of the common chunks used by the modules.&lt;/p&gt;

&lt;p&gt;I refactored the code by late loading components which were not used/rendered on the screen on initial load. There were components which were loaded after the users actions like button clicks or scrolls, which were lazy loaded when needed.&lt;br&gt;
This reduced my chunk size and also that led to a quicker load time of my page.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to lazy load components -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;React SSR - For SSR, '@loadable' is recommended.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import  loadable  from  '@loadable/component';

// using it when needed 
const  myReactComponent = loadable(() =&amp;gt;  import(`../../components/MyReactComponent`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;React Lazy and Suspense
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Suspense from react;

class Test {
   constructor(){

   }

   someFunction(){
    // Import here and not statically
    const myReactComponent = React.lazy(() =&amp;gt; 
    import(`../MyReactComponent`));
    this.setState({myReactComponent:myReactComponent});
  }

  render(){
    return (
      &amp;lt;Suspense fallback={&amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;}&amp;gt;
    &amp;lt;this.state.myReactComponent&amp;gt;
    &amp;lt;/this.state.myReactComponent&amp;gt;
      &amp;lt;/Suspense&amp;gt;
    )
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fallback attribute takes in a markup which can have text/loaders or another component.&lt;/p&gt;

&lt;p&gt;Cheers.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Lazy loading images using JS &amp; ReactJS</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Fri, 17 May 2024 18:10:21 +0000</pubDate>
      <link>https://dev.to/amythical/late-loading-images-using-js-2d1d</link>
      <guid>https://dev.to/amythical/late-loading-images-using-js-2d1d</guid>
      <description>&lt;p&gt;We all want the page content to load as soon as possible. When we have a bunch of images, we wish they would load in the order we see them and only load what would be visible to a user.&lt;/p&gt;

&lt;p&gt;The default loading of images in a collection is random and load in any order leading to a not so smooth user experience.&lt;/p&gt;

&lt;p&gt;I wanted to lazy load the images.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Options&lt;/strong&gt; to begin with in 2024 - &lt;/p&gt;

&lt;p&gt;1.Native loading - The loading="lazy" attribute natively supported by browsers (&lt;a href="https://web.dev/articles/browser-level-image-lazy-loading"&gt;https://web.dev/articles/browser-level-image-lazy-loading&lt;/a&gt;)&lt;br&gt;
2.Intersection observer - custom implementation&lt;br&gt;
3.React components abstracting the functionality of lazy loading &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/react-lazy-load-image-component"&gt;https://www.npmjs.com/package/react-lazy-load-image-component&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/react-intersection-observer"&gt;https://www.npmjs.com/package/react-intersection-observer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Native lazy loading worked well for me as I was focussed on Chrome.&lt;br&gt;
&lt;a href="https://web.dev/articles/browser-level-image-lazy-loading"&gt;https://web.dev/articles/browser-level-image-lazy-loading&lt;/a&gt;&lt;br&gt;
Important to have the image width and height specified for this to work well.&lt;/p&gt;

&lt;p&gt;For native lazy loading to work in firefox I read a suggestion on stack overflow to put the loading="lazy" attribute before the src attribute in an image tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img loading="lazy" src="path/to/myimage.jpg" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CONS: No Control on the trigger to start loading images&lt;/strong&gt;&lt;br&gt;
With native lazy loading there is however no control on the trigger when the images will start loading. Chrome has some defaults for when to trigger the lazy loading based ond on the screen size and the internet speed.&lt;br&gt;
For finer control on the trigger eg start loading images before 100px from the scrolling point, #2 Intersection Observer or #3 components based on the Intersection Observer will give finer control.&lt;/p&gt;

&lt;p&gt;Some enhancements to the image conatiner/holding div while the image loads - &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LQIP - Low Quality Image Placeholder&lt;/strong&gt;&lt;br&gt;
We can load a low quality image in place of the heavier/actual image until its loaded.&lt;/p&gt;

&lt;p&gt;If you are using React, below is a code snippet to give you an idea,  (I haven't yet taken to functional components preferring the class components with lifecycle methods) :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
componentDidMount(){
   this.setState({photoUrl:this.props.lqipUrl});// load the lqip first
    loadImage();//async load the main image
}

const loadImage = async (pUrl)=&amp;gt;{
   const tImage = new Image(pUrl);
   const that = this;
    tImage.onLoad = ()=&amp;gt;{that.setState({photoUrl:pUrl};// change to the main image after loading 
    tImage.src = pUrl;
}

render(){
  return (
     &amp;lt;img src={this.state.photoUrl} loading="lazy" /&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could also apply some blur effect on the image tag when the LQIP is loaded , when the main image is loaded we can animate the blur to sharpen the image. &lt;/p&gt;

&lt;p&gt;Some good tips on lazy loading &lt;a href="https://imagekit.io/blog/lazy-loading-images-complete-guide/"&gt;https://imagekit.io/blog/lazy-loading-images-complete-guide/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dominant color of the image as a background color&lt;/strong&gt;&lt;br&gt;
I like the idea of using a background color for the image div while the main image loads incase we dont use a lqip. This background color is a dominant color in the image, so it looks a little seamless when the image with similar colors loads. &lt;br&gt;
If we use stock photo apis we get the dominant color as an attribute in the API we can use.&lt;br&gt;
I have seen this in action on pexels.com&lt;/p&gt;

&lt;p&gt;Cover Image credits - Photo by Samer Daboul from Pexels &lt;a href="https://www.pexels.com/photo/close-up-shot-of-printed-photos-1226721/"&gt;https://www.pexels.com/photo/close-up-shot-of-printed-photos-1226721/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Do share in the comments if you have any other approaches to lazy loading images.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ux</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Simple function to print table in browser console</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Thu, 16 May 2024 13:52:39 +0000</pubDate>
      <link>https://dev.to/amythical/consoletable-5ei4</link>
      <guid>https://dev.to/amythical/consoletable-5ei4</guid>
      <description>&lt;p&gt;TLDR - &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/console/table_static"&gt;console.table()&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;When debugging for performance bottlenecks often we want to run through loops and function calls and record the time between the calls and see the bottle necks so we can optimise them.&lt;/p&gt;

&lt;p&gt;I have found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/console/table_static"&gt;console.table()&lt;/a&gt; to be very helpful.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let tfun1s,tfun2e,tfun2s,tfun2e,ts,te = 0;

ts = performance.now();
tfun1s = performance.now();
fun1();
tfun1e = performance.now();

tfun2s = performance.now();
fun2();
tfun2e = performance.now();
te = performance.now();

// time in microseconds
console.table(
   ["fun1",tfun1e-tfun1s],
   ["fun2",tfun2e-tfun2s],
   ["Total",te-ts],
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the console the output shows up like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1ehvemr7l0ockcge81d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1ehvemr7l0ockcge81d.png" alt="Image description" width="800" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I found this much later and its so much easier to use and control with wrapper functions instead of numerous console logs dotted along the code.&lt;/p&gt;

&lt;p&gt;Happy debugging!&lt;/p&gt;

&lt;p&gt;Would like to know any other helpful tips for debugging client side code for optimisation - please leave you comments.&lt;/p&gt;

&lt;p&gt;Cover photo credits - &lt;a href="https://unsplash.com/photos/a-computer-screen-with-a-bunch-of-code-on-it-ieic5Tq8YMk"&gt;https://unsplash.com/photos/a-computer-screen-with-a-bunch-of-code-on-it-ieic5Tq8YMk&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>debug</category>
      <category>programming</category>
    </item>
    <item>
      <title>New to DEV.to - About me</title>
      <dc:creator>amythical</dc:creator>
      <pubDate>Thu, 16 May 2024 05:28:01 +0000</pubDate>
      <link>https://dev.to/amythical/new-to-devto-6i8</link>
      <guid>https://dev.to/amythical/new-to-devto-6i8</guid>
      <description>&lt;p&gt;New to DEV.to but not new to Software development.&lt;br&gt;
20+ years into software development &lt;br&gt;
Here is my journey -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;90s dot com - noob web developer&lt;/li&gt;
&lt;li&gt;Built Web products for large enterprises&lt;/li&gt;
&lt;li&gt;Early member of a digital agency riding the Facebook/Orkut social media rush of mid 2000s (Successful exit)&lt;/li&gt;
&lt;li&gt;Crash landed my hardware IOT startup and learnt hardware is hard (&lt;a href="https://www.indiegogo.com/projects/neo-the-first-social-storytelling-toy#/"&gt;https://www.indiegogo.com/projects/neo-the-first-social-storytelling-toy#/&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Currently building a webapp to help businesses create videos with ease
Trying to find the first early users. Building and  learning something new everyday with javascript/wasm/ffmpeg/aws/C and loving it!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What brings me here ?&lt;br&gt;
To share my experiences/learning and code snippets when I build and learn everyday&lt;br&gt;
Learning and connecting with other devs at dev.to&lt;/p&gt;

&lt;p&gt;What interests me?&lt;br&gt;
Im glad my favourite 'C language' is back and playing a major role in client side development via WASMs.&lt;br&gt;
With WASM the power of computing has improved on the browsers. I am keenly interested in leveraging WASM and other client side computing technologies to improve the product performance, user experience and at the same time optimise costs for the product builder.&lt;/p&gt;

&lt;p&gt;I have an eye on generative AI integrations &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As a developer to improve productivity &lt;/li&gt;
&lt;li&gt;As a builder to make a product smarter and provide more value to the user&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Happy to help and discuss any questions on Javascript, AWS(Lambda/SQS/SES/S3/CloudFront), FFMPEG, Emscripten, WebCodecs, javascript, browser based computing using WASM/Workers, AI integrations&lt;/p&gt;

&lt;p&gt;Header Image Credits - &lt;a href="https://www.agilenative.com/2017/01/hello-world/"&gt;https://www.agilenative.com/2017/01/hello-world/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ffmpeg</category>
      <category>webassembly</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
