<?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: shubham khatik</title>
    <description>The latest articles on Forem by shubham khatik (@shubhamkhatik).</description>
    <link>https://forem.com/shubhamkhatik</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%2F578138%2Fdddd1467-aef4-434d-b90f-b6266dda36a9.png</url>
      <title>Forem: shubham khatik</title>
      <link>https://forem.com/shubhamkhatik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shubhamkhatik"/>
    <language>en</language>
    <item>
      <title>Escaping the Buffer: The Advanced Guide to Streams in Node.js &amp; Next.js</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Sat, 14 Mar 2026 12:17:40 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/escaping-the-buffer-the-advanced-guide-to-streams-in-nodejs-nextjs-4734</link>
      <guid>https://forem.com/shubhamkhatik/escaping-the-buffer-the-advanced-guide-to-streams-in-nodejs-nextjs-4734</guid>
      <description>&lt;h1&gt;
  
  
  Escaping the Buffer: The Advanced Guide to Streams in Node.js &amp;amp; Next.js
&lt;/h1&gt;

&lt;p&gt;If you have been writing JavaScript for a few years, you are probably intimately familiar with &lt;code&gt;async/await&lt;/code&gt;. It makes asynchronous code look synchronous, keeps the event loop unblocked, and is universally loved.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;async/await&lt;/code&gt; hides a massive system design bottleneck: &lt;strong&gt;Buffering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you are building data-intensive applications — proxying AWS S3 uploads, generating massive CSVs from MongoDB, or rendering complex Next.js Server Components — relying solely on buffered data will inevitably lead to &lt;strong&gt;Out of Memory (OOM) crashes&lt;/strong&gt; and abysmal &lt;strong&gt;Time to First Byte (TTFB)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's break down the real-world problems buffering causes, understand the mental model of Streams, and look at how to properly architect streaming solutions across Node.js, Next.js, and databases.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Real Problem: Buffering vs. Streaming
&lt;/h2&gt;

&lt;p&gt;When you use standard &lt;code&gt;await&lt;/code&gt; to read a file or fetch an API, the underlying engine loads &lt;strong&gt;100% of that data into RAM&lt;/strong&gt; before your code executes the next line.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Backend Failure Mode (OOM Crashes)
&lt;/h3&gt;

&lt;p&gt;Imagine an Express route that downloads a 2GB video file for a user:&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;// 🚨 THE WRONG WAY: Buffering&lt;/span&gt;
&lt;span class="nx"&gt;app&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="s1"&gt;/download&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Node reads the ENTIRE 2GB file into RAM right now.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;videoData&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;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;promises&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./massive-video.mp4&lt;/span&gt;&lt;span class="dl"&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;videoData&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;If your AWS EC2 instance has 1GB of RAM, this code instantly crashes your Node process with &lt;code&gt;Fatal Error: heap out of memory&lt;/code&gt;. If you have 8GB of RAM, it only takes four concurrent users to kill your server.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Frontend Failure Mode (High TTFB)
&lt;/h3&gt;

&lt;p&gt;If your React frontend fetches a massive JSON payload, the browser downloads the entire payload, holds it in memory, and waits for the connection to close before parsing it. The user stares at a blank screen or a spinner until the very last byte arrives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The intended outcome of Streams&lt;/strong&gt; is to fix this by breaking data into manageable "chunks." You process chunk 1 while chunk 2 is downloading — keeping your server's memory footprint flat and delivering data to the frontend instantly.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The Mental Model: Two Colliding Worlds
&lt;/h2&gt;

&lt;p&gt;The most confusing part of modern JavaScript architecture is that there are &lt;strong&gt;two entirely different Stream APIs&lt;/strong&gt;. With the rise of the Next.js App Router and Edge computing, these two worlds are crashing into each other.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Node.js Streams (&lt;code&gt;node:stream&lt;/code&gt;)&lt;/strong&gt; — The backend heavyweights. These belong strictly to the Node runtime. They use &lt;code&gt;.pipe()&lt;/code&gt; and &lt;code&gt;.pipeline()&lt;/code&gt;. You use these for filesystem operations, heavy local data processing, and traditional Express &lt;code&gt;req&lt;/code&gt;/&lt;code&gt;res&lt;/code&gt; objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Web Streams API (&lt;code&gt;ReadableStream&lt;/code&gt;)&lt;/strong&gt; — The modern standard. Originally built for the browser (like the &lt;code&gt;fetch&lt;/code&gt; API response body), but now natively used by Next.js Edge Functions, Route Handlers, and Cloudflare Workers. They use &lt;code&gt;.pipeTo()&lt;/code&gt; and &lt;code&gt;.pipeThrough()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Mixing these up is the &lt;strong&gt;number one cause of bugs&lt;/strong&gt; when migrating an Express app to a Next.js App Router API.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  3. Practical Workflows &amp;amp; Real-World Execution
&lt;/h2&gt;

&lt;p&gt;Let's look at how Streams are actually used in real environments, evaluating the constraints and correct implementations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario A: Massive Database Exports (MongoDB / PostgreSQL)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt; You need to export 1 million user records to a CSV file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Constraint:&lt;/strong&gt; Running &lt;code&gt;await User.find({})&lt;/code&gt; will load 1 million objects into your Node server's RAM, crashing it instantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution (Node Streams):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both MongoDB and PostgreSQL offer native &lt;strong&gt;stream cursors&lt;/strong&gt;. Instead of fetching an array, you stream documents one by one, format them, and pipe them directly to the client.&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;// Express.js + MongoDB Example&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;pipeline&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;node:stream/promises&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;Transform&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;node:stream&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&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="s1"&gt;/export-users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="o"&gt;=&amp;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;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="s1"&gt;text/csv&lt;/span&gt;&lt;span class="dl"&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;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Disposition&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="s1"&gt;attachment; filename="users.csv"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 1. Create a database read stream&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cursorStream&lt;/span&gt; &lt;span class="o"&gt;=&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;find&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// 2. Transform each JSON document into a CSV row on the fly&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toCsvTransform&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Transform&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;objectMode&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;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;encoding&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&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;csvLine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;doc&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="s2"&gt;,&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;\n`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;csvLine&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="c1"&gt;// 3. pipeline() handles backpressure and cleans up memory if the user disconnects&lt;/span&gt;
  &lt;span class="k"&gt;try&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;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cursorStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toCsvTransform&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;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Stream pipeline failed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&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;h3&gt;
  
  
  Scenario B: AI Text Generation (The Web Streams API)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt; LLMs take several seconds to generate a response. Waiting for the full string ruins the UX.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution (Web Streams):&lt;/strong&gt; Read the &lt;code&gt;ReadableStream&lt;/code&gt; from the &lt;code&gt;fetch&lt;/code&gt; API chunk-by-chunk to create a real-time "typing" effect.&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;// React Client Component&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleAskAI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&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;/api/chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Get the Web Stream reader&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&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="nf"&gt;getReader&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;decoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextDecoder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;while &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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&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;reader&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;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Decode the raw bytes into text and update React state immediately&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunkText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;decoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setChatText&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;chunkText&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;h3&gt;
  
  
  Scenario C: Next.js App Router (Bridging the Gap)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The Problem:&lt;/strong&gt; You want to stream a large file from your server disk to the user, but Next.js Route Handlers expect a &lt;strong&gt;Web &lt;code&gt;ReadableStream&lt;/code&gt;&lt;/strong&gt; as the response — not a Node &lt;code&gt;fs.ReadStream&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution:&lt;/strong&gt; Convert the Node Stream to a Web Stream. In modern Node (v16+), there is a built-in utility for this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/app/api/download/route.ts&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;createReadStream&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;node:fs&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;Readable&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;node:stream&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;GET&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;nodeStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./heavy-asset.zip&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Convert Node.js Stream → Web Streams API ReadableStream&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Readable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toWeb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nodeStream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;webStream&lt;/span&gt;&lt;span class="p"&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="s1"&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="s1"&gt;application/zip&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="s1"&gt;Content-Disposition&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="s1"&gt;attachment; filename="asset.zip"&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;h2&gt;
  
  
  4. When NOT to Use Streams (Failure Modes)
