<?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: M Saad Ahmad</title>
    <description>The latest articles on Forem by M Saad Ahmad (@m_saad_ahmad).</description>
    <link>https://forem.com/m_saad_ahmad</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%2F1853445%2F521468fd-cc24-4c92-9f2f-ec930c04557b.jpg</url>
      <title>Forem: M Saad Ahmad</title>
      <link>https://forem.com/m_saad_ahmad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/m_saad_ahmad"/>
    <language>en</language>
    <item>
      <title>Day 61 of #100DaysOfCode — Python Refresher Part 1</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Sat, 04 Apr 2026 08:39:59 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-61-of-100daysofcode-python-refresher-part-1-51n2</link>
      <guid>https://forem.com/m_saad_ahmad/day-61-of-100daysofcode-python-refresher-part-1-51n2</guid>
      <description>&lt;p&gt;When I started my #100DaysOfCode journey, I began with frontend development using React, then moved into backend development with Node.js and Express. After that, I explored databases to understand how data is stored and managed, followed by building full-stack applications with Next.js. It is now time to start learning Python, not from scratch, but as a refresher to strengthen my fundamentals and expand my backend skillset.&lt;/p&gt;

&lt;p&gt;Learning Python strengthens my core programming skills and offers a new perspective beyond JavaScript. It aligns with backend development, data handling, and automation, allowing me to build on my existing knowledge and become a more versatile developer.&lt;/p&gt;

&lt;p&gt;Today, for Day 61, I focused on revisiting the core building blocks of Python.&lt;/p&gt;




&lt;h2&gt;
  
  
  Core Syntax
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Variables &amp;amp; Data Types
&lt;/h3&gt;

