<?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>Forem: sandeshsapkota</title>
    <description>The latest articles on Forem by sandeshsapkota (@sandeshsapkota).</description>
    <link>https://forem.com/sandeshsapkota</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%2F312978%2F27638c3c-e5e3-4e61-a0da-cc238e5814a6.jpg</url>
      <title>Forem: sandeshsapkota</title>
      <link>https://forem.com/sandeshsapkota</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sandeshsapkota"/>
    <language>en</language>
    <item>
      <title>Next JS : Basic features</title>
      <dc:creator>sandeshsapkota</dc:creator>
      <pubDate>Sat, 16 Apr 2022 17:00:46 +0000</pubDate>
      <link>https://forem.com/sandeshsapkota/next-js-basic-features-33n5</link>
      <guid>https://forem.com/sandeshsapkota/next-js-basic-features-33n5</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2c5xama40le8ay9a9xhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2c5xama40le8ay9a9xhy.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over the years, Next JS is becoming so popular and we have heard many times that our fellow developer talking about it and saying how great it is.  &lt;/p&gt;

&lt;p&gt;Truly such an amazing framework it is.&lt;/p&gt;

&lt;p&gt;Today, I would like to  talk about  what really Next JS is and will be covering its major features.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Next JS
&lt;/h1&gt;

&lt;p&gt;Next JS is a javascript framework built on top of  React JS which enables  &lt;strong&gt;static  page generation&lt;/strong&gt;  and &lt;strong&gt;server side rendering&lt;/strong&gt;  on  traditional react application, &lt;/p&gt;

&lt;p&gt;Next JS  offers few many other amazing features like  such as &lt;strong&gt;routing&lt;/strong&gt; and &lt;strong&gt;css modules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next JS gives us choice to choose weather to render on client side or  to render on server and &lt;strong&gt;also&lt;/strong&gt; we can choose it to be a hybrid application.&lt;/p&gt;

&lt;p&gt;Let's go through each of its features.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Generation
&lt;/h2&gt;

&lt;p&gt;Static Generation is the method of pre-rendering where the  HTML pages gets generated statically at the build time.  This means at the end when we build our application for the production, the HTML pages get generated with all the content and data.  and Next JS by default do this and we need not to worry about any configuration.&lt;/p&gt;

&lt;p&gt;Even if the page use external data which is in api, at the time of building the html will be generated after that api call is resolved.&lt;/p&gt;

&lt;p&gt;Here is a small snippet that shows  api call inside next js &lt;code&gt;getStaticProps&lt;/code&gt; function which  sends data to the products component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticProps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000/api/products&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;products&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;  
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;grid gap-4 p-6&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
  &lt;span class="p"&gt;{&lt;/span&gt;  
                &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;  
                        &lt;span class="na"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/products/[slug]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
  &lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;,}&lt;/span&gt;  
                    &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text-blue-600&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
  &lt;span class="p"&gt;})&lt;/span&gt;  
            &lt;span class="p"&gt;}&lt;/span&gt;  
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt; &lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the most vital feature of Next JS. This helps to boost performance*  and as well as better SEO because  the HTML get fetched from the server.&lt;/p&gt;

&lt;p&gt;It is recommended to use static generation for static pages like e-commerce pages, blogs and marketing pages.&lt;/p&gt;

&lt;p&gt;From the next JS official doc&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://nextjs.org/docs/basic-features/pages#static-generation-recommended" rel="noopener noreferrer"&gt;&lt;strong&gt;Static Generation (Recommended)&lt;/strong&gt;&lt;/a&gt;: The HTML is generated at  &lt;strong&gt;build time&lt;/strong&gt;  and will be reused on each request.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;*because unlike plain react app where the DOM elements loads after loading the main Javascript file which takes more time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Side Rendering
&lt;/h2&gt;

&lt;p&gt;So we use Static Generation whenever when we have static type pages but what do we do when we have data that keep changing. Here, comes server side rendering.&lt;/p&gt;

&lt;p&gt;In server side rendering the HTML get generated  at server at each request. for eg we have a products page where products get added and deleted fast at that time we use  Next JS  &lt;code&gt;getServerSideProps&lt;/code&gt; method and fetch api inside this function . &lt;/p&gt;

&lt;p&gt;So each time user visit the products page the api get called and html get generated at the server and sends to the client. The way we send props to the component is same with &lt;code&gt;getStaticProps&lt;/code&gt;  function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiURL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
  &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CSS Module
&lt;/h2&gt;