&lt;/h2&gt;

&lt;p&gt;Streams add &lt;strong&gt;massive architectural complexity&lt;/strong&gt;. Error handling is notoriously difficult because a stream can fail halfway through — for example, the user loses internet connection while downloading.&lt;/p&gt;

&lt;p&gt;If you are fetching a standard JSON list of 50 items or doing simple CRUD operations, &lt;strong&gt;do not use streams&lt;/strong&gt;. Stick to standard &lt;code&gt;async/await&lt;/code&gt; and buffering.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✅ Use streams strictly as an accelerator when you hit the &lt;strong&gt;physical limits of your server's RAM&lt;/strong&gt;, or when network latency demands immediate partial rendering.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  5. The "Halfway" Failure Mode: Preventing Memory Leaks
&lt;/h2&gt;

&lt;p&gt;Streams are not atomic. A 1GB download might fail at 500MB because the user closed their laptop, the browser tab crashed, or the Wi-Fi dropped.&lt;/p&gt;

&lt;p&gt;If you do not handle this properly, the source stream (like a database cursor or a file read stream) will &lt;strong&gt;stay open forever&lt;/strong&gt;, waiting to send the rest of the data. This creates a silent memory leak that will eventually take down your Node.js instance.&lt;/p&gt;

&lt;p&gt;Here is how to handle halfway failures across different parts of the stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Node.js Backend: Never Use Raw &lt;code&gt;.pipe()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In older Express tutorials, you will constantly see this:&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;// 🚨 DANGEROUS: If 'res' closes early, 'fileStream' stays open forever.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fileStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./massive.mp4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;fileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Fix:&lt;/strong&gt; Always use &lt;code&gt;stream.pipeline&lt;/code&gt; (specifically the Promise-based version). If the user disconnects or the network fails, &lt;code&gt;pipeline&lt;/code&gt; automatically sends a &lt;code&gt;destroy&lt;/code&gt; signal to every stream in the chain, safely freeing up your server's RAM.&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;pipeline&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;node:stream/promises&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;fs&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;node:fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&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="s1"&gt;/download&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="o"&gt;=&amp;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;fileStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./massive.mp4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ✅ SAFE: pipeline monitors the connection.&lt;/span&gt;
    &lt;span class="c1"&gt;// If the user drops, it destroys fileStream automatically.&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileStream&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;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ERR_STREAM_PREMATURE_CLOSE&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User canceled the download halfway through.&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pipeline failed:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&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;h3&gt;
  
  
  2. Next.js App Router: Catching the &lt;code&gt;AbortSignal&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In Next.js 14+ Route Handlers, the user's browser connection is tied to the standard Web &lt;code&gt;Request&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;If a user navigates away while your API is streaming a heavy database query, you need to listen to &lt;code&gt;req.signal&lt;/code&gt; — an &lt;code&gt;AbortSignal&lt;/code&gt; that fires the moment the client drops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/app/api/heavy-export/route.ts&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;GET&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;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;encoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextEncoder&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;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReadableStream&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// 🛑 CRITICAL CHECK: Did the user close the browser tab?&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;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aborted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="s1"&gt;Client disconnected. Halting DB query.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="c1"&gt;// Simulate heavy DB fetch&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;fetchNextDatabaseRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;encoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&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;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&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="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stream&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;
  
  
  3. React Frontend: Canceling the Stream Reader
&lt;/h3&gt;

&lt;p&gt;If you are consuming a stream in the browser (like the AI "typing" effect) and the user clicks a &lt;strong&gt;"Stop Generating"&lt;/strong&gt; button or navigates away, you must actively cancel the reader.&lt;/p&gt;

&lt;p&gt;If you unmount the component without canceling, the browser will keep downloading chunks in the background — wasting the user's bandwidth.&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="s1"&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;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useRef&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="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;AIChat&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;readerRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;startStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="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;/api/ai&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;readerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&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="nf"&gt;getReader&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// ... loop through reader.read() ...&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="c1"&gt;// 🧹 Cleanup: runs when the component unmounts&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="k"&gt;return &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;readerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Instantly kills the active download stream&lt;/span&gt;
        &lt;span class="nx"&gt;readerRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User navigated away&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;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;startStream&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Generate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&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;a href="https://www.linkedin.com/posts/shubhamkhatik_activity-7438562352024854528-rPsK?utm_source=share&amp;amp;utm_medium=member_desktop&amp;amp;rcm=ACoAACd_x7wBD8X33xQgl3Mz6elMnSk717ZTyDE" rel="noopener noreferrer"&gt;LinkedIN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>data</category>
      <category>buffer</category>
      <category>webdev</category>
      <category>ai</category>
    </item>
    <item>
      <title>The DSA Dependency Graph: A Logical Roadmap from Arrays to DP</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Thu, 25 Dec 2025 10:22:14 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/the-dsa-dependency-graph-a-logical-roadmap-from-arrays-to-dp-3n05</link>
      <guid>https://forem.com/shubhamkhatik/the-dsa-dependency-graph-a-logical-roadmap-from-arrays-to-dp-3n05</guid>
      <description>&lt;p&gt;The Problem&lt;br&gt;
