<?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: HARSH SINGH</title>
    <description>The latest articles on Forem by HARSH SINGH (@hs309123).</description>
    <link>https://forem.com/hs309123</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%2F1858029%2F52bca989-58f3-4b7a-9de5-3ee821b82b73.png</url>
      <title>Forem: HARSH SINGH</title>
      <link>https://forem.com/hs309123</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hs309123"/>
    <language>en</language>
    <item>
      <title>Mastering Next.js: Decoding Server-Side and Client-Side Components</title>
      <dc:creator>HARSH SINGH</dc:creator>
      <pubDate>Sun, 04 Aug 2024 21:12:56 +0000</pubDate>
      <link>https://forem.com/hs309123/mastering-nextjs-decoding-server-side-and-client-side-components-n2e</link>
      <guid>https://forem.com/hs309123/mastering-nextjs-decoding-server-side-and-client-side-components-n2e</guid>
      <description>&lt;p&gt;Hi Guys! Welcome back to my blog with mastering nextjs with me.&lt;br&gt;
Today we'll get to know about Server-Side and Client-Side Components in Next.js. It is a particularly fun topic to do some research about as the concept is rather simple but hides many complexities behind the simple use of these components.&lt;/p&gt;

&lt;p&gt;Understanding the key differences and where to use these components will help you to  building optimized, performant, and SEO-friendly applications and fully utilize the powerfull features that Next.js provide. In this blog, we’ll dive deep into these two types of components, explore their key differences, and discuss when to use each.&lt;/p&gt;
&lt;h1&gt;
  
  
  What are Server-Side Components?
&lt;/h1&gt;

&lt;p&gt;Every Component that is made in Next.js is by default is a Server-Side Component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//  app/page.jsx

async function getData() {
  const res = await fetch('https://api.example.com/...')
  // The return value is *not* serialized
  // You can return Date, Map, Set, etc.

  if (!res.ok) {
    // This will activate the closest `error.js` Error Boundary
    throw new Error('Failed to fetch data')
  }

  return res.json()
}