&lt;p&gt;Yaaaaay! best part !! with CSS Module we can scope css. Here is how to use CSS Module &lt;/p&gt;

&lt;p&gt;First &lt;strong&gt;create&lt;/strong&gt; a css file with &lt;code&gt;Filename.module.css&lt;/code&gt; and &lt;strong&gt;import it&lt;/strong&gt; in JS file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.title&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  
&lt;span class="nc"&gt;.description&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="nc"&gt;.description&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4rem&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
  &lt;span class="nl"&gt;line-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../styles/Home.module.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;  

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
             &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;grid gap-4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
                 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Next&lt;/span&gt; &lt;span class="nx"&gt;JS&lt;/span&gt; &lt;span class="nx"&gt;Title&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt; &lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;                 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Next&lt;/span&gt; &lt;span class="nx"&gt;JS&lt;/span&gt; &lt;span class="nx"&gt;Title&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt; &lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;             &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;     &lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this way the  &lt;code&gt;Home.module.css&lt;/code&gt; file will get loaded only when the &lt;code&gt;Home&lt;/code&gt; component renders.  and for the global stylesheet Next JS let us import the css file directly only in &lt;code&gt;app.js&lt;/code&gt; file but we can not directly import css files in other  &lt;code&gt;js&lt;/code&gt; files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../styles/globals.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use &lt;code&gt;sass&lt;/code&gt; by installing &lt;code&gt;sass&lt;/code&gt; package. &lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Routing
&lt;/h2&gt;

&lt;p&gt;Routing with Next JS is super easy. It has file based system routing for pages. for  e.g if we add a file &lt;code&gt;product.js&lt;/code&gt;  inside &lt;code&gt;pages&lt;/code&gt; directory  and &lt;code&gt;/product&lt;/code&gt; will be automatically available as route.&lt;/p&gt;

&lt;p&gt;But to be available as route &lt;code&gt;product.js&lt;/code&gt;  should at least export a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Products&lt;/span&gt; &lt;span class="nx"&gt;listing&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Product&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also make a &lt;code&gt;product&lt;/code&gt; directory and inside it create &lt;code&gt;index.js&lt;/code&gt; file and the &lt;code&gt;/product&lt;/code&gt; routing will be available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatic Code Splitting
&lt;/h2&gt;

&lt;p&gt;As I have already explained, with css module the specific module css will render only for that component. Like this, Next JS also make chunks of Javascript file for specific files. for e.g if I have a library imported for a page that animates something or does something and is only in the current page, Next JS bundles this library only for this page but if the library used in multiple pages Next JS will bundle it globally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Image Optimization
&lt;/h2&gt;

&lt;p&gt;If we have heavier images in size, Next JS optimize the correctly sized image for each device, eventually which  helps us to improve largest contentful paint. And these images are loaded only when the images entered the viewport.&lt;/p&gt;

&lt;p&gt;For this we need to import &lt;code&gt;'next/image'&lt;/code&gt; component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;next/image&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Hero&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;HERO&lt;/span&gt; &lt;span class="nx"&gt;SECTION&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt;
        &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path to the image&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;350px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  
        &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;300px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;
        &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Picture of the author&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;src&lt;/strong&gt;, &lt;strong&gt;width&lt;/strong&gt; and &lt;strong&gt;height&lt;/strong&gt; properties are necessary for the image component. &lt;/p&gt;

&lt;p&gt;When the next image component loads that image sits on already allocated space which means image component solves another web vital Cumulative layout shift. Read more &lt;a href="https://sortable.com/blog/ad-ops/what-is-cumulative-layout-shift-cls/#:~:text=Cumulative%20Layout%20Shift%20(CLS)%20is,as%20the%20user%20views%20it" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Experience with Next JS
&lt;/h1&gt;

&lt;p&gt;Developing performatic application is such a complex task . pondering over optimizing images , separating css and loading necessary css only for that specific page  (&lt;strong&gt;scoping&lt;/strong&gt;) and dealing with initial loading time takes a lot of work and time and Here we have Next JS which solves those problems altogether.&lt;/p&gt;

&lt;p&gt;It has been really great experience working with Next JS and I personally feel that it is  evolving for the modern web will be there for few years to come.&lt;/p&gt;

&lt;h4&gt;
  
  
  Thank you  for reading.
&lt;/h4&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>reusable tab with nesting feature
</title>
      <dc:creator>sandeshsapkota</dc:creator>
      <pubDate>Wed, 26 Aug 2020 08:21:38 +0000</pubDate>
      <link>https://forem.com/sandeshsapkota/reusable-tab-with-nesting-feature-pnl</link>
      <guid>https://forem.com/sandeshsapkota/reusable-tab-with-nesting-feature-pnl</guid>
      <description>&lt;p&gt;Hi everyone !  hope you having a great day.&lt;/p&gt;