We often treat Data Structures and Algorithms (DSA) like a grocery list. We pick "Heaps" today and "Graphs" tomorrow. But computer science topics have dependencies. You cannot understand Heaps if you don't understand Arrays (how they are stored) and Trees (how they are visualized).&lt;/p&gt;

&lt;p&gt;I created a 7-Phase Roadmap tailored to build a mental model step-by-step. This order ensures that every new topic uses tools you mastered in the previous phase.&lt;/p&gt;

&lt;p&gt;🏗️ Phase 1: Basic Linear Structures &amp;amp; Their Patterns Goal: Mastering sequential data and index manipulation. Before touching complex nodes, master the line.&lt;br&gt;
Time &amp;amp; Space Complexity (Big-O): The measuring stick for all code.Arrays: Memory models (Static vs Dynamic).&lt;br&gt;
Searching and sorting Algorithms: Essential pre-requisite for Two Pointers.&lt;br&gt;
Recursion (Pattern): Just the basics (Base case vs Recursive case).Two Pointers (Pattern): The most common array optimization.&lt;br&gt;
Binary Search (Algorithm): $O(\log N)$ logic.&lt;br&gt;
Sliding Window (Pattern): Subarrays and substrings.&lt;br&gt;
Prefix Sum (Pattern): 1D and 2D range queries.Intervals (Pattern): Merging and inserting ranges.&lt;br&gt;
Matrix Traversal (Pattern): Basics of iterating 2D grids.&lt;/p&gt;

&lt;p&gt;🔍 Phase 2: The Power of Lookup (Hashing)Goal: Trading space for speed.&lt;br&gt;
Hash Table / Hash Map: The $O(1)$ lookup king.&lt;br&gt;
Strings: Immutability and manipulation techniques.&lt;/p&gt;

&lt;p&gt;🔗 Phase 3: Pointers &amp;amp; Recursion Goal: Moving from Indexes to References.&lt;br&gt;
Linked List: Single and Doubly linked.&lt;br&gt;
Recursion (Advanced): Multiple recursive calls (Memoization prep).&lt;br&gt;
Stack &amp;amp; Queue: LIFO and FIFO mechanics.&lt;br&gt;
Monotonic Stack (Pattern): Finding the "next greater element.&lt;br&gt;
"Fast &amp;amp; Slow Pointers (Pattern): Cycle detection.&lt;/p&gt;

&lt;p&gt;🌳 Phase 4: Non-Linear Structures (Hierarchical)Goal: Parent-Child relationships.&lt;br&gt;
Trees: Height, depth, diameter properties.&lt;br&gt;
Binary Trees: The standard interview structure.&lt;br&gt;
Tree Traversals: Preorder, Inorder, Postorder, Level-order.&lt;br&gt;
Binary Search Tree (BST): Sorted hierarchical data.&lt;br&gt;
Heaps / Priority Queue: Organizing by priority.&lt;br&gt;
Top K Elements (Pattern): Using Heaps efficiently.&lt;br&gt;
Divide and Conquer (Pattern): Merge Sort logic applied to trees.&lt;/p&gt;

&lt;p&gt;🕵️ Phase 5: Advanced Search &amp;amp; BacktrackingGoal: Exploring all possibilities.&lt;br&gt;
Backtracking (Pattern): Solving puzzles (Sudoku, N-Queens).&lt;br&gt;
Tries: Prefix trees for autocomplete.&lt;/p&gt;

&lt;p&gt;🌐 Phase 6: Connectivity (Graphs)Goal: Many-to-Many relationships.&lt;br&gt;
Graphs: Adjacency Matrix vs. Adjacency List.&lt;br&gt;
Matrix Traversal (Advanced): Treating Grids as Graphs.&lt;br&gt;
Graph Traversals: BFS (Shortest path in unweighted) &amp;amp; DFS.&lt;br&gt;
Union Find: Disjoint Set Union (DSU).&lt;br&gt;
Shortest Path: Dijkstra's Algorithm.&lt;/p&gt;

&lt;p&gt;🚀 Phase 7: Optimization (The Hardest Part)Goal: Solving overlapping sub-problems.&lt;br&gt;
Bit Manipulation: Binary logic.&lt;br&gt;
Dynamic Programming (DP): &lt;br&gt;
The pipeline: Recursion → Memoization → Tabulation → Space Optimization.&lt;br&gt;
Why this order?&lt;br&gt;
Recursion is Split: You need basic recursion for sorting, but advanced recursion for Trees and DP.&lt;br&gt;
Matrix is Split: You need to know how to loop through a matrix (Phase 1) before you can run BFS on it (Phase 6).&lt;br&gt;
Patterns First: We learn "Two Pointers" immediately after Arrays because that is how you actually use arrays in the real world.&lt;/p&gt;

&lt;p&gt;Save this roadmap and stop getting lost in the noise! 🚀&lt;/p&gt;

&lt;p&gt;==&amp;gt; Data Structures&lt;br&gt;
=&amp;gt; Linear&lt;br&gt;
. Array&lt;br&gt;
. Strings&lt;br&gt;
. Linked List&lt;br&gt;
. Stack&lt;br&gt;
. Queue&lt;br&gt;
. Deque&lt;br&gt;
. Hash Table / Hash Map&lt;/p&gt;

&lt;p&gt;=&amp;gt; Non-Linear&lt;br&gt;
. Trees&lt;br&gt;
. Binary Search Tree (BST)&lt;br&gt;
. Heaps / Priority Queue&lt;br&gt;
. Segment Tree / Fenwick Tree (BIT)&lt;br&gt;
. Graphs&lt;br&gt;
. Tries&lt;br&gt;
. Union Find (Disjoint Set)&lt;/p&gt;

&lt;p&gt;==&amp;gt; Algorithms&lt;br&gt;
. Linear Search&lt;br&gt;
. Basic Sorting (Bubble, Selection, Insertion)&lt;br&gt;
. Merge Sort / Quick Sort&lt;br&gt;
. Cyclic Sort&lt;br&gt;
. Binary Search&lt;br&gt;
. Modified Binary Search&lt;br&gt;
. Bit Manipulation&lt;br&gt;
. Tree Traversals&lt;br&gt;
. Graph Traversals&lt;br&gt;
. Topological Sort&lt;br&gt;
. Shortest Path (Dijkstra / Bellman-Ford / Floyd-Warshall)&lt;br&gt;
. Minimum Spanning Tree (Kruskal's / Prim's)&lt;/p&gt;