&lt;p&gt;Variables are used to store data, and Python automatically assigns the data type based on the value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ali&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;      &lt;span class="c1"&gt;# string
&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;          &lt;span class="c1"&gt;# integer
&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;99.99&lt;/span&gt;     &lt;span class="c1"&gt;# float
&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;  &lt;span class="c1"&gt;# boolean
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don’t need to declare types explicitly like in some other languages; Python handles it dynamically.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conditionals (&lt;code&gt;if/elif/else&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Conditionals let your program make decisions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Adult&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Teen&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Child&lt;/span&gt;&lt;span class="sh"&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 fundamental for controlling program flow, especially in backend logic (auth checks, validations, etc.).&lt;/p&gt;




&lt;h3&gt;
  
  
  Loops (&lt;code&gt;for&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Loops allow you to repeat actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&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 python"&gt;&lt;code&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;for&lt;/code&gt; is used more often in Python, especially when working with collections.&lt;/p&gt;




&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;Functions group logic into reusable blocks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They help keep your code clean and modular.&lt;/p&gt;




&lt;h2&gt;
  
  
  Data Structures
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Lists
&lt;/h3&gt;

&lt;p&gt;Lists store multiple items in order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Dictionaries
&lt;/h3&gt;

&lt;p&gt;Dictionaries store data in key-value pairs, similar to objects in JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ali&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Must Know:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Looping through dicts
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Nested data handling
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ali&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&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 directly maps to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JSON (backend APIs)&lt;/li&gt;
&lt;li&gt;Database data&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Sets
&lt;/h3&gt;

&lt;p&gt;Sets store unique values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# {1, 2, 3}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Tuples
&lt;/h3&gt;

&lt;p&gt;Tuples are like lists, but immutable (cannot be changed).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Loops &amp;amp; Iteration
&lt;/h2&gt;

&lt;p&gt;Looping through collections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;range()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nested loops (useful for complex data):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;Functions are where your code becomes structured and reusable:&lt;/p&gt;

&lt;p&gt;Functions make your code reusable and structured.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ali&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“How do I structure logic cleanly?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Good function design = cleaner code + easier debugging + better scalability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Working with JSON (IMPORTANT FOR BACKEND)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json_string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;json_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSON is how data is exchanged between frontend and backend.&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 python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;json_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ali&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json_string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&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 directly used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;Django / Flask&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Basic File Handling
&lt;/h2&gt;

&lt;p&gt;Working with files is a fundamental skill in backend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading files&lt;/li&gt;
&lt;li&gt;Writing files
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;file.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logs&lt;/li&gt;
&lt;li&gt;Data processing&lt;/li&gt;
&lt;li&gt;Simple storage tasks&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  List Comprehension (VERY USEFUL)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A shorter and cleaner way to write loops.&lt;/p&gt;

&lt;p&gt;Equivalent version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Cleaner + faster code&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Today wasn’t about learning something completely new; it was about reinforcing the fundamentals. Python feels simple on the surface, but mastering these basics is what makes you effective when building real-world applications.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 60 of #100DaysOfCode — Deploying the Task App</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Thu, 02 Apr 2026 21:01:12 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-60-of-100daysofcode-deploying-the-task-app-295o</link>
      <guid>https://forem.com/m_saad_ahmad/day-60-of-100daysofcode-deploying-the-task-app-295o</guid>
      <description>&lt;p&gt;Over the past two days, I built and styled a Task Manager app with Next.js, MongoDB, and Shadcn UI. Today was the final step: getting it live on Vercel.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Vercel?
&lt;/h2&gt;

&lt;p&gt;Vercel is made by the same team behind Next.js, so deployment is essentially zero-config. No server setup, no CI/CD pipeline to configure, just connect your repo and push.&lt;/p&gt;




&lt;h2&gt;
  
  
  Steps Followed
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Push code to GitHub&lt;/strong&gt;&lt;br&gt;
Make sure your project is in a GitHub repo before anything else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Import the project on Vercel&lt;/strong&gt;&lt;br&gt;
Go to &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;vercel.com&lt;/a&gt; → New Project → Import your GitHub repo. Vercel auto-detects it's a Next.js app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Add Environment Variables&lt;/strong&gt;&lt;br&gt;
This is the most important step. Your &lt;code&gt;.env.local&lt;/code&gt; file doesn't get pushed to GitHub, so you need to add your variables manually in Vercel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="n"&gt;MONGODB_URI&lt;/span&gt;=&lt;span class="n"&gt;your_mongodb_connection_string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to: Project Settings → Environment Variables → add them there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Deploy&lt;/strong&gt;&lt;br&gt;
Hit deploy. That's it. Vercel builds and hosts it automatically.&lt;/p&gt;

&lt;p&gt;Every &lt;code&gt;git push&lt;/code&gt; to your main branch triggers a new deployment going forward.&lt;/p&gt;




&lt;h3&gt;
  
  
  Gotcha: MongoDB Atlas Network Access
&lt;/h3&gt;

&lt;p&gt;If your app connected fine locally but throws errors in production, check MongoDB Atlas → Network Access. You need to allow connections from anywhere (&lt;code&gt;0.0.0.0/0&lt;/code&gt;) since Vercel uses dynamic IPs.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Environment variables must be added manually on Vercel — don't assume they carry over&lt;/li&gt;
&lt;li&gt;Vercel's build logs are really useful for debugging deployment errors&lt;/li&gt;
&lt;li&gt;Going from local to live took less than 5 minutes with Next.js + Vercel&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Live App
&lt;/h2&gt;

&lt;p&gt;The Task Management App is live on this &lt;a href="https://taskmanagementapp-nextjs.vercel.app/" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmej29n8ddztsqrzoim8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmej29n8ddztsqrzoim8b.png" alt="App Demo"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;That's Day 60 done. The app is live! 60 days down, 40 to go.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 59 of #100DayOfCode — Styling the Task Management App</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Thu, 02 Apr 2026 17:18:50 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-59-of-100dayofcode-styling-the-task-management-app-16mm</link>
      <guid>https://forem.com/m_saad_ahmad/day-59-of-100dayofcode-styling-the-task-management-app-16mm</guid>
      <description>&lt;p&gt;Yesterday on Day 58, I built a Task Manager app with Next.js, MongoDB, and Server Actions. The app worked, but it looked rough. Today was all about making it actually nice to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Used
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tailwind CSS&lt;/strong&gt; — for layout and spacing utilities&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shadcn UI&lt;/strong&gt; — for pre-built, accessible components I can own and customize&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up Shadcn
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx shadcn@latest init
npx shadcn@latest add button card badge input textarea &lt;span class="k"&gt;select &lt;/span&gt;label
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This drops the component source directly into your project under &lt;code&gt;/components/ui&lt;/code&gt;. No black-box library — you own the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Styled
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Layout&lt;/strong&gt; — A clean navbar with a centered &lt;code&gt;max-w-4xl&lt;/code&gt; main container across all pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task List&lt;/strong&gt; — A responsive 2-column grid on larger screens, with a friendly empty state when no tasks exist yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task Cards&lt;/strong&gt; — Each task is wrapped in a &lt;code&gt;Card&lt;/code&gt; with the title, a colored status badge, description, timestamps, and Edit/Delete actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task Form&lt;/strong&gt; — Proper &lt;code&gt;Label&lt;/code&gt; + &lt;code&gt;Input&lt;/code&gt;/&lt;code&gt;Textarea&lt;/code&gt;/&lt;code&gt;Select&lt;/code&gt; combinations with placeholders, and a dynamic submit button that says "Create Task" or "Update Task" depending on the context.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Gotcha with Shadcn Badge&lt;/p&gt;

&lt;p&gt;Shadcn's &lt;code&gt;Badge&lt;/code&gt; only ships with 4 variants: &lt;code&gt;default&lt;/code&gt;, &lt;code&gt;secondary&lt;/code&gt;, &lt;code&gt;destructive&lt;/code&gt;, &lt;code&gt;outline&lt;/code&gt;. There's no built-in &lt;code&gt;success&lt;/code&gt; or &lt;code&gt;warning&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of extending the component, I used a simple helper with Tailwind classes directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;statusStyles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;completed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bg-green-100 text-green-700 hover:bg-green-100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;in-progress&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bg-yellow-100 text-yellow-700 hover:bg-yellow-100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bg-gray-100 text-gray-600 hover:bg-gray-100&lt;/span&gt;&lt;span class="dl"&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Badge&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;statusStyles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Badge&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean enough, and no need to touch the component internals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;Button&lt;/code&gt; with &lt;code&gt;asChild&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Shadcn's &lt;code&gt;Button&lt;/code&gt; has an &lt;code&gt;asChild&lt;/code&gt; prop that lets you pass a &lt;code&gt;Link&lt;/code&gt; inside it and get all the button styles applied to the anchor — no wrapper divs needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;asChild&lt;/span&gt; &lt;span class="na"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"outline"&lt;/span&gt; &lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"sm"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`/tasks/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Edit&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of those small things that makes Shadcn feel really well thought out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;Shadcn isn't a traditional component library. You install only what you need, and the code lives in your project. That means full control — no fighting with overrides or &lt;code&gt;!important&lt;/code&gt; hacks.&lt;/p&gt;

&lt;p&gt;Tomorrow, for Day 60, I'll be deploying the app on &lt;strong&gt;Vercel&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 58 of #100DayOfCode — Building Task Management CRUD App with Next.js</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Wed, 01 Apr 2026 18:06:20 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-58-of-100dayofcode-building-task-management-crud-app-with-nextjs-3ihk</link>
      <guid>https://forem.com/m_saad_ahmad/day-58-of-100dayofcode-building-task-management-crud-app-with-nextjs-3ihk</guid>
      <description>&lt;p&gt;For Day 58 of my #100DaysOfCode journey, my goal was to build a basic but fully functional &lt;strong&gt;Task Management App&lt;/strong&gt; using Next.js. Up until this point, I had been learning individual Next.js concepts: server and client components, routing with the App Router, data fetching and caching, API routing, server actions, and connecting to a MongoDB database. I felt confident with each concept in isolation, but I hadn't yet put them all together in a single project.&lt;/p&gt;

&lt;p&gt;So today's goal was simple: build something real, something CRUD-based, that forces me to use everything I've learned so far in one place.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;p&gt;The app is a simple &lt;strong&gt;Task Manager&lt;/strong&gt; where you can create, view, edit, and delete tasks. Each task has a &lt;strong&gt;title&lt;/strong&gt;, &lt;strong&gt;description&lt;/strong&gt;, and a &lt;strong&gt;status&lt;/strong&gt; (pending, in-progress, or completed). The app has three pages and three components:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/tasks&lt;/code&gt; — displays all tasks stored in the database&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/tasks/new&lt;/code&gt; — a form to create a new task&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/tasks/[id]&lt;/code&gt; — a pre-filled form to edit an existing task&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Components:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;TaskList&lt;/code&gt; — receives all tasks and maps over them&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TaskCard&lt;/code&gt; — displays an individual task with Edit and Delete options&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TaskForm&lt;/code&gt; — a reusable form used on both the create and edit pages&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🛠 Concepts Used
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;App Router&lt;/strong&gt; — folder-based routing with dynamic segments like &lt;code&gt;[id]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server Components&lt;/strong&gt; — pages that fetch data directly from MongoDB without any client-side fetching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client Components&lt;/strong&gt; — components like &lt;code&gt;TaskForm&lt;/code&gt; and &lt;code&gt;TaskCard&lt;/code&gt; that handle user interaction and form inputs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server Actions&lt;/strong&gt; — functions that run on the server, connected directly to forms via the &lt;code&gt;action&lt;/code&gt; prop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MongoDB&lt;/strong&gt; — storing and querying tasks using &lt;code&gt;clientPromise&lt;/code&gt; without Mongoose&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;revalidatePath&lt;/code&gt;&lt;/strong&gt; — to refresh the page cache after every mutation so the UI always reflects the latest database state&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;redirect&lt;/code&gt;&lt;/strong&gt; — to send the user back to &lt;code&gt;/tasks&lt;/code&gt; after creating or updating a task&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Folder Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;next-crud-app/
├── app/
│   ├── page.js               --&amp;gt; Landing page
│   └── tasks/
│       ├── page.js           --&amp;gt; Lists all tasks (Server Component)
│       ├── new/
│       │   └── page.js       --&amp;gt; Create task form page (Server Component)
│       └── [id]/
│           └── page.js       --&amp;gt; Edit task form page (Server Component)
├── actions/
│   └── tasks.js              --&amp;gt; All server actions (CRUD)
│
├── db/
│   └── mongodb.js            --&amp;gt; MongoDB connection
│
│
├── components/
│   ├── TaskCard.jsx          --&amp;gt; Single task display (Client Component)
│   ├── TaskForm.jsx          --&amp;gt; Reusable form (Client Component)
│   └── TaskList.jsx          --&amp;gt; Maps over tasks (Server Component)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;db/mongodb.js&lt;/code&gt; — sets up and exports the MongoDB client connection used across all server actions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;actions/tasks.js&lt;/code&gt; — the heart of the app; contains all five CRUD functions that interact with MongoDB&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tasks/page.js&lt;/code&gt; — an async server component that fetches all tasks and passes them down to &lt;code&gt;TaskList&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tasks/[id]/page.js&lt;/code&gt; — receives the dynamic &lt;code&gt;id&lt;/code&gt; from &lt;code&gt;params&lt;/code&gt;, fetches that specific task, and passes it to &lt;code&gt;TaskForm&lt;/code&gt; for pre-filling&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Interesting Part — &lt;code&gt;actions/tasks.js&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The most interesting and challenging file to write was &lt;code&gt;actions/tasks.js&lt;/code&gt;. This is where all the CRUD logic lives and where everything connects together.&lt;/p&gt;

&lt;p&gt;What made it challenging was that I initially wrote it based on how I thought it would work, similar to how I had written a contact form previously. I passed plain objects to functions and queried MongoDB by &lt;code&gt;id&lt;/code&gt; as a plain string. But I quickly ran into several issues I wasn't aware of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Server actions receive &lt;code&gt;FormData&lt;/code&gt;, not plain objects.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When a server action is connected to a form via the &lt;code&gt;action&lt;/code&gt; prop, Next.js automatically passes &lt;code&gt;FormData&lt;/code&gt; to it. You have to use &lt;code&gt;formData.get("fieldName")&lt;/code&gt; to extract values — you can't just pass a plain object.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;MongoDB &lt;code&gt;_id&lt;/code&gt; is an &lt;code&gt;ObjectId&lt;/code&gt;, not a string.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When querying, updating, or deleting by &lt;code&gt;_id&lt;/code&gt;, you need to wrap the id with &lt;code&gt;new ObjectId(id)&lt;/code&gt; from the &lt;code&gt;mongodb&lt;/code&gt; package. A plain string won't match anything in the database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;code&gt;revalidatePath&lt;/code&gt; and &lt;code&gt;redirect&lt;/code&gt; are required.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Without &lt;code&gt;revalidatePath&lt;/code&gt;, the page cache doesn't update after a mutation, so the UI won't reflect changes. Without &lt;code&gt;redirect&lt;/code&gt;, the user just stays on the form page after submitting.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Serialization is needed when returning data.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;MongoDB returns special document objects that can't be passed directly as props to client components. Wrapping the result in &lt;code&gt;JSON.parse(JSON.stringify(data))&lt;/code&gt; converts it to a plain JavaScript object that Next.js can safely pass across the server-client boundary.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;clientPromise&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../db/mongo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ObjectId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;revalidatePath&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/cache&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;redirect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/navigation&lt;/span&gt;&lt;span class="dl"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&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="nx"&gt;client&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;clientPromise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;taskapp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tasks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;insertOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;status&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;revalidatePath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/tasks&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/tasks&lt;/span&gt;&lt;span class="dl"&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;
  
  
  What I Struggled With
&lt;/h2&gt;

&lt;p&gt;Two things genuinely confused me during this build:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Understanding which file uses which component and why.&lt;/strong&gt;&lt;br&gt;
With multiple pages and components, I kept getting confused about who imports what and why. For example, I wasn't sure if &lt;code&gt;TaskList&lt;/code&gt; should also import &lt;code&gt;TaskForm&lt;/code&gt;, or whether &lt;code&gt;TaskForm&lt;/code&gt; belonged on the page directly. Mapping out the dependency chain clearly helped a lot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tasks/page.js → TaskList → TaskCard
tasks/new/page.js → TaskForm
tasks/[id]/page.js → TaskForm
actions/tasks.js → used by TaskCard and TaskForm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Understanding how &lt;code&gt;actions/tasks.js&lt;/code&gt; actually works.&lt;/strong&gt;&lt;br&gt;
I initially wrote the file by instinct based on my contact form experience. But server actions in a CRUD context behave differently; they receive &lt;code&gt;FormData&lt;/code&gt;, need &lt;code&gt;ObjectId&lt;/code&gt; for queries, and require &lt;code&gt;revalidatePath&lt;/code&gt; to keep the UI in sync. Once I understood these gaps, the file made complete sense.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;What made this project unique as part of my #100DaysOfCode journey is that it wasn't about learning something new; it was about &lt;strong&gt;connecting everything I already knew into one working app&lt;/strong&gt;. It's one thing to understand server actions in isolation, and another thing entirely to wire them up to a form, pass the right data, hit the database, and have the UI update automatically.&lt;/p&gt;

&lt;p&gt;This project gave me that full picture for the first time. It also revealed the gaps in my understanding that I wouldn't have found without actually building something.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;The app is fully functional but completely unstyled. The next step is to add &lt;strong&gt;Tailwind CSS&lt;/strong&gt; to style the UI and then &lt;strong&gt;deploy it on Vercel&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Stay tuned for Day 59!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 57 of #100DayOfCode — Understanding Server Actions vs API Routes &amp; MongoDB in Next.js</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Tue, 31 Mar 2026 05:36:25 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-57-of-100dayofcode-understanding-server-actions-vs-api-routes-mongodb-in-nextjs-am8</link>
      <guid>https://forem.com/m_saad_ahmad/day-57-of-100dayofcode-understanding-server-actions-vs-api-routes-mongodb-in-nextjs-am8</guid>
      <description>&lt;p&gt;API routing in Next.js enables developers to create backend endpoints directly within their applications, allowing for request handling, data processing, and API exposure. However, as apps become more complex, especially with UI interactions like form submissions, API routes can become repetitive and cluttered.&lt;/p&gt;

&lt;p&gt;That's where &lt;strong&gt;Server Actions&lt;/strong&gt; offer a streamlined solution by enabling server-side logic execution directly from components, removing the need for a separate API layer. This keeps your code simpler and easier to maintain by placing logic closer to its usage.&lt;/p&gt;

&lt;p&gt;Today, for Day 57, the goal was to understand what server actions is and how we can connect to MongoDB using both API routing and server actions&lt;/p&gt;




&lt;h2&gt;
  
  
  What Are Server Actions in Next.js?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Server Actions&lt;/strong&gt; are a new feature in Next.js that allow you to run server-side code directly from your components &lt;strong&gt;without creating a separate API endpoint&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They are defined using the &lt;code&gt;"use server"&lt;/code&gt; directive and are typically used inside &lt;strong&gt;React Server Components&lt;/strong&gt; or triggered via forms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Idea:
&lt;/h3&gt;

&lt;p&gt;Instead of calling an API route, you call a server function directly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Simple Server Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/actions.js&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use server&lt;/span&gt;&lt;span class="dl"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&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="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Simulate saving to database&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User created:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&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;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="c1"&gt;// app/page.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createUser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./actions&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Page&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;form&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;createUser&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;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter name&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;button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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;/form&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;No API route. No fetch. Just direct server execution.&lt;/p&gt;




&lt;h2&gt;
  
  
  Server Actions vs API Routes in Next.js
&lt;/h2&gt;

&lt;p&gt;Here’s how they compare:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Server Actions&lt;/th&gt;
&lt;th&gt;API Routes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Setup&lt;/td&gt;
&lt;td&gt;Inline in components&lt;/td&gt;
&lt;td&gt;Separate endpoint files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network Request&lt;/td&gt;
&lt;td&gt;Not required&lt;/td&gt;
&lt;td&gt;Required (fetch/axios)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use Case&lt;/td&gt;
&lt;td&gt;UI-driven mutations&lt;/td&gt;
&lt;td&gt;Public or external APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Faster (no HTTP overhead)&lt;/td&gt;
&lt;td&gt;Slightly slower (HTTP roundtrip)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reusability&lt;/td&gt;
&lt;td&gt;Limited to app&lt;/td&gt;
&lt;td&gt;Can be used anywhere (frontend/backend)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Security&lt;/td&gt;
&lt;td&gt;Runs on server by default&lt;/td&gt;
&lt;td&gt;Needs manual handling&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  When to Use What?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Server Actions When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Handling &lt;strong&gt;form submissions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Performing &lt;strong&gt;simple mutations&lt;/strong&gt; (create/update/delete)&lt;/li&gt;
&lt;li&gt;Working tightly with UI components&lt;/li&gt;
&lt;li&gt;You don’t need a public API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Submitting a contact form or saving user preferences.&lt;/p&gt;




&lt;h3&gt;
  
  
  Use API Routes When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Building a &lt;strong&gt;public API&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Integrating with &lt;strong&gt;third-party services&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Handling &lt;strong&gt;complex backend logic&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You need endpoints accessible from &lt;strong&gt;mobile apps or external clients&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Payment processing, webhook handling, or external integrations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Connecting MongoDB with Next.js
&lt;/h2&gt;

&lt;p&gt;Connecting MongoDB with Next.js allows you to build full-stack applications where your frontend and database work seamlessly together. Whether you use Server Actions or API Routes, Next.js makes it straightforward to establish a database connection, perform CRUD operations, and manage data efficiently—all within the same project structure.&lt;/p&gt;




&lt;h2&gt;
  
  
  MongoDB with Server Actions
&lt;/h2&gt;

&lt;p&gt;You can directly connect to MongoDB inside a Server Action since it runs on the server.&lt;/p&gt;

&lt;h3&gt;
  
  
  💻 Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/mongodb.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;connectDB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connections&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;readyState&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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&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="c1"&gt;// app/actions.js&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;connectDB&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../lib/mongodb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../models/User&lt;/span&gt;&lt;span class="dl"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&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;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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 clean and avoids unnecessary API layers.&lt;/p&gt;




&lt;h2&gt;
  
  
  MongoDB with API Routes
&lt;/h2&gt;

&lt;p&gt;Here, you define a traditional API endpoint and connect to MongoDB inside it.&lt;/p&gt;

&lt;h3&gt;
  
  
  💻 Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/api/users.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;connectDB&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../../lib/mongodb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../../models/User&lt;/span&gt;&lt;span class="dl"&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connectDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&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;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&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;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;405&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="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Method not allowed&lt;/span&gt;&lt;span class="dl"&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// frontend usage&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;createUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&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="s2"&gt;/api/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ali&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is more flexible and reusable across platforms.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Server Actions and API Routes both solve similar problems but in different ways.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server Actions&lt;/strong&gt; simplify development by eliminating the need for API endpoints in many cases, making your code more direct and efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Routes&lt;/strong&gt; remain essential when you need flexibility, external access, or more complex backend logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When working with MongoDB:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Server Actions&lt;/strong&gt; for tight UI + database interactions.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;API Routes&lt;/strong&gt; when building scalable, reusable backend services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding and mastering both approaches gives us the flexibility to build modern, performant full-stack apps with Next.js&lt;/p&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 56 of #100DayOfCode — Next.js API routes</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Mon, 30 Mar 2026 04:34:55 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-56-of-100dayofcode-nextjs-api-routes-1p6h</link>
      <guid>https://forem.com/m_saad_ahmad/day-56-of-100dayofcode-nextjs-api-routes-1p6h</guid>
      <description>&lt;p&gt;When building full-stack applications, one of the biggest challenges is managing separate frontend and backend codebases. This is where Next.js API routes come in. With the App Router, Next.js allows you to build backend endpoints directly inside your project, no need for a separate Express server.&lt;/p&gt;

&lt;p&gt;Today, for day 56, the goal was to understand the API routing in Next.js&lt;/p&gt;




&lt;h2&gt;
  
  
  React.js + Node.js vs Next.js
&lt;/h2&gt;

&lt;h3&gt;
  
  
  In MERN:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Frontend → React&lt;/li&gt;
&lt;li&gt;Backend → Express (separate server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  In Next.js:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Same project = Frontend + Backend&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You don’t need a separate backend anymore.&lt;br&gt;
API routes live inside your app.&lt;/p&gt;


&lt;h2&gt;
  
  
  📁 Folder Structure (App Router Way)
&lt;/h2&gt;

&lt;p&gt;We create APIs like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;app/api/users/route.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This automatically maps to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/api/users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No extra routing setup needed.&lt;/p&gt;




&lt;h2&gt;
  
  
  HTTP Methods in Next.js API Routes
&lt;/h2&gt;

&lt;p&gt;Inside &lt;code&gt;route.js&lt;/code&gt;, you export functions like:&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;GET&lt;/span&gt;&lt;span class="p"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;PUT&lt;/span&gt;&lt;span class="p"&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;DELETE&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;Each function handles a specific HTTP method.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Important correction:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These are &lt;strong&gt;Web standard Request/Response handlers&lt;/strong&gt;, not Express handlers.&lt;/li&gt;
&lt;li&gt;They run in &lt;strong&gt;server environments (Node.js or Edge Runtime)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Request &amp;amp; Response Handling
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Basic Example:
&lt;/h3&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;GET&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="nx"&gt;Response&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="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&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;h3&gt;
  
  
  Access Request Data:
&lt;/h3&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;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&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="nx"&gt;body&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;request&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="nx"&gt;Response&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="na"&gt;received&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&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 replaces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express &lt;code&gt;req.body&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Express &lt;code&gt;res.json()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Better understanding:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;request&lt;/code&gt; = Web API &lt;code&gt;Request&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Response.json()&lt;/code&gt; = built-in helper (Next.js sugar over standard Response)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Connecting Frontend to API
&lt;/h2&gt;

&lt;p&gt;From your 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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="s2"&gt;/api/users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&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;res&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No need for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express server&lt;/li&gt;
&lt;li&gt;Axios (optional)&lt;/li&gt;
&lt;li&gt;CORS setup (in most cases)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Dynamic API Routes
&lt;/h2&gt;

&lt;p&gt;Create dynamic endpoints like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;app/api/users/[id]/route.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Access params:
&lt;/h3&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;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;params&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="nx"&gt;Response&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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;Same concept as dynamic pages.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Correction:&lt;/strong&gt;&lt;br&gt;
You don’t access &lt;code&gt;params.id&lt;/code&gt; directly, you must destructure it from the second argument.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛡️ Basic Validation &amp;amp; Error Handling
&lt;/h2&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;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&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="nx"&gt;body&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;request&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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="nx"&gt;Response&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Name required&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Response&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="na"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;You control:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Status codes&lt;/li&gt;
&lt;li&gt;Errors&lt;/li&gt;
&lt;li&gt;Responses&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Database Integration
&lt;/h2&gt;

&lt;p&gt;You can connect databases directly inside API routes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MongoDB (e.g., with Mongoose)&lt;/li&gt;
&lt;li&gt;PostgreSQL (e.g., with Prisma)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;API routes act like your backend layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best practice:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reuse DB connections (avoid reconnecting on every request in dev)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔥 Why This Is Powerful
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;No separate backend setup&lt;/li&gt;
&lt;li&gt;Full-stack in one project&lt;/li&gt;
&lt;li&gt;Faster development&lt;/li&gt;
&lt;li&gt;Cleaner architecture&lt;/li&gt;
&lt;/ul&gt;




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

&lt;ul&gt;
&lt;li&gt;Next.js API routes replace Express in many cases&lt;/li&gt;
&lt;li&gt;File-based routing makes APIs super simple&lt;/li&gt;
&lt;li&gt;Uses Web standard &lt;code&gt;Request&lt;/code&gt; &amp;amp; &lt;code&gt;Response&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Works seamlessly with frontend via &lt;code&gt;fetch()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Supports dynamic routes and full backend logic&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Coming from React + Node.js, this felt weird at first…&lt;br&gt;
But once it clicks, it’s actually &lt;strong&gt;simpler and more powerful&lt;/strong&gt;.&lt;br&gt;
We're basically writing backend logic &lt;em&gt;inside the frontend project&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 55 of #100DayOfCode — Data Fetching and Caching in NextJS</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Sat, 28 Mar 2026 20:24:31 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-55-of-100dayofcode-data-fetching-and-caching-in-nextjs-2mk8</link>
      <guid>https://forem.com/m_saad_ahmad/day-55-of-100dayofcode-data-fetching-and-caching-in-nextjs-2mk8</guid>
      <description>&lt;p&gt;Data fetching with React follows the same well-known drill: &lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useState&lt;/code&gt;, loading spinners, and API keys being dangerously exposed on the frontend. Next.js changes all of that. With its built-in server-side data fetching and automatic caching system, Next.js makes it possible to build fast, secure, full-stack applications without reaching for a dozen extra libraries.&lt;/p&gt;

&lt;p&gt;For day 55, the goal was to understand how data fetching works in Next.js, how it compares to the React way, and how to use the different caching strategies to make the app as fast and efficient as possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Data Fetching: React vs Next.js
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How React Fetches Data
&lt;/h3&gt;

&lt;p&gt;In React, data fetching happens entirely in the browser. When a user visits a page, the browser first downloads the HTML (which is mostly empty), then runs your JavaScript bundle, and only &lt;em&gt;then&lt;/em&gt; fires off an API request to fetch the data. This means the user always sees a loading state before the actual content appears.&lt;/p&gt;

&lt;p&gt;Here is what that typically looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&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;react&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;Posts&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;posts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPosts&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="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;https://api.example.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loading&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;posts&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;post&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;This works, but it comes with real problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Loading spinners everywhere&lt;/strong&gt; —&amp;gt; users see an empty page or spinner before any content appears.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple round trips&lt;/strong&gt; —&amp;gt; the browser loads the page, then makes a separate API request, adding latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exposed API keys&lt;/strong&gt; —&amp;gt; any secret keys used in &lt;code&gt;fetch&lt;/code&gt; calls are visible in the browser's DevTools Network tab.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No built-in caching&lt;/strong&gt; —&amp;gt; every render triggers a fresh API call unless you manually set up something like React Query or SWR.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SEO suffers&lt;/strong&gt; —&amp;gt; search engines crawl the empty HTML before JavaScript runs, so they may not see your content at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Next.js Fetches Data
&lt;/h3&gt;

&lt;p&gt;Next.js introduces &lt;strong&gt;Server Components&lt;/strong&gt;; components that run on the server before the HTML is ever sent to the browser. Because they run on the server, you can make them &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; your data at the top level. No &lt;code&gt;useEffect&lt;/code&gt;, no &lt;code&gt;useState&lt;/code&gt;, no loading spinner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Posts&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="nx"&gt;res&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;https://api.example.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&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;res&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;posts&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;post&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;That is the entire component. The server fetches the data, builds the HTML with the data already inside it, and sends the finished page to the browser. The user never sees a loading state because by the time the page arrives, the data is already there.&lt;/p&gt;

&lt;p&gt;This approach is better for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No loading spinners&lt;/strong&gt; —&amp;gt; data arrives with the page, not after it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better performance&lt;/strong&gt; —&amp;gt; one round trip instead of two.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API keys stay hidden&lt;/strong&gt; —&amp;gt; the fetch call never reaches the browser, so secrets stay on the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better SEO&lt;/strong&gt; —&amp;gt; search engines receive fully rendered HTML with real content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built-in caching&lt;/strong&gt; —&amp;gt; Next.js automatically caches fetch responses on the server (more on this below).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;fetch&lt;/code&gt; syntax is the same as in React or plain JavaScript. The only difference is where it executes, on the server, and the automatic handling by Next.js.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Comparison of Data Fetching between React/JS vs Next.js
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flhvhn001wawru6j0cdl8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flhvhn001wawru6j0cdl8.png" alt="Date Fetching React vs Next.js" width="800" height="658"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Caching in Next.js?
&lt;/h2&gt;

&lt;p&gt;Caching means storing the result of an API call so that the next time the same data is needed, it can be served instantly from the stored copy instead of making another network request. Without caching, every user visiting your page triggers a fresh API call, which is slow and expensive, especially if thousands of users are hitting the same endpoint.&lt;/p&gt;

&lt;p&gt;Next.js extends the native &lt;code&gt;fetch&lt;/code&gt; API with its own caching layer baked in. You control the caching behavior by passing options to your &lt;code&gt;fetch&lt;/code&gt; call.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important version note:&lt;/strong&gt; In Next.js 13 and 14, caching was &lt;strong&gt;ON by default&lt;/strong&gt; and you had to opt out. In Next.js 15, the default was &lt;strong&gt;flipped&lt;/strong&gt;, caching is now OFF by default, and you have to opt in. Always check your version before assuming the default behavior.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Types of Caching in Next.js
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Cache Forever (force-cache)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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;https://api.example.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;force-cache&lt;/span&gt;&lt;span class="dl"&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 fetches the data once and caches it indefinitely. Subsequent requests for the same data are served instantly from the cache without hitting the API again. This is the most aggressive caching strategy and gives you the fastest possible response times.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. No Cache (no-store)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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;https://api.example.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-store&lt;/span&gt;&lt;span class="dl"&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 completely disables caching. Every request hits the API fresh and gets the latest data. This is the slowest option but guarantees you always have up-to-date information.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Revalidate Every N Seconds (ISR)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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;https://api.example.com/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;revalidate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&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 the sweet spot for most applications. It caches the response but automatically refreshes it every 60 seconds in the background. Users always get a fast cached response, but the data does not go stale forever. This is called &lt;strong&gt;Incremental Static Regeneration (ISR)&lt;/strong&gt; and it uses a stale-while-revalidate strategy, serve the cached version immediately, then quietly fetch a fresh copy in the background for the next request.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Revalidate On Demand
&lt;/h4&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;revalidatePath&lt;/span&gt; &lt;span class="p"&gt;}&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/cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Call this function whenever your data changes&lt;/span&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;updatePost&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ... update logic&lt;/span&gt;

  &lt;span class="nf"&gt;revalidatePath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// tells Next.js to rebuild this page&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of refreshing on a time interval, you manually tell Next.js when the cached data is stale. This is useful when you know exactly when data changes — for example, when a user submits a form, a CMS editor publishes new content, or a webhook fires from an external service. The page is rebuilt on the next request after &lt;code&gt;revalidatePath&lt;/code&gt; is called.&lt;/p&gt;




&lt;h2&gt;
  
  
  Which Caching Strategy Should Be Used?
&lt;/h2&gt;

&lt;p&gt;Choosing the right caching strategy depends entirely on how often the fetched data in the app changes and how critical it is that users see the latest version. Here is a practical breakdown:&lt;/p&gt;

&lt;h3&gt;
  
  
  Use &lt;code&gt;force-cache&lt;/code&gt; for content that rarely changes
&lt;/h3&gt;

&lt;p&gt;Think documentation pages, marketing landing pages, legal pages, blog posts that are rarely edited, or any content that was written once and stays the same. Since this data does not change, there is no reason to ever hit the API again after the first fetch. &lt;code&gt;force-cache&lt;/code&gt; gives you maximum speed with zero cost.&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;const&lt;/span&gt; &lt;span class="nx"&gt;res&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;https://api.example.com/docs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;force-cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use &lt;code&gt;revalidate&lt;/code&gt; (ISR) for content that changes occasionally
&lt;/h3&gt;

&lt;p&gt;This is the right choice for the majority of real-world pages: e-commerce product listings where prices update periodically, news articles, blog indexes, weather data for a specific city, or sports standings. The data changes, but not so frequently that a slightly stale version causes real problems. A revalidation interval of 60 seconds to an hour is usually appropriate, depending on how time-sensitive the data is.&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="c1"&gt;// Refresh every 10 minutes — good for product listings&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&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;https://api.example.com/products&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;revalidate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;600&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;h3&gt;
  
  
  Use &lt;code&gt;revalidatePath&lt;/code&gt; for content driven by user actions or a CMS
&lt;/h3&gt;

&lt;p&gt;When you are building a blog or CMS-backed site and an editor publishes a new post, you want that page to update immediately, not in 10 minutes. &lt;code&gt;revalidatePath&lt;/code&gt; lets you trigger a rebuild the moment the content changes. The same applies to e-commerce admin panels, user-generated content, or any situation where a specific action is the trigger for stale data.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;revalidatePath&lt;/span&gt; &lt;span class="p"&gt;}&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/cache&lt;/span&gt;&lt;span class="dl"&gt;'&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;publishPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;published&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nf"&gt;revalidatePath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/blog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// immediately mark the blog listing as stale&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use &lt;code&gt;no-store&lt;/code&gt; for data that must always be live
&lt;/h3&gt;

&lt;p&gt;User-specific data like dashboards, account settings, or notification counts must never be cached; each user needs their own fresh data. The same goes for live stock prices, real-time sports scores, or anything where a stale response would cause real user confusion or financial consequences.&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;const&lt;/span&gt; &lt;span class="nx"&gt;res&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="s2"&gt;`https://api.example.com/user/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/dashboard`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;no-store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Quick Reference
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data type&lt;/th&gt;
&lt;th&gt;Strategy&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Static content, never changes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;force-cache&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Docs, legal pages, about page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Changes occasionally&lt;/td&gt;
&lt;td&gt;&lt;code&gt;revalidate: 3600&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blog posts, product listings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Changes frequently&lt;/td&gt;
&lt;td&gt;&lt;code&gt;revalidate: 60&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;News feed, prices, scores&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Changes unpredictably&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;revalidatePath&lt;/code&gt; on demand&lt;/td&gt;
&lt;td&gt;CMS content, user-published posts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Must always be live&lt;/td&gt;
&lt;td&gt;&lt;code&gt;no-store&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;User dashboard, live data, notifications&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Data fetching and caching in Next.js simplify web application development. Instead of complex client-side fetching with &lt;code&gt;useEffect&lt;/code&gt;, Next.js manages loading states, API key security, and caching out of the box.&lt;/p&gt;

&lt;p&gt;With Server Components, you can use &lt;code&gt;async/await&lt;/code&gt; data fetching directly in your components with minimal boilerplate. The built-in caching system allows for fine-grained control over data freshness in your &lt;code&gt;fetch&lt;/code&gt; calls. These features enable you to build fast, secure, and SEO-friendly full-stack applications in Next.js while maintaining a great developer experience.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 54 of #100DaysOfCode — Creating Blog App</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Sat, 28 Mar 2026 05:52:15 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-54-of-100daysofcode-creating-blog-app-gn3</link>
      <guid>https://forem.com/m_saad_ahmad/day-54-of-100daysofcode-creating-blog-app-gn3</guid>
      <description>&lt;p&gt;On day 54, my goal was to create a basic blog app using Next.js, applying the concepts I had learned so far. I focused on the fundamentals of Next.js, including how it operates, the differences between server and client components, the &lt;code&gt;Link&lt;/code&gt; component, and App Router routing. This blog app served as a practical way to integrate all these concepts into a single real project, rather than working through isolated examples.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Built
&lt;/h2&gt;

&lt;p&gt;A simple blog app using Next.js App Router. It has a home page that lists all posts, a dedicated blog section for reading individual posts, and separate pages for About and Contact, all wrapped in a shared Header and Footer layout.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;🛠️ Concepts Used&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next.js App Router &amp;amp; file-based routing&lt;/li&gt;
&lt;li&gt;Root layout with shared Header &amp;amp; Footer&lt;/li&gt;
&lt;li&gt;Server-side vs client-side components&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Link&lt;/code&gt; component for navigation&lt;/li&gt;
&lt;li&gt;Dynamic routing with &lt;code&gt;[slug]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Folder Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/
├── layout.jsx              --&amp;gt; Root layout (with Header and Footer)
├── page.jsx                --&amp;gt; Home page — lists all posts
├── about/
│   └── page.jsx            --&amp;gt; About me page
├── contact/
│   └── page.jsx            --&amp;gt; Contact page
├── blog/
│   ├── page.jsx            --&amp;gt; All posts listing
│   └── [slug]/
│       ├── not-found.jsx
│       └── page.jsx        --&amp;gt; Individual post (dynamic route)
│
├── data/
│    └── posts.js           --&amp;gt; Post data
│
│
├── components/
│       ├── Header.jsx      --&amp;gt; Header component
│       └── Footer.jsx      --&amp;gt; Footer component
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The root &lt;code&gt;layout.jsx&lt;/code&gt; wraps the entire app with a shared &lt;code&gt;Header&lt;/code&gt; and &lt;code&gt;Footer&lt;/code&gt; from the &lt;code&gt;components/&lt;/code&gt; directory. The home page (&lt;code&gt;app/page.jsx&lt;/code&gt;) lists all posts, while dedicated routes exist for &lt;code&gt;/about&lt;/code&gt; and &lt;code&gt;/contact&lt;/code&gt;. The &lt;code&gt;/blog&lt;/code&gt; route has its own listing page, and dynamic post pages are handled via the &lt;code&gt;[slug]&lt;/code&gt; segment, where each post is rendered based on its URL slug, with a &lt;code&gt;not-found.jsx&lt;/code&gt; as a fallback for invalid slugs.&lt;/p&gt;




&lt;h3&gt;
  
  
  The Interesting Part: &lt;code&gt;[slug]&lt;/code&gt; Dynamic Routing
&lt;/h3&gt;

&lt;p&gt;The thing that actually made sense to me through building this was how Next.js handles dynamic routes. Instead of creating a separate page file for every blog post, you create a single folder named &lt;code&gt;[slug]&lt;/code&gt; and Next.js does the rest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;blog/
└── [slug]/
    ├── page.jsx        --&amp;gt; renders the individual post
    └── not-found.jsx   --&amp;gt; handles invalid slugs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define all the posts inside the &lt;code&gt;posts.js&lt;/code&gt; file and import it in &lt;code&gt;page.jsx&lt;/code&gt; file. Inside &lt;code&gt;page.jsx&lt;/code&gt;, you get the slug from &lt;code&gt;params&lt;/code&gt; and use it to fetch or find the right post:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;BlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;params&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;slug&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// use slug to find and display the correct post&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if someone visits a slug that doesn't exist, &lt;code&gt;not-found.jsx&lt;/code&gt; kicks in automatically. Clean and simple.&lt;/p&gt;




&lt;h3&gt;
  
  
  What I Struggled With
&lt;/h3&gt;

&lt;p&gt;Understanding when the &lt;strong&gt;&lt;code&gt;not-found.jsx&lt;/code&gt;&lt;/strong&gt; file gets triggered took me some time. I initially thought I needed to handle redirects manually, but Next.js takes care of it for you as long as the file is correctly placed within the &lt;code&gt;[slug]&lt;/code&gt; folder.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Link&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/saad299/100-Days-of-Code/tree/main/Days_51-60/nextapp" rel="noopener noreferrer"&gt;GitHub Repo Link&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Building this blog app was a solid checkpoint in my 100 Days of Code journey. It's one thing to understand routing or layouts in theory, but actually wiring them together into a real project makes everything stick differently.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 53 of #100DaysOfCode — Routing (App Router)</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Fri, 27 Mar 2026 06:13:19 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-53-of-100daysofcode-routing-app-router-1f57</link>
      <guid>https://forem.com/m_saad_ahmad/day-53-of-100daysofcode-routing-app-router-1f57</guid>
      <description>&lt;p&gt;Routing is one of the most fundamental concepts in any web application. It determines how users move between different pages and how those pages are structured. In Next.js, routing has evolved significantly with the introduction of the &lt;strong&gt;App Router&lt;/strong&gt;, which uses a file-based system to make navigation more intuitive, scalable, and powerful, all without needing external libraries.&lt;/p&gt;

&lt;p&gt;Today, for day 53, the goal was to understand routing in Next.js&lt;/p&gt;




&lt;h2&gt;
  
  
  App Router Mental Model
&lt;/h2&gt;

&lt;p&gt;In Next.js App Router:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Folders = Routes&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Files = UI + Behavior&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;👉 Your project structure defines &lt;strong&gt;your entire routing system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;No need for separate route configuration like in traditional React apps.&lt;/p&gt;




&lt;h2&gt;
  
  
  Core File Conventions (Built-in Superpowers)
&lt;/h2&gt;

&lt;p&gt;Next.js gives you powerful features just by naming files correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;page.js&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Defines a route&lt;/li&gt;
&lt;li&gt;Required to make a route accessible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;app/about/page.js → /about
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;layout.js&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Shared UI (Navbar, Footer, Sidebar)&lt;/li&gt;
&lt;li&gt;Wraps all child routes automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Layouts can be &lt;strong&gt;nested&lt;/strong&gt;, which is extremely powerful for dashboards and apps.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;loading.js&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Shows loading UI while data is being fetched&lt;/li&gt;
&lt;li&gt;Works with React Suspense under the hood&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;error.js&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Handles runtime errors in that route segment&lt;/li&gt;
&lt;li&gt;Must be a &lt;strong&gt;Client Component&lt;/strong&gt; (&lt;code&gt;"use client"&lt;/code&gt; required)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;not-found.js&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Custom 404 UI for that route segment&lt;/li&gt;
&lt;li&gt;Triggered using &lt;code&gt;notFound()&lt;/code&gt; from Next.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Key Insight:&lt;/strong&gt;&lt;br&gt;
All of this comes &lt;strong&gt;without installing any extra libraries&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Nested Routing (Zero Config)
&lt;/h2&gt;

&lt;p&gt;Routing is automatically created from folders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;app/
  dashboard/
    page.js        → /dashboard
    settings/
      page.js      → /dashboard/settings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 No need for route definitions like in React Router.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dynamic Routes
&lt;/h2&gt;

&lt;p&gt;Dynamic routing is super clean in App Router using square brackets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a dynamic route:&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;app/blog/[slug]/page.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Access params:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;Page&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;params&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slug&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;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 replaces React Router params.&lt;/p&gt;

&lt;h3&gt;
  
  
  ⚠️ Important Update (Accuracy Fix)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;Server Components&lt;/strong&gt; → use &lt;code&gt;params&lt;/code&gt; (as shown above)&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;Client Components&lt;/strong&gt; → use:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useParams&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/navigation&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Route Groups
&lt;/h2&gt;

&lt;p&gt;Route groups help organize your code without affecting URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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;app/
  (auth)/
    login/page.js
    register/page.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 URLs will still be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/login
/register
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Navigation Hooks
&lt;/h2&gt;

&lt;p&gt;These hooks come from:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;usePathname&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;next/navigation&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;useRouter()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Used for programmatic navigation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/dashboard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;code&gt;usePathname()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Returns the current route path&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;⚠️ Important&lt;/p&gt;

&lt;p&gt;👉 These hooks only work in &lt;strong&gt;Client Components&lt;/strong&gt; (&lt;code&gt;"use client"&lt;/code&gt; required)&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Layout Nesting
&lt;/h2&gt;

&lt;p&gt;App Router allows multiple layout levels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Root Layout → &lt;code&gt;app/layout.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Section Layout → &lt;code&gt;app/dashboard/layout.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Nested Layouts → deeper levels&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/dashboard&lt;/code&gt; → has its own layout&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/dashboard/settings&lt;/code&gt; → &lt;strong&gt;inherits dashboard layout automatically&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 This is perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dashboards&lt;/li&gt;
&lt;li&gt;Admin panels&lt;/li&gt;
&lt;li&gt;Complex apps&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Takeaways (Quick Recap)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;File system = routing system&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;page.js&lt;/code&gt; creates routes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;layout.js&lt;/code&gt; enables shared UI&lt;/li&gt;
&lt;li&gt;Nested routing works automatically&lt;/li&gt;
&lt;li&gt;Dynamic routes use &lt;code&gt;[param]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Route groups organize without changing URLs&lt;/li&gt;
&lt;li&gt;Navigation hooks require Client Components&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The Next.js App Router simplifies routing by turning your folder structure into a powerful system. Once you understand the core conventions, building scalable and well-structured applications becomes much easier.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Folders = Routes, Files = Behavior”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 52 of #100DayOfCode — Script, Link &amp; Image components in Next.js</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Thu, 26 Mar 2026 04:59:30 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-52-of-100dayofcode-script-link-image-components-in-nextjs-5fe</link>
      <guid>https://forem.com/m_saad_ahmad/day-52-of-100dayofcode-script-link-image-components-in-nextjs-5fe</guid>
      <description>&lt;p&gt;If you're building modern web applications with &lt;strong&gt;Next.js&lt;/strong&gt;, you've probably come across three powerful built-in components: &lt;strong&gt;Link&lt;/strong&gt;, &lt;strong&gt;Script&lt;/strong&gt;, and &lt;strong&gt;Image&lt;/strong&gt;. These components are designed to improve performance, optimize user experience, and simplify common tasks like navigation, third-party script loading, and image optimization. Unlike traditional HTML elements, these components are deeply integrated with Next.js features such as routing, lazy loading, and performance optimization, making them essential tools for any developer working in the Next.js ecosystem.&lt;/p&gt;

&lt;p&gt;The goal for day 52 was to understand the Link, Script, and Image components in Next.js and their use.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔗 The Link Component in Next.js
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Link component&lt;/strong&gt; in Next.js is used for &lt;strong&gt;client-side navigation&lt;/strong&gt; between pages. Instead of using traditional &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags, Next.js encourages developers to use &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; because it enables &lt;strong&gt;faster page transitions&lt;/strong&gt;, &lt;strong&gt;prefetching&lt;/strong&gt;, and avoids full page reloads.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; instead of &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client-side routing&lt;/strong&gt; for faster navigation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No full page reloads&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic prefetching&lt;/strong&gt; of linked pages&lt;/li&gt;
&lt;li&gt;Improved performance and UX&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  💻 Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Link&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/link&lt;/span&gt;&lt;span class="dl"&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="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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome to My App&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/about"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Go to About Page
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;👉 Using &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; ensures your app feels like a &lt;strong&gt;single-page application (SPA)&lt;/strong&gt; while still benefiting from server-side rendering.&lt;/p&gt;




&lt;h2&gt;
  
  
  📜 The Script Component in Next.js
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Script component&lt;/strong&gt; in Next.js allows you to &lt;strong&gt;load and manage third-party scripts&lt;/strong&gt; efficiently. It gives you fine-grained control over when and how scripts are loaded, improving performance and avoiding render-blocking issues.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Note: Unlike &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; in HTML, Next.js &lt;code&gt;&amp;lt;Script&amp;gt;&lt;/code&gt; optimizes loading strategies.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Why use &lt;code&gt;&amp;lt;Script&amp;gt;&lt;/code&gt; instead of traditional script tags?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Control loading strategies (&lt;code&gt;beforeInteractive&lt;/code&gt;, &lt;code&gt;afterInteractive&lt;/code&gt;, &lt;code&gt;lazyOnload&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Prevent render-blocking&lt;/li&gt;
&lt;li&gt;Optimize performance and Core Web Vitals&lt;/li&gt;
&lt;li&gt;Better handling of third-party scripts&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  💻 Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Script&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/script&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Analytics&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="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Script&lt;/span&gt;
        &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://example.com/analytics.js"&lt;/span&gt;
        &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"afterInteractive"&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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;👉 This ensures scripts load &lt;strong&gt;only when needed&lt;/strong&gt;, improving your app’s speed and performance.&lt;/p&gt;




&lt;h2&gt;
  
  
  🖼️ The Image Component in Next.js
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Image component&lt;/strong&gt; is one of the most powerful features of Next.js. It provides &lt;strong&gt;automatic image optimization&lt;/strong&gt;, including resizing, lazy loading, and modern format delivery (like WebP).&lt;/p&gt;

&lt;h3&gt;
  
  
  What makes &lt;code&gt;&amp;lt;Image&amp;gt;&lt;/code&gt; special?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Automatic image optimization&lt;/li&gt;
&lt;li&gt;Lazy loading by default&lt;/li&gt;
&lt;li&gt;Responsive images&lt;/li&gt;
&lt;li&gt;Supports modern formats&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  💻 Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Profile&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My Profile&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Image&lt;/span&gt;
        &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/profile.jpg"&lt;/span&gt;
        &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Profile Picture"&lt;/span&gt;
        &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;👉 Using &lt;code&gt;&amp;lt;Image&amp;gt;&lt;/code&gt; helps improve &lt;strong&gt;Core Web Vitals&lt;/strong&gt;, especially &lt;strong&gt;Largest Contentful Paint (LCP)&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding and using &lt;strong&gt;Link, Script, and Image components in Next.js&lt;/strong&gt; is crucial for building &lt;strong&gt;high-performance, SEO-friendly web applications&lt;/strong&gt;. The &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt; component enhances navigation speed, &lt;code&gt;&amp;lt;Script&amp;gt;&lt;/code&gt; optimizes third-party script loading, and &lt;code&gt;&amp;lt;Image&amp;gt;&lt;/code&gt; ensures efficient image delivery. Together, they help developers create fast, scalable, and user-friendly applications.&lt;/p&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 51 0f #100DaysOfCode — Server Components vs Client Components</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Wed, 25 Mar 2026 05:04:23 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-51-0f-100daysofcode-server-components-vs-client-components-1p3h</link>
      <guid>https://forem.com/m_saad_ahmad/day-51-0f-100daysofcode-server-components-vs-client-components-1p3h</guid>
      <description>&lt;p&gt;Modern web applications require both performance and interactivity, with users expecting fast page loads and dynamic experiences. Traditionally, developers faced a trade-off between server-side rendering (SSR) for speed and client-side rendering (CSR) for interactivity, leading to complexity.&lt;br&gt;
Next.js addresses this by introducing Server Components and Client Components, enabling a smart division of responsibilities between the server and the browser. This approach is the reason Next.js offers both server and client components.&lt;/p&gt;

&lt;p&gt;For today, day 51, the goal was to understand what server and client components are in Next.js, what the difference is between them, and why they even exist.&lt;/p&gt;


&lt;h2&gt;
  
  
  What is a Server Component?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Server Component&lt;/strong&gt; in Next.js is a React component that runs &lt;strong&gt;only on the server&lt;/strong&gt;. It never gets shipped to the browser, meaning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smaller JavaScript bundle&lt;/li&gt;
&lt;li&gt;Better performance&lt;/li&gt;
&lt;li&gt;Direct access to backend resources (databases, APIs, secrets)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Example of a Server Component
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/page.jsx&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;getData&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="nx"&gt;res&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;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&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="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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Page&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="nx"&gt;data&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;getData&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Server Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;👉 &lt;strong&gt;This is how you normally write Next.js code, and this, by default, is a server component.&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  ⚙️ Why are Components Server Components by Default?
&lt;/h2&gt;

&lt;p&gt;Next.js defaults to &lt;strong&gt;Server Components&lt;/strong&gt; because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance-first approach&lt;/strong&gt;: Less JavaScript sent to the client&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Sensitive logic stays on the server&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smaller bundles&lt;/strong&gt;: Faster load times&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better SEO&lt;/strong&gt;: Content is pre-rendered on the server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By making server components the default, Next.js encourages developers to build faster and more efficient applications without extra effort.&lt;/p&gt;


&lt;h2&gt;
  
  
  💻 What is a Client Component?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Client Component&lt;/strong&gt; is a component that runs in the &lt;strong&gt;browser&lt;/strong&gt;. It is required when you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State (&lt;code&gt;useState&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Effects (&lt;code&gt;useEffect&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Event handlers (&lt;code&gt;onClick&lt;/code&gt;, &lt;code&gt;onChange&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Browser APIs (like &lt;code&gt;localStorage&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To define a Client Component, you must explicitly add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example of a Client Component
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Client Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Increment
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;By adding &lt;code&gt;"use client"&lt;/code&gt; above the codes, the default server component now becomes a client component&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 Why Do Client Components Exist?
&lt;/h2&gt;

&lt;p&gt;If Server Components are so powerful, why do we even need Client Components?&lt;/p&gt;

&lt;p&gt;Because not everything can happen on the server.&lt;/p&gt;

&lt;p&gt;Client Components exist for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User Interactions&lt;/strong&gt; (clicks, typing, animations)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Real-time updates&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic UI behavior&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Browser-only APIs&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without Client Components, your app would be fast—but static and non-interactive.&lt;/p&gt;




&lt;h2&gt;
  
  
  Server vs Client Components (Side-by-Side Comparison)
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Server Component 🖥️&lt;/th&gt;
&lt;th&gt;Client Component 🌐&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Runs on&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;Browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default in Next.js&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JavaScript sent&lt;/td&gt;
&lt;td&gt;❌ Minimal&lt;/td&gt;
&lt;td&gt;✅ Included&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interactivity&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access to DB/API&lt;/td&gt;
&lt;td&gt;✅ Direct&lt;/td&gt;
&lt;td&gt;❌ Via API calls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;useState/useEffect&lt;/td&gt;
&lt;td&gt;❌ Not allowed&lt;/td&gt;
&lt;td&gt;✅ Allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SEO Friendly&lt;/td&gt;
&lt;td&gt;✅ Excellent&lt;/td&gt;
&lt;td&gt;⚠️ Depends&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;🚀 Faster&lt;/td&gt;
&lt;td&gt;⚠️ Heavier&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  When to Use Server vs Client Components
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use &lt;strong&gt;Server Components&lt;/strong&gt; when:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fetching data from a database or API&lt;/li&gt;
&lt;li&gt;Rendering static or semi-static content&lt;/li&gt;
&lt;li&gt;SEO is important&lt;/li&gt;
&lt;li&gt;You want better performance and smaller bundles&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use &lt;strong&gt;Client Components&lt;/strong&gt; when:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need interactivity (buttons, forms, UI events)&lt;/li&gt;
&lt;li&gt;You use React hooks (&lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useEffect&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;You rely on browser APIs&lt;/li&gt;
&lt;li&gt;You’re building dynamic UI (modals, dropdowns, animations)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Pro Tip:&lt;br&gt;
Keep components as &lt;strong&gt;Server Components by default&lt;/strong&gt;, and only convert to Client Components &lt;strong&gt;when absolutely necessary&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The introduction of &lt;strong&gt;Server and Client Components in Next.js&lt;/strong&gt; is a game-changer for modern web development. It allows developers to build applications that are both &lt;strong&gt;highly performant and interactive&lt;/strong&gt;, without compromising one for the other.&lt;/p&gt;

&lt;p&gt;By understanding when to use each type, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimize performance&lt;/li&gt;
&lt;li&gt;Improve SEO&lt;/li&gt;
&lt;li&gt;Deliver better user experiences&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 50 #100DaysOfCode — Introduction to Next.js</title>
      <dc:creator>M Saad Ahmad</dc:creator>
      <pubDate>Tue, 24 Mar 2026 06:05:40 +0000</pubDate>
      <link>https://forem.com/m_saad_ahmad/day-50-100daysofcode-introduction-to-nextjs-22k5</link>
      <guid>https://forem.com/m_saad_ahmad/day-50-100daysofcode-introduction-to-nextjs-22k5</guid>
      <description>&lt;p&gt;Over the past days of my 100 Days of Code challenge, I learned how to build UI components with React, manage state, and handle user interactions on the frontend. On the backend side, I worked with Node.js and Express.js to build APIs, connected them to databases, and understood how data flows between the client and the server. But as I kept building, one thing became obvious — maintaining a separate frontend and backend project, manually setting up routing with React Router, and figuring out how to make pages SEO-friendly was adding a lot of overhead to every project.&lt;/p&gt;

&lt;p&gt;This is where Next.js changes the game.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Next.js
&lt;/h2&gt;

&lt;p&gt;Next.js is a &lt;strong&gt;React framework&lt;/strong&gt; that helps you build:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster apps&lt;/li&gt;
&lt;li&gt;SEO-friendly apps&lt;/li&gt;
&lt;li&gt;Full-stack applications&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key features Next.js adds to React:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;File-based routing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Your folder structure &lt;em&gt;is&lt;/em&gt; your routing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Pages are rendered on the server per request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Static Site Generation (SSG)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Pages are pre-built at deploy time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;API Routes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Write backend endpoints inside your frontend project&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Why Next.js Exists
&lt;/h2&gt;

&lt;p&gt;Plain React is a UI library. It's great at building interfaces, but it doesn't tell you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to structure your project&lt;/li&gt;
&lt;li&gt;How to handle routing&lt;/li&gt;
&lt;li&gt;How to render things on the server&lt;/li&gt;
&lt;li&gt;How to write backend logic alongside your frontend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's where &lt;strong&gt;frameworks exist&lt;/strong&gt; — and Next.js fills all those gaps for React.&lt;br&gt;
Next.js solves these problems by giving you a &lt;strong&gt;full-stack framework on top of React&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  React SPA vs Next.js
&lt;/h2&gt;
&lt;h3&gt;
  
  
  React SPA vs Next.js: What's the Real Difference?
&lt;/h3&gt;

&lt;p&gt;A plain React app is a &lt;strong&gt;Single Page Application (SPA)&lt;/strong&gt;. The browser downloads one HTML file, then JavaScript takes over and renders everything client-side.&lt;/p&gt;

&lt;p&gt;Next.js changes this model:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;React SPA&lt;/th&gt;
&lt;th&gt;Next.js&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rendering&lt;/td&gt;
&lt;td&gt;Client-side only&lt;/td&gt;
&lt;td&gt;Server + Client&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SEO&lt;/td&gt;
&lt;td&gt;Poor (JS-heavy)&lt;/td&gt;
&lt;td&gt;Better (HTML sent from server)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Routing&lt;/td&gt;
&lt;td&gt;Manual (React Router)&lt;/td&gt;
&lt;td&gt;Built-in (file-based)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend&lt;/td&gt;
&lt;td&gt;Separate project&lt;/td&gt;
&lt;td&gt;API routes built-in&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Depends on bundle size&lt;/td&gt;
&lt;td&gt;Optimized by default&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is a &lt;strong&gt;mindset shift&lt;/strong&gt;, not just a syntax change.&lt;/p&gt;


&lt;h2&gt;
  
  
  Pages Router vs App Router
&lt;/h2&gt;

&lt;p&gt;Next.js has two routing systems. You'll likely encounter both, but the &lt;strong&gt;App Router is the modern, recommended approach&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Pages Router (&lt;code&gt;pages/&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;App Router (&lt;code&gt;app/&lt;/code&gt;)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Introduced&lt;/td&gt;
&lt;td&gt;Original Next.js&lt;/td&gt;
&lt;td&gt;Next.js 13+&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Status&lt;/td&gt;
&lt;td&gt;Still supported&lt;/td&gt;
&lt;td&gt;Recommended going forward&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default component type&lt;/td&gt;
&lt;td&gt;Client Components&lt;/td&gt;
&lt;td&gt;Server Components&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;👉 &lt;strong&gt;Use App Router for any new project.&lt;/strong&gt; The Pages Router still works and is still widely used in older codebases, so it's worth knowing it exists.&lt;/p&gt;


&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;When you create a new Next.js app, here's what the key files and folders mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-app/
├── app/
│   ├── layout.js       ← Wraps all pages (like a shell/template)
│   ├── page.js         ← The "/" homepage route
│   └── globals.css     ← Global styles
├── public/             ← Static assets (images, fonts)
├── next.config.js      ← Next.js configuration
└── package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;app/&lt;/code&gt; folder is the App Router. Everything inside it is a route, a layout, or a component.&lt;/p&gt;




&lt;h2&gt;
  
  
  File-Based Routing: One of the Biggest Shifts
&lt;/h2&gt;

&lt;p&gt;This is one of the most satisfying things about Next.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You don't configure routes. You create folders.&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;app/
  page.js              → "/"
  about/
    page.js            → "/about"
  blog/
    page.js            → "/blog"
    [slug]/
      page.js          → "/blog/any-post-name"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nested routes
&lt;/h3&gt;

&lt;p&gt;Just nest folders. &lt;code&gt;app/dashboard/settings/page.js&lt;/code&gt; gives you &lt;code&gt;/dashboard/settings&lt;/code&gt;. That's it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic routes
&lt;/h3&gt;

&lt;p&gt;Wrap a folder name in square brackets:&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="nx"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;/&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="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;/blog/hello-world&lt;/code&gt;, &lt;code&gt;/blog/my-first-post&lt;/code&gt; — all route to the same component. The &lt;code&gt;slug&lt;/code&gt; is passed as a prop.&lt;/p&gt;

&lt;p&gt;If you've used React Router before, this replaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Old React Router way&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/blog/:slug"&lt;/span&gt; &lt;span class="na"&gt;element&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With just... a folder.&lt;/p&gt;




&lt;h2&gt;
  
  
  Server Components vs Client Components
&lt;/h2&gt;

&lt;p&gt;This is the &lt;strong&gt;most important concept in Next.js&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Components (Default)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Run on the &lt;strong&gt;server&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Faster performance&lt;/li&gt;
&lt;li&gt;Smaller bundle size&lt;/li&gt;
&lt;li&gt;Can directly fetch data&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Client Components
&lt;/h2&gt;

&lt;p&gt;Use this at the top:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Needed when you use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State (&lt;code&gt;useState&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Effects (&lt;code&gt;useEffect&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Event handlers (onClick, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Rule of thumb:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Default = &lt;strong&gt;Server Component&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;"use client"&lt;/code&gt; only when needed&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Styling in Next.js
&lt;/h2&gt;

&lt;p&gt;Next.js supports multiple styling approaches out of the box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Global CSS&lt;/strong&gt; — &lt;code&gt;globals.css&lt;/code&gt;, applied app-wide via &lt;code&gt;layout.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CSS Modules&lt;/strong&gt; — &lt;code&gt;Button.module.css&lt;/code&gt;, scoped to a single component (no class name conflicts)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tailwind CSS&lt;/strong&gt; — Works great with Next.js, can be set up during project creation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you already know Tailwind basics, it integrates seamlessly.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠 Running &amp;amp; Building
&lt;/h2&gt;

&lt;p&gt;Basic commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npm run dev     → development
npm run build   → production build
npm run start   → run production server
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Important understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;dev&lt;/strong&gt; = fast, for development&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;build + start&lt;/strong&gt; = optimized, for production&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🌍 Why Next.js is Used in Real-World Projects
&lt;/h2&gt;

&lt;p&gt;Because it gives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SEO-ready&lt;/strong&gt;: Server rendering means search engines can read your content&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: Automatic code splitting, image optimization, caching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Full-stack in one project&lt;/strong&gt;: No need for a separate Express/Node backend for basic APIs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vercel deployment&lt;/strong&gt;: One-click deploy, zero config (Next.js is made by the Vercel team)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scales well&lt;/strong&gt;: Used by large companies like Vercel, TikTok, Twitch, and many others&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Companies don’t want to configure everything manually; they want a &lt;strong&gt;production-ready system&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;One-Line Summary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Next.js&lt;/td&gt;
&lt;td&gt;React + routing + SSR + API routes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;App Router&lt;/td&gt;
&lt;td&gt;Modern file-based routing in &lt;code&gt;app/&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server Component&lt;/td&gt;
&lt;td&gt;Renders on server, default in App Router&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Client Component&lt;/td&gt;
&lt;td&gt;Runs in browser, needs &lt;code&gt;"use client"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;File-based routing&lt;/td&gt;
&lt;td&gt;Folder = Route&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;npm run build&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates production-ready app&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;So to summarize, today was about understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why frameworks exist&lt;/li&gt;
&lt;li&gt;Why React alone isn’t always enough&lt;/li&gt;
&lt;li&gt;How Next.js changes the way we think about frontend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Biggest takeaway:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Next.js is not just a tool, it’s a &lt;strong&gt;different way of building web apps&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading. Feel free to share your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>nextjs</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