&lt;p&gt;while working project to projects, a  developer may have to reuse things that he has already built before.&lt;/p&gt;

&lt;p&gt;so based on my experience, I have to build some component like &lt;code&gt;tab&lt;/code&gt; or &lt;code&gt;accordion&lt;/code&gt; or any other similar thing  again and again in projects to projects.&lt;/p&gt;

&lt;p&gt;So, I thought to built a &lt;code&gt;tab&lt;/code&gt; and if I need to build a tab next time in any project I am  going to use the same &lt;code&gt;tab&lt;/code&gt; that I have already built  which will save a huge time for me. &lt;/p&gt;

&lt;p&gt;Then I came up with this &lt;a href="https://www.digitalocean.com/community/tutorials/js-tabs"&gt;this blog&lt;/a&gt; . Thanks to  Josh Stoddard . I have took reference from his code while trying to make it more effective to use. &lt;/p&gt;

&lt;p&gt;And it can be use in multiple section in a page (many different tabs). &lt;/p&gt;

&lt;p&gt;so demo below !&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sandeshsapkota/embed/GRoGEBz?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;keeping in mind few things to use this tab would be helpful.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the header of the tab must have &lt;code&gt;.tab&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;tab items must be wrapped with &lt;code&gt;.tab__list&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;tab contents items must be wrapped with &lt;code&gt;.tab__content&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;if other than &lt;code&gt;first item&lt;/code&gt; is required to show active initially, add &lt;code&gt;is--active&lt;/code&gt; class on the &lt;code&gt;tab__item&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;if you are going to use nested tab, repeating 1 to 3 is necessary&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thank you ! &lt;/p&gt;

&lt;p&gt;enjoy !&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating an bubble animation with javascript</title>
      <dc:creator>sandeshsapkota</dc:creator>
      <pubDate>Sun, 31 May 2020 12:02:13 +0000</pubDate>
      <link>https://forem.com/sandeshsapkota/creating-an-bubble-animation-with-javascript-na</link>
      <guid>https://forem.com/sandeshsapkota/creating-an-bubble-animation-with-javascript-na</guid>
      <description>&lt;p&gt;Hello everyone ! hope you are doing well !&lt;/p&gt;

&lt;p&gt;Today, we are going to build a bubble animation with javascript. Pre-requisite is basic understanding in HTML, CSS And Javascript.&lt;/p&gt;

&lt;p&gt;Here is what we are going to build today. &lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sandeshsapkota/embed/wvKVYeL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Lets start with what we need for Html&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;div id="app"&amp;gt;  
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for CSS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*,  
*:before,  
*:after {  
  margin: 0;  
  padding: 0;  
  box-sizing: border-box;  
}  

body {  
  background: rgba(240, 248, 255, 0.9);  
  overflow: hidden;  
}  

.bubble {  
  position: absolute;  
  top: 0;  
  left: 0;  
  border-radius: 50%;  
  cursor: pointer;  
  transition: linear 2s, transform .2s;  
}  

.bubble:hover {  
  transform: scale(1.7);  
}  