&lt;p&gt;==&amp;gt; Patterns&lt;br&gt;
. Two Pointers&lt;br&gt;
. Fast &amp;amp; Slow Pointers&lt;br&gt;
. Sliding Window&lt;br&gt;
. Prefix Sum&lt;br&gt;
. Intervals&lt;br&gt;
. Cyclic Sort          ← borderline pattern/algorithm, fits both&lt;br&gt;
. Matrix Traversal&lt;br&gt;
. Monotonic Stack/Queue&lt;br&gt;
. Divide &amp;amp; Conquer&lt;br&gt;
. Greedy&lt;br&gt;
. Recursion&lt;br&gt;
. Backtracking&lt;br&gt;
. Top K Elements (Heap)&lt;br&gt;
. K-way Merge&lt;/p&gt;

&lt;h2&gt;
  
  
  . Dynamic Programming
&lt;/h2&gt;

&lt;p&gt;====&amp;gt; order of learning&lt;br&gt;
=&amp;gt; Phase 1: Basic Linear Structures &amp;amp; Their Patterns&lt;br&gt;
. Time &amp;amp; Space Complexity (Big-O)&lt;br&gt;
. array&lt;br&gt;
. strings&lt;br&gt;
. Recursion (pattern) [base case, recursive case, call stack visualization]&lt;br&gt;
. searching (Algorithm) [Linear Search]&lt;br&gt;
. Basic Sorting (Bubble, Selection, Insertion)&lt;br&gt;
. two pointers (pattern)&lt;br&gt;
. binary search (algorithm)&lt;br&gt;
. Modified Binary Search&lt;br&gt;
. sliding window (pattern)&lt;br&gt;
. Cyclic Sort&lt;br&gt;
. prefix sum (pattern) [(1D and 2D)]&lt;br&gt;
. Intervals (Pattern)&lt;br&gt;
. Matrix Traversal (Pattern) [indexing, 2D prefix sum]&lt;/p&gt;

&lt;p&gt;=&amp;gt; Phase 2: The Power of Lookup (Hashing)&lt;br&gt;
. hash table /hash map&lt;br&gt;
. Greedy (Pattern)&lt;/p&gt;

&lt;p&gt;=&amp;gt; Phase 3: Pointers &amp;amp; Recursion&lt;br&gt;
. linked list &lt;br&gt;
. Recursion (pattern) [multiple recursive calls, memoization prep, linked list recursion]&lt;br&gt;
. sorting (Merge sort, Quick sort)&lt;br&gt;
. Divide and Conquer (Pattern)&lt;br&gt;
. stack&lt;br&gt;
. queue&lt;br&gt;
. Deque (Double-ended Queue)&lt;br&gt;
. Monotonic Stack/Queue (pattern)&lt;br&gt;
. fast and slow pointers (pattern)&lt;/p&gt;

&lt;p&gt;=&amp;gt; Phase 4: Non-Linear Structures (Hierarchical)&lt;br&gt;
. Trees and properties (height, depth, diameter)&lt;br&gt;
. binary trees&lt;br&gt;
. Tree Traversals (Algorithm) [(preorder, inorder, postorder, level-order)]&lt;br&gt;
. Binary Search Tree (BST)&lt;br&gt;
. Heaps / Priority Queue&lt;br&gt;
. Segment Tree / Fenwick Tree (BIT) &lt;br&gt;
. Top K Elements (Pattern)&lt;br&gt;
. K-way Merge&lt;/p&gt;

&lt;p&gt;=&amp;gt; Phase 5: Advanced Search &amp;amp; Backtracking&lt;br&gt;
. Backtracking (Pattern)&lt;br&gt;
. Tries&lt;/p&gt;

&lt;p&gt;=&amp;gt; Phase 6: Connectivity (Graphs)&lt;br&gt;
. Graphs and Graph representations (Adjacency List / Matrix)&lt;br&gt;
. Matrix Traversal (Pattern) [(DFS/BFS on grids)]&lt;br&gt;
. Graph Traversals (Algorithm) &lt;br&gt;
. Topological Sort&lt;br&gt;
. Union Find (Disjoint Set)&lt;br&gt;
. Shortest Path (Algorithm) (Dijkstra / Bellman-Ford / Floyd-Warshall)&lt;br&gt;
. Minimum Spanning Tree (Kruskal's / Prim's)&lt;/p&gt;

&lt;p&gt;=&amp;gt; Phase 7: Optimization (The Hardest Part)&lt;br&gt;
. Bit Manipulation (algorithm)&lt;br&gt;
. Dynamic Programming  (pattern) [Recursion → Memoization. Tabulation. Space Optimization]&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>beginners</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding OAuth Login Flow with GitHub: Why Your Backend is Essential</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Fri, 17 Oct 2025 11:31:21 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/understanding-oauth-login-flow-with-github-why-your-backend-is-essential-c90</link>
      <guid>https://forem.com/shubhamkhatik/understanding-oauth-login-flow-with-github-why-your-backend-is-essential-c90</guid>
      <description>&lt;p&gt;Implementing social login using OAuth with providers like GitHub can be a powerful way to improve user experience and security. However, the OAuth flow involves several important steps that developers sometimes misunderstand, especially around how tokens are handled between frontend and backend.&lt;/p&gt;

&lt;p&gt;In this post, you’ll walk through the typical OAuth login process using GitHub as an example, understand the roles of the frontend and backend, and highlight critical security considerations.&lt;/p&gt;




&lt;h3&gt;
  
  
  What is OAuth and Why Use It?
&lt;/h3&gt;

&lt;p&gt;OAuth is an open authorization standard that grants third-party applications limited access to user accounts on services like Google, GitHub, and Facebook—without exposing user credentials. It enables users to log in using their existing accounts without creating new passwords.&lt;/p&gt;




&lt;h3&gt;
  
  
  The GitHub OAuth Login Flow in a Nutshell
&lt;/h3&gt;

&lt;p&gt;When integrating GitHub OAuth in a React app, you often hear about getting a "code" after the user authorizes your app, then exchanging that code for an access token to fetch user data. But what exactly happens here, and why can’t the frontend do everything?&lt;/p&gt;