export default async function Page() {
  const data = await getData()
 if(data){
  return &amp;lt;main&amp;gt;&amp;lt;/main&amp;gt;
 }
 else{
 notFound()
 }

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

&lt;/div&gt;



&lt;p&gt;Next.js provide the native fetch feature in which there are many options to revalidate the cache or do not store the cache at all. You can read more about it here. &lt;a href="https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching" rel="noopener noreferrer"&gt;Link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the Server-Side Component, the component is rendered on the server and whatever data needs to be fetched or API calls needed to be made happens on the server side. Complete HTML data is generated on the server and sent to the browser to load. &lt;br&gt;
Once the HTML is loaded, JavaScript takes over to "hydrate" the page, making it interactive by attaching event listeners and enabling any dynamic client-side functionality.&lt;/p&gt;
&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improved SEO&lt;/strong&gt;: Since the content is pre-rendered on server, makes it easier for search engines to crawl and index the page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Faster Initial Load Time&lt;/strong&gt;:Users receive a fully-rendered page immediately, which reduces the time it takes for them to see and interact with the content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better Performance on Slow Networks&lt;/strong&gt;:Since the HTML is pre-rendered, users on slower networks can see the content faster, even if the client-side JavaScript takes longer to load.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Some Limitations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased Server Load&lt;/strong&gt;: Since the server handles rendering, it can become a bottleneck under heavy traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slower Interactions&lt;/strong&gt;: After the initial page load, interactions might be slower compared to fully client-rendered applications, especially if frequent server round-trips are required.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  What are Client-Side Components?
&lt;/h1&gt;

&lt;p&gt;Every component in Next.js is Server Component but after adding "use client" on top of the component it becomes a Client component.&lt;/p&gt;

&lt;p&gt;After making it a client component, it will have access to all the browser functionalities like the window object, setTimeOut etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client"

import { useEffect, useState } from 'react';

export default function ClientSideComponent() {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() =&amp;gt; {
    fetch('https://api.example.com/data')
      .then((res) =&amp;gt; res.json())
      .then((data) =&amp;gt; {
        setData(data);
        setLoading(false);
      });
  }, []);

  if (loading) return &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt;;

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Client-Side Component Example&amp;lt;/h1&amp;gt;
      &amp;lt;pre&amp;gt;{JSON.stringify(data, null, 2)}&amp;lt;/pre&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this when request is made to the server, it responds with the minimal amount of HTML or HTML shell like a div which happens in react and that gets loaded after that all the javasript files are loaded and executed and all the API calls and rendering of the jsx components occurs at this time and we will get a fully interactive page.&lt;br&gt;
User can interact with the page with reloading it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rich Interactivity&lt;/strong&gt;: Ideal for building highly interactive user interfaces. They enable real-time updates, animations, and smooth transitions without requiring a full page reload.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better User Experience for Single-Page Applications (SPAs)&lt;/strong&gt;: Since the content is rendered on the client, users experience a seamless flow as they navigate through different parts of the application. This is a common approach in SPAs, where only a portion of the page updates in response to user interactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced Server Load&lt;/strong&gt;: By offloading rendering tasks to the client, CSR reduces the server's workload.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility in Data Fetching&lt;/strong&gt;: CSR allows developers to fetch data on-demand, meaning you can load data only when needed, based on user interactions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Some Limitations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slower Initial Load Time&lt;/strong&gt; : Slower load time as the page is rendered on the browser after the javascript is loaded on to the browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SEO Challenges&lt;/strong&gt;: As you have guessed it, Since the page is not pre-rendered hence it is difficult for search engines to crawl and index the pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Potential for Poor User Experience on Slow Networks&lt;/strong&gt;: Users on slow networks may experience delays while waiting for JavaScript to load and execute, leading to a suboptimal user experience. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Key Differences Between Server-Side and Client-Side Components
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Rendering:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-Side&lt;/strong&gt;:Rendered on the server before the HTML is sent to the client's browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client-Side&lt;/strong&gt;:Rendered directly in the client's browser after the initial HTML is loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Initial Load Time
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-Side&lt;/strong&gt;: Typically faster,server sends pre-rendered HTML page to browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client-Side&lt;/strong&gt;: May have slower initial load, JS files are downloaded and then executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  SEO
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-Side&lt;/strong&gt;: Better, as easy to crawl pre-rendered HTML pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client-Side&lt;/strong&gt;: Challenging, as content is rendered dynamically on browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Interactivity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-Side&lt;/strong&gt;: Initial page is fully rendered and static, with interactivity added after the page is hydrated by JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client-Side&lt;/strong&gt;: Highly interactive from the start, with dynamic content that can change based on user actions or real-time data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Data Fetching
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-Side&lt;/strong&gt;: Fetched on the server, component rendered with complete content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client-Side&lt;/strong&gt;: Fetched on the Client/Browser, may lead to performance issues on slower network.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Many Client components can contain Server Components and Vice versa&lt;/em&gt;&lt;/strong&gt;&lt;br&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%2Fuwlkwyyt9wapi3ou11hp.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%2Fuwlkwyyt9wapi3ou11hp.png" alt="Server Components Containing Client Components and Vice Versa" width="352" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-Side Components&lt;/strong&gt;: Ideal for content-heavy, SEO-focused applications where initial load time is critical, such as blogs, e-commerce sites, and marketing pages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Client-Side Components&lt;/strong&gt;: Best for highly interactive applications, single-page applications (SPAs), or scenarios where content needs to be frequently updated or personalized.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nextjs</category>
      <category>frontend</category>
      <category>react</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>Mastering Next.js: My Journey from SSG to SSR Through Trial and Error</title>
      <dc:creator>HARSH SINGH</dc:creator>
      <pubDate>Mon, 29 Jul 2024 19:38:33 +0000</pubDate>
      <link>https://forem.com/hs309123/mastering-nextjs-my-journey-from-ssg-to-ssr-through-trial-and-error-3j9c</link>
      <guid>https://forem.com/hs309123/mastering-nextjs-my-journey-from-ssg-to-ssr-through-trial-and-error-3j9c</guid>
      <description>&lt;p&gt;Hi Guys! In this blog i wanna share with you the next new project that I've been working on for a good amount of time. It really takes me back to when i started my journey with MERN stack when i used to struggle in every project that I made but after having gone through all that hardships I can still say that getting stuck on some topics still freaks me out a little bit. &lt;/p&gt;

&lt;p&gt;I want to share with all of you about the difference between SSG and SSR.&lt;br&gt;
I was doing a minor project of making a blog site and i wanted to generate the blogs on the build time but It was not genrating and after 1 week of searching and wandering on internet learning about the difference I finally understood what is the difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Next.js is a powerfull React framework developed by vercel. It has several built-in features to enhance development and user experience.&lt;br&gt;
Some of the features are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hybrid Static &amp;amp; Server Rendering : It supports both SSR and SSG, allowing developers to choose best of any according to their use-case.&lt;/li&gt;
&lt;li&gt;API Routes : It allows you to build API endpoints in the nextjs app itself eliminating the need for a separate backend service for simple API tasks.&lt;/li&gt;
&lt;li&gt;Automatic Code Splitting and Optimization : It splits the code and only send necessary javasript to browser and has many built-in optimization like Image optimization, faster builds etc.&lt;/li&gt;
&lt;li&gt;File-system Routing : It has a file sytem routing(whichever you are using app or page router) automatically creates routes.&lt;/li&gt;
&lt;li&gt;TypeScript Support : It is recommended to code in typescript as it catches many errors in the runtime only which if not used may cause problems in build time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SSG VS SSR
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Server Side Generation (SSG)
&lt;/h3&gt;

&lt;p&gt;SSG generates static HTML pages at the build time. The content is pre-rendered which means it doesn't change until you rebuild the site.&lt;br&gt;
It is best for pages that doesn't change too frequetly like the Marketing pages or documentation which doesn't change too frequently.&lt;br&gt;
It basically is generated during the build time and are static so they load extremely quickly.&lt;/p&gt;

&lt;p&gt;In this to get the Dynamic path that you want to render on build time you can use the &lt;code&gt;getStaticParams&lt;/code&gt;. You can read more about this here. &lt;a href="https://nextjs.org/docs/app/api-reference/functions/generate-static-params" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Server Side Rendering(SSR)
&lt;/h3&gt;

&lt;p&gt;SSR generates HTML pages on each request. The server renders the HTML dynamically for each incoming request. It is best for pages that need to be dynamic and up-to-date on every request. This includes user dashboards, news feeds, and other content that changes frequently or is user-specific.&lt;br&gt;
Since HTML is generated on each request so it is slower than SSG but it ensures that the content is always fresh and up-to-date.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Thought
&lt;/h3&gt;

&lt;p&gt;My journey with Next.js, SSG, and SSR has been filled with learning opportunities. Each error and challenge has deepened my understanding and improved my problem-solving skills. I encourage you to experiment with both SSG and SSR in your projects to see how they can best serve your needs.&lt;/p&gt;

&lt;p&gt;Thank you for reading! I hope my experiences help you in your Next.js journey. Feel free to share your own experiences or ask questions in the comments below. Let's continue learning and growing together as a community. Happy coding!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>react</category>
      <category>fullstack</category>
    </item>
  </channel>
</rss>