.bubble--bust {  
  transform: scale(1.8);  
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;.bubble&lt;/code&gt; is the styling class for the random circle that appears on the dom and  &lt;code&gt;.bubble-bust&lt;/code&gt; is the class that we add to the &lt;code&gt;bubble&lt;/code&gt; right before the &lt;code&gt;bubble&lt;/code&gt; dissaper, to make the animation better.&lt;/p&gt;

&lt;p&gt;that's it for HTML, and CSS part. this is enough for application. lets go to javascript slowly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const root = document.querySelector('#app');  
const {innerHeight, innerWidth} = window;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;root&lt;/code&gt; is the node where we append &lt;code&gt;bubble&lt;/code&gt; . And we will need &lt;code&gt;innerHeight&lt;/code&gt;, &lt;code&gt;innerWidth&lt;/code&gt; of the viewport  to position the bubble in random values between &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;innerWidth&lt;/code&gt; and &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;innerHeight&lt;/code&gt; on &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; axis repectively.&lt;/p&gt;

&lt;p&gt;so we are going to create each bubble by initiating a  &lt;code&gt;class Bubble()&lt;/code&gt; which is function constructor. lets see what we are going to write inside the &lt;code&gt;constructor&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Bubble {  
    constructor() {  
        this.bubbleSpan = undefined;  
        this.handleNewBubble();  
        this.color = this.randomColor();  

        this.posX = 0;  
        this.posY = 0;  

        // setting height and width of the bubble  
        this.height = this.randomNumber(60, 20);  
        this.width = this.height;  

         this.bubbleEnd.call(this.bubbleSpan,this.randomNumber(6000, 3000))  
    }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from the first three lines we are creating &lt;code&gt;this.handleNewBubble()&lt;/code&gt;  creates a new node and &lt;code&gt;this.color = this.randomColor()&lt;/code&gt; sets the random background color.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.height = this.randomNumber(60, 20);  
this.width = this.height;

this.bubbleEnd.call(this.bubbleSpan, this.randomNumber(6000, 3000))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is for setting height and width of the bubble between 20  to 60px. and width will be equal to height. And &lt;code&gt;this.bubbleEnd()&lt;/code&gt; function is for removing the bubble between 3 to 6 second after it's appearence.&lt;/p&gt;

&lt;p&gt;Lets see all other major function of our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // creates and appends a new bubble in the DOM
    handleNewBubble() {
        this.bubbleSpan = document.createElement('span');
        this.bubbleSpan.classList.add('bubble');
        root.append(this.bubbleSpan);
        this.handlePosition()
        this.bubbleSpan.addEventListener('click', this.bubbleEnd)
    }

    handlePosition() { // positioning the bubble in different random X and Y
        const randomY = this.randomNumber(60, -60);
        const randomX = this.randomNumber(60, -60);

        this.bubbleSpan.style.backgroundColor = this.color;
        this.bubbleSpan.style.height = this.height + "px";
        this.bubbleSpan.style.width = this.height + "px";

        this.posY = this.randomNumber(innerHeight - 20, 20);
        this.posX = this.randomNumber(innerWidth - 20, 20);

        this.bubbleSpan.style.top = this.posY + 'px';
        this.bubbleSpan.style.left = this.posX + 'px';

        const randomSec = this.randomNumber(4000, 200);
        setTimeout(this.handlePosition.bind(this), randomSec); // calling for re-position of bubble
    }

    bubbleEnd(removingTime = 0) {
        setTimeout(() =&amp;gt; {
            this.classList.add('bubble--bust');
        }, removingTime === 0 ? removingTime : removingTime - 100);

        setTimeout(() =&amp;gt; {
            this.remove();
            bubbles.push(new Bubble())
        }, removingTime)

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Long thing short ...&lt;/p&gt;

&lt;p&gt;so, &lt;code&gt;handleNewBubble()&lt;/code&gt; function insert a new node to the DOM and calls for &lt;code&gt;handlePosition()&lt;/code&gt; function. it also asign the event handler if the buble is clicked it will be remove from the DOM because we are calling &lt;code&gt;bubbleEnd()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handlePosition()&lt;/code&gt; function positon the &lt;code&gt;bubble&lt;/code&gt; , styles it with height, width and background-color and re-position it.&lt;/p&gt;

&lt;p&gt;And, &lt;code&gt;bubbleEnd()&lt;/code&gt; function  just remove the &lt;code&gt;bubble&lt;/code&gt; node from the DOM and it adds &lt;code&gt;bubble--bust&lt;/code&gt; class to the node before removing to make the &lt;code&gt;bubble&lt;/code&gt; bigger to create animation.&lt;/p&gt;

&lt;p&gt;And finally here is where we call the &lt;code&gt;new Bubble()&lt;/code&gt; so new bubble appears on the DOM after every second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// creating many bubble instance in every second  
setInterval(function () {  
    new Bubble()  
}, 1000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We made it :) Gracias !&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sandeshsapkota/embed/wvKVYeL?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>animation</category>
      <category>codepen</category>
    </item>
    <item>
      <title>magnet-cursor </title>
      <dc:creator>sandeshsapkota</dc:creator>
      <pubDate>Sat, 25 Jan 2020 13:46:17 +0000</pubDate>
      <link>https://forem.com/sandeshsapkota/building-a-magnet-cursor-animation-3bk9</link>
      <guid>https://forem.com/sandeshsapkota/building-a-magnet-cursor-animation-3bk9</guid>
      <description>&lt;p&gt;Inspired from this &lt;a href="https://fuegocaminaconmigo.com/"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/sandeshsapkota/embed/zYxRvKp?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>codepen</category>
      <category>cursoranimation</category>
      <category>javascript</category>
      <category>css</category>
    </item>
  </channel>
</rss>