&lt;h3&gt;
  
  
  Step-by-Step Flow
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. User Authorizes Your App on GitHub
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Users click “Login with GitHub” on your React app.&lt;/li&gt;
&lt;li&gt;They are redirected to GitHub’s consent page to grant permission.&lt;/li&gt;
&lt;li&gt;After authorization, GitHub redirects users back with a temporary authorization code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Frontend Receives the Authorization Code
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The React frontend receives this short-lived authorization code (let’s call it &lt;code&gt;code1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;This code doesn’t have user info but signals the user’s consent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Frontend Sends Code to Backend
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;/auth/github/callback&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="s1"&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;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;code1&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="s1"&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="s1"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Backend Exchanges Code for Access Token
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Backend uses the code, &lt;code&gt;client_id&lt;/code&gt;, and &lt;code&gt;client_secret&lt;/code&gt; to request an access token from GitHub.
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tokenResponse&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;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&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://github.com/login/oauth/access_token&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;client_id&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;GITHUB_CLIENT_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;client_secret&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;GITHUB_CLIENT_SECRET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;receivedCode1&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="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="na"&gt;Accept&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;accessToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tokenResponse&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="nx"&gt;access_token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. Backend Requests User Info from GitHub
&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;userResponse&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;axios&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="s1"&gt;https://api.github.com/user&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;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;Authorization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Bearer &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;userData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userResponse&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="c1"&gt;// Process and store user data or create session&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. Backend Creates Session or Issues JWT
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Backend creates a session or issues a signed JWT token encoding user info.&lt;/li&gt;
&lt;li&gt;Sends this token or session ID back to the frontend.&lt;/li&gt;
&lt;li&gt;Frontend uses this on subsequent requests to authenticate the user.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Why Offload Token Exchange to Backend?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client Secret Security:&lt;/strong&gt; Never expose &lt;code&gt;client_secret&lt;/code&gt; on the frontend—it’s a private credential.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token Privacy:&lt;/strong&gt; Backend keeps access tokens hidden from clients, limiting risk from frontend breaches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control &amp;amp; Revocation:&lt;/strong&gt; Backend manages session lifecycle, allowing token/session invalidation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CORS &amp;amp; Rate Limits:&lt;/strong&gt; Backend avoids cross-origin and rate limiting issues with third-party APIs.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Summary of Data Flow
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Party&lt;/th&gt;
&lt;th&gt;What it Receives/Uses&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Frontend React App&lt;/td&gt;
&lt;td&gt;Authorization Code (&lt;code&gt;code1&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Signals user permission to backend&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend Server&lt;/td&gt;
&lt;td&gt;code1 → Access Token&lt;/td&gt;
&lt;td&gt;Gets authorized API access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend Server&lt;/td&gt;
&lt;td&gt;Access Token&lt;/td&gt;
&lt;td&gt;Fetches user data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend Server&lt;/td&gt;
&lt;td&gt;User data &amp;amp; session/JWT&lt;/td&gt;
&lt;td&gt;Creates session or issues JWT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frontend React App&lt;/td&gt;
&lt;td&gt;Session cookie or JWT&lt;/td&gt;
&lt;td&gt;Authenticates future requests&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




</description>
      <category>github</category>
      <category>security</category>
      <category>tutorial</category>
      <category>backend</category>
    </item>
    <item>
      <title>The Complete Guide to AI-SEO in 2026</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Tue, 26 Aug 2025 10:49:56 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/the-complete-guide-to-ai-seo-in-2026-1j54</link>
      <guid>https://forem.com/shubhamkhatik/the-complete-guide-to-ai-seo-in-2026-1j54</guid>
      <description>&lt;p&gt;🔥 The Complete Guide to AI-SEO in 2026: 6 Factors That Matter Most&lt;/p&gt;

&lt;p&gt;AI search engines like Google SGE (Search Generative Experience), Perplexity, and ChatGPT Browse are changing the way people consume information.&lt;br&gt;
Instead of clicking links, users often read AI summaries directly.&lt;/p&gt;

&lt;p&gt;So, how do you make sure your site is the one AI pulls data from?&lt;br&gt;
The answer → AI-SEO.&lt;/p&gt;

&lt;p&gt;Here are the 6 major factors that decide whether search engines and AI models trust your content.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Technical SEO (Crawlability &amp;amp; Indexability)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before ranking or summarizing your content, Google bots and AI scrapers need to find and read your pages.&lt;/p&gt;

&lt;p&gt;Key elements:&lt;/p&gt;

&lt;p&gt;Sitemap.xml → Lists all pages &amp;amp; last updated date.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;url&amp;gt;&lt;br&gt;
  &amp;lt;loc&amp;gt;https://example.com/ai-seo&amp;lt;/loc&amp;gt;&lt;br&gt;
  &amp;lt;lastmod&amp;gt;2025-08-24&amp;lt;/lastmod&amp;gt;&lt;br&gt;
&amp;lt;/url&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Use next-sitemap in Next.js to auto-generate.&lt;/p&gt;

&lt;p&gt;Robots.txt → Controls what crawlers can access.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
User-agent: *&lt;br&gt;
Allow: /&lt;br&gt;
Disallow: /admin&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Page speed / Core Web Vitals → Optimize for LCP, CLS, FID.&lt;br&gt;
Tools: PageSpeed Insights&lt;br&gt;
.&lt;/p&gt;

&lt;p&gt;Mobile-friendliness → Responsive UI is mandatory.&lt;/p&gt;

&lt;p&gt;HTTPS → Secure browsing (SSL certificate).&lt;/p&gt;

&lt;p&gt;Canonical tags → Prevent duplicate content issues.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&amp;lt;link rel="canonical" href="https://example.com/ai-seo" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On-Page SEO (Content Optimization)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once crawlers can access your site, they need to understand it.&lt;/p&gt;

&lt;p&gt;Key elements:&lt;/p&gt;

&lt;p&gt;Title tag &amp;amp; Meta description&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&amp;lt;title&amp;gt;AI SEO Guide 2025 - Next.js Optimization&amp;lt;/title&amp;gt;&lt;br&gt;
&amp;lt;meta name="description" content="Learn how to optimize websites for AI SEO in 2025 with Next.js."&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Headings (H1, H2, H3) → Maintain hierarchy with keywords.&lt;br&gt;
Example:&lt;br&gt;
`&lt;br&gt;
H1: AI SEO Guide 2025&lt;/p&gt;

&lt;p&gt;H2: Technical SEO Best Practices&lt;/p&gt;

&lt;p&gt;H3: Sitemap.xml Example`&lt;/p&gt;

&lt;p&gt;Keyword placement → Natural use in intro, body, headings, alt text.&lt;/p&gt;

&lt;p&gt;Internal linking → Pass authority to deeper pages.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&amp;lt;a href="/ai-seo/technical"&amp;gt;Technical SEO Guide&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Image alt text → Helps search engines &amp;amp; accessibility.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&amp;lt;img src="seo.png" alt="AI SEO best practices infographic" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Schema markup (Structured Data) → Enables rich snippets.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&amp;lt;html lang="en"&amp;gt;&lt;br&gt;
&amp;lt;head&amp;gt;&lt;br&gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;&lt;br&gt;
  &amp;lt;title&amp;gt;AI SEO Guide&amp;lt;/title&amp;gt;&lt;br&gt;
  &amp;lt;script type="application/ld+json"&amp;gt;&lt;br&gt;
  {&lt;br&gt;
    "@context": "https://schema.org",&lt;br&gt;
    "@type": "FAQPage",&lt;br&gt;
    "mainEntity": [{&lt;br&gt;
      "question": "What is AI SEO?",&lt;br&gt;
      "acceptedAnswer": { "text": "AI SEO optimizes content for AI-powered search engines." }&lt;br&gt;
    }]&lt;br&gt;
  }&lt;br&gt;
  &amp;lt;/script&amp;gt;&lt;br&gt;
&amp;lt;/head&amp;gt;&lt;br&gt;
&amp;lt;body&amp;gt;&lt;br&gt;
  &amp;lt;h1&amp;gt;AI SEO Guide&amp;lt;/h1&amp;gt;&lt;br&gt;
  &amp;lt;p&amp;gt;AI SEO optimizes content for AI-powered search engines...&amp;lt;/p&amp;gt;&lt;br&gt;
&amp;lt;/body&amp;gt;&lt;br&gt;
&amp;lt;/html&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Content Quality (E-E-A-T Framework)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Google and AI models now score content based on Expertise, Experience, Authoritativeness, and Trustworthiness (E-E-A-T).&lt;/p&gt;

&lt;p&gt;Key elements:&lt;/p&gt;

&lt;p&gt;Expertise → Written by subject-matter experts.&lt;/p&gt;

&lt;p&gt;Experience → Real-world examples, case studies.&lt;/p&gt;

&lt;p&gt;Authoritativeness → Backlinks from trusted sites.&lt;/p&gt;

&lt;p&gt;Trustworthiness → Accuracy, updated sources, transparency.&lt;/p&gt;

&lt;p&gt;📌 Example:&lt;/p&gt;

&lt;p&gt;Add an author bio → “Written by Shubham Khatik, Full-Stack Developer (4 YOE in React &amp;amp; Next.js).”&lt;/p&gt;

&lt;p&gt;Add citations &amp;amp; references to studies or docs.&lt;/p&gt;

&lt;p&gt;Keep a “last updated” date on blog posts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Off-Page SEO (Backlinks &amp;amp; Authority)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Search engines &amp;amp; AI models rely heavily on external signals.&lt;/p&gt;

&lt;p&gt;Key elements:&lt;/p&gt;

&lt;p&gt;Backlinks → Guest posts, niche collaborations.&lt;/p&gt;

&lt;p&gt;Mentions → HARO (Help a Reporter Out), PR campaigns.&lt;/p&gt;

&lt;p&gt;Social signals → Shares on LinkedIn, Twitter/X.&lt;/p&gt;

&lt;p&gt;Community engagement → Reddit, StackOverflow answers with links.&lt;/p&gt;

&lt;p&gt;📌 Example:&lt;br&gt;
If you run a React SEO blog, write a guest article for Smashing Magazine → link back to your site.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User Experience (Engagement Metrics)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Search engines track how users behave on your site.&lt;/p&gt;

&lt;p&gt;Key elements:&lt;/p&gt;

&lt;p&gt;CTR (Click-through rate) → Titles &amp;amp; meta descriptions should attract clicks.&lt;/p&gt;

&lt;p&gt;Dwell time → Keep users engaged with visuals, videos, TOCs.&lt;/p&gt;

&lt;p&gt;Bounce rate → Reduce drop-offs with clean navigation &amp;amp; summaries.&lt;/p&gt;

&lt;p&gt;Mobile UX → Ensure smooth layouts.&lt;/p&gt;

&lt;p&gt;📌 Example:&lt;/p&gt;

&lt;p&gt;Add Table of Contents in long blogs (reduces bounce).&lt;/p&gt;

&lt;p&gt;Use sticky navigation for mobile.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Content Freshness&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;AI prefers up-to-date content.&lt;/p&gt;

&lt;p&gt;Key elements:&lt;/p&gt;

&lt;p&gt;Update old blogs regularly (e.g., “SEO 2022” → “SEO 2025”).&lt;/p&gt;

&lt;p&gt;Sitemap  → Helps Google &amp;amp; AI detect new updates.&lt;/p&gt;

&lt;p&gt;Dynamic content → Job boards, latest stats, tools.&lt;/p&gt;

&lt;p&gt;Seasonal updates → Example: “Black Friday Deals 2025.”&lt;/p&gt;

&lt;p&gt;Schema dateModified → Mark updated content.&lt;/p&gt;

&lt;p&gt;📌 Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
  "@context": "https://schema.org",&lt;br&gt;
  "@type": "Article",&lt;br&gt;
  "headline": "AI SEO Guide 2025",&lt;br&gt;
  "dateModified": "2025-08-24"&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🎯 Final Takeaway&lt;/p&gt;

&lt;p&gt;In 2025, SEO isn’t just about keywords anymore.&lt;br&gt;
AI-driven search engines rank content using a combination of:&lt;/p&gt;

&lt;p&gt;Technical SEO – Can bots access your site?&lt;/p&gt;

&lt;p&gt;On-Page SEO – Is your content structured for machines?&lt;/p&gt;

&lt;p&gt;Content Quality (E-E-A-T) – Is it expert &amp;amp; trustworthy?&lt;/p&gt;

&lt;p&gt;Off-Page SEO – Do others vouch for you?&lt;/p&gt;

&lt;p&gt;User Experience – Do users stay engaged?&lt;/p&gt;

&lt;p&gt;Content Freshness – Is it up to date?&lt;/p&gt;

&lt;p&gt;👉 AI will always summarize. Your job is to be the source AI trusts.&lt;/p&gt;

</description>
      <category>seo</category>
      <category>aiseo</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Sitemaps for SEO</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Tue, 26 Aug 2025 09:18:14 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/sitemaps-for-seo-25co</link>
      <guid>https://forem.com/shubhamkhatik/sitemaps-for-seo-25co</guid>
      <description>&lt;p&gt;🚀 Just discovered something new about SEO &amp;amp; AI!&lt;/p&gt;

&lt;p&gt;We all know  tags help describe a single page, but here’s the twist:&lt;/p&gt;

&lt;p&gt;👉 Sitemaps (sitemap.xml) tell Google &amp;amp; AI about your entire website and when each page was last updated.&lt;/p&gt;

&lt;p&gt;Without it, crawlers guess your structure. With it, you hand them a clean map + freshness signals 📅.&lt;/p&gt;

&lt;p&gt;Example (inside sitemap.xml):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;url&amp;gt;

  &amp;lt;loc&amp;gt;https://example.com/ai-seo&amp;lt;/loc&amp;gt;

  &amp;lt;lastmod&amp;gt;2025-08-24&amp;lt;/lastmod&amp;gt;

&amp;lt;/url&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; = your page URL&lt;/p&gt;

&lt;p&gt; = when the page was last updated&lt;/p&gt;

&lt;p&gt;This helps search engines &amp;amp; AI summaries pick the freshest content, not outdated info.&lt;/p&gt;

&lt;p&gt;💡 In React apps → put sitemap.xml in /public/ folder.&lt;/p&gt;

&lt;p&gt;💡 In Next.js (App Router) → create app/sitemap.ts and let Next auto-generate lastmod.&lt;/p&gt;

&lt;p&gt;🔑 Takeaway:&lt;/p&gt;

&lt;p&gt;SEO is shifting from just meta tags → to structured data + sitemaps + freshness.&lt;/p&gt;

&lt;p&gt;If you want your content to show up in AI summaries, freshness is no longer optional 🚀.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/shubhamkhatik_seo-nextjs-webdevelopment-activity-7366035601676046336-ni6d?utm_source=share&amp;amp;utm_medium=member_desktop&amp;amp;rcm=ACoAACd_x7wBD8X33xQgl3Mz6elMnSk717ZTyDE" rel="noopener noreferrer"&gt;linkedin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>aiseo</category>
      <category>seo</category>
      <category>sitemap</category>
    </item>
    <item>
      <title>🚀 Next.js App Router : CSR SSR SSG ISR</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Sun, 24 Aug 2025 10:17:57 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/nextjs-app-router-csr-ssr-ssg-isr-p7e</link>
      <guid>https://forem.com/shubhamkhatik/nextjs-app-router-csr-ssr-ssg-isr-p7e</guid>
      <description>&lt;p&gt;🚀 Next.js App Router: Forget getStaticProps, Think Caching&lt;/p&gt;

&lt;p&gt;When I first switched from Pages Router to the new App Router in Next.js, I noticed something strange:&lt;/p&gt;

&lt;p&gt;👉 There’s no getStaticProps or getServerSideProps.&lt;/p&gt;

&lt;p&gt;At first it felt like SSG &amp;amp; SSR were “gone”… but in reality, they’ve just been reimagined.&lt;/p&gt;

&lt;p&gt;In App Router, rendering strategy = caching strategy.&lt;/p&gt;

&lt;p&gt;Here’s how it works ⬇️&lt;/p&gt;

&lt;p&gt;🔹 CSR (Client-Side Rendering)&lt;/p&gt;

&lt;p&gt;`'use client';&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;/p&gt;

&lt;p&gt;fetch('/api/data').then(r =&amp;gt; r.json());&lt;/p&gt;

&lt;p&gt;}, []);`&lt;/p&gt;

&lt;p&gt;Happens by default when you fetch data in a Client Component.&lt;/p&gt;

&lt;p&gt;🔹 SSR (Server-Side Rendering)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await fetch(url, { cache: 'no-store' })&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This ensures fresh data on every request.&lt;/p&gt;

&lt;p&gt;🔹 SSG (Static Site Generation)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await fetch(url, { cache: 'force-cache' }) // default&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Data is fetched at build time, HTML is reused for all users.&lt;/p&gt;

&lt;p&gt;🔹 ISR (Incremental Static Regeneration)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await fetch(url, { next: { revalidate: 60 } })&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Page is pre-rendered like SSG, but revalidated every 60s in the background.&lt;/p&gt;

&lt;p&gt;⚖️ Old mental model:&lt;/p&gt;

&lt;p&gt;getStaticProps = SSG&lt;/p&gt;

&lt;p&gt;getServerSideProps = SSR&lt;/p&gt;

&lt;p&gt;⚡ New mental model:&lt;/p&gt;

&lt;p&gt;fetch + caching = CSR, SSR, SSG, ISR&lt;/p&gt;

&lt;p&gt;💡 Key takeaway:&lt;/p&gt;

&lt;p&gt;Next.js App Router unifies rendering modes under one API. Instead of special lifecycle methods, you just decide caching rules → much more flexible (and powerful).&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>seo</category>
    </item>
    <item>
      <title>JavaScript Question Part 2</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Wed, 06 Aug 2025 10:59:11 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/javascript-question-part-2-1km8</link>
      <guid>https://forem.com/shubhamkhatik/javascript-question-part-2-1km8</guid>
      <description>&lt;p&gt;🚀 Frontend Performance &amp;amp; React Tips Every Developer Should Know 🚀&lt;/p&gt;

&lt;p&gt;Ever wondered what really happens when you hit a URL in your browser? It’s a fascinating journey through DNS lookup, HTTP requests, and the Critical Rendering Path (CRP)—the secret sauce that transforms code into pixels on your screen. Optimizing the CRP means faster load times and happier users!&lt;/p&gt;

&lt;p&gt;⚡️ Quick Rundown on Key Frontend Concepts:&lt;/p&gt;

&lt;p&gt;Repaint vs. Reflow:&lt;/p&gt;

&lt;p&gt;Repaint updates visual styles (color, visibility) and costs less;&lt;/p&gt;

&lt;p&gt;Reflow recalculates layouts (size, position) and is more expensive.&lt;/p&gt;

&lt;p&gt;Canceling Old API Calls:&lt;/p&gt;

&lt;p&gt;Use AbortController to cancel outdated fetch requests so your app only processes the latest data and stays performant.&lt;/p&gt;

&lt;p&gt;Parallel API Calls:&lt;/p&gt;

&lt;p&gt;Use Promise.all() when all API calls must succeed, as it fails fast on the first rejection. Use Promise.allSettled() when you need to run all calls in parallel and handle both successes and failures individually.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/shubhamkhatik/frontend-interview-guide/blob/main/JavaScript%20Question/js%20part%202.md" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/shubhamkhatik/javascript-question-4n79"&gt;JavaScript Question Part 1&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Frontend #WebDevelopment #ReactJS #JavaScript #Performance #WebPerf #CodingTips
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>JavaScript Question Part 1</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Tue, 05 Aug 2025 08:26:51 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/javascript-question-4n79</link>
      <guid>https://forem.com/shubhamkhatik/javascript-question-4n79</guid>
      <description>&lt;p&gt;🚀 Boost Your Web Performance &amp;amp; User Experience!&lt;/p&gt;

&lt;p&gt;Understanding frontend essentials can transform your website’s speed and efficiency:&lt;/p&gt;

&lt;p&gt;🔹 Preload, Prefetch, Reconnect, Prerender: Techniques to load resources smarter—fetch critical files early, set up server connections in advance, anticipate user navigation, and even render pages before they’re requested!&lt;/p&gt;

&lt;p&gt;🔹 Caching &amp;amp; HTTP Headers: Use Cache-Control and ETag headers to control how browsers store and validate content, reducing load times and server strain.&lt;/p&gt;

&lt;p&gt;🔹 Asset Optimization &amp;amp; Image Compression: Minimize file sizes with compression techniques and serve modern formats like WebP for faster loads without sacrificing quality. Know when to use PNG, JPEG, or WebP.&lt;/p&gt;

&lt;p&gt;🔹 Memory Leaks: Avoid hidden performance killers by managing memory carefully—release unused DOM nodes and event listeners to keep your app smooth and responsive.&lt;/p&gt;

&lt;p&gt;Master these strategies and watch your frontend performance soar! ⚡&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/shubhamkhatik/frontend-interview-guide/blob/main/JavaScript%20Question/js%20part%201.md" rel="noopener noreferrer"&gt;github&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  WebDevelopment #Frontend #WebPerformance #Caching #ImageOptimization #JavaScript #MemoryManagement
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>System Design</title>
      <dc:creator>shubham khatik</dc:creator>
      <pubDate>Wed, 30 Jul 2025 06:30:33 +0000</pubDate>
      <link>https://forem.com/shubhamkhatik/system-design-23e6</link>
      <guid>https://forem.com/shubhamkhatik/system-design-23e6</guid>
      <description>&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/shubhamkhatik_system-design-activity-7356212091541762049-etVj?utm_source=share&amp;amp;utm_medium=member_desktop&amp;amp;rcm=ACoAACd_x7wBD8X33xQgl3Mz6elMnSk717ZTyDE" rel="noopener noreferrer"&gt;PDF [in depth]&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Summary: Backend System Design
&lt;/h3&gt;

&lt;p&gt;This conversation covered foundational and advanced topics in backend system design, focusing on how to build reliable, scalable, performant, and maintainable systems. Here is a structured summary by category:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Core Concepts and Patterns&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Consistency &amp;amp; Availability Patterns:&lt;/strong&gt;
Distributed systems balance data consistency (strong, eventual, causal, read-your-writes, monotonic reads/writes) with availability (failover, replication, load balancing, redundancy, auto-scaling, circuit breakers, bulkhead, leader election).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Horizontal Scaling:&lt;/strong&gt;
Adding more servers/instances improves throughput, uptime, and elasticity. Stateless design, load balancers, service discovery, and distributed storage enable scaling out effectively, while stateful components must use replication/sync strategies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Architecture &amp;amp; Key Components&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Databases:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Wide Column Stores (e.g., Cassandra, HBase): Flexible, scalable for semi-structured big data.&lt;/li&gt;
&lt;li&gt;Denormalization: Boosts read performance but increases redundancy and consistency risk.&lt;/li&gt;
&lt;li&gt;Indexes: Speed up queries via auxiliary structures (B-trees, hash, bitmap) to avoid full scans, trading off extra storage and slower writes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Caching:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Strategies: Cache-aside, read-through, write-through, write-back, write-around, refresh-ahead.&lt;/li&gt;
&lt;li&gt;Placement: Client-side, server/db-side, distributed (e.g., Redis), edge/CDN.&lt;/li&gt;
&lt;li&gt;Balances speed, consistency, and complexity—chosen based on workload and latency needs.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;System Design Patterns:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Reliability Patterns: Bulkhead, circuit breaker, retry, saga, health checks, load leveling, leader election, replication; all used for fault tolerance and service continuity.&lt;/li&gt;
&lt;li&gt;Cloud Patterns: Bulkhead, circuit breaker, retry, throttling, cache aside, external configuration, strangler, etc., supporting resilience, scalability, and cost optimization.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Operations &amp;amp; Performance&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Load Balancers &amp;amp; Algorithms:&lt;/strong&gt;
Hardware/software/cloud-based, L4 (transport) vs. L7 (application), and algorithms (round robin, least connections, source IP hash, etc.) optimize traffic distribution, uptime, and scalability.

&lt;ul&gt;
&lt;li&gt;Load balancer and reverse proxy differences/overlaps highlighted.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Performance Antipatterns:&lt;/strong&gt;
Issues like chatty I/O, lack of caching, synchronous/blocking calls, retry storms, and single points of failure degrade speed and stability. These are avoided through batching, async communication, caching, and robust error handling.&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Observability &amp;amp; Maintenance&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring:&lt;/strong&gt;
Systems are instrumented using metrics, logging, tracing, dashboards, and alerting tools (Prometheus, Grafana, Datadog). Monitoring is vital for detecting/regressing faults, optimizing performance, and capacity planning.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Data &amp;amp; API Management&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Service Discovery:&lt;/strong&gt;
Microservices use service registries (e.g., Consul, Eureka), client/server-side discovery, and mesh/gateways for dynamic routing as instances scale or change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event-Driven &amp;amp; Schedule-Driven Jobs:&lt;/strong&gt;
Systems offload background tasks, triggered by events (real-time response) or schedules (cron jobs), improving performance and automation flexibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GraphQL:&lt;/strong&gt;
An efficient, flexible, strongly-typed API query language enabling clients to fetch exactly the data needed in a single call, compared to REST.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Reliability, Security, and Resilience&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reliability Patterns:&lt;/strong&gt;
Patterns like replication, circuit breakers, bulkhead, and automatic failover are central to ensuring high availability and resilient, self-healing systems. Security is reinforced through patterns like rate limiting and external configuration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Best Practices &amp;amp; Tradeoffs&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Statelessness enables fast scaling&lt;/strong&gt;; stateful systems require more carefully managed replication and synchronization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching and indexing accelerate reads&lt;/strong&gt; but bring risks of staleness and overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cloud design patterns must balance resilience, complexity, and cost.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring and observability are essential&lt;/strong&gt; for continuous improvement and stability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotent operations and asynchronous design&lt;/strong&gt; are key to building robust APIs and distributed systems resilient to retries and failures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This collection of patterns and strategies forms a toolkit for building highly available, scalable, secure, and observable backend architectures suitable for modern cloud-native, data-intensive applications.&lt;/p&gt;

</description>
      <category>system</category>
      <category>cloud</category>
      <category>backend</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
