<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BHARGAB KALITA</title>
    <description>The latest articles on DEV Community by BHARGAB KALITA (@bhargab).</description>
    <link>https://dev.to/bhargab</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%2F441148%2F36444de4-00b1-4045-b1ff-e148f599b7d1.jpg</url>
      <title>DEV Community: BHARGAB KALITA</title>
      <link>https://dev.to/bhargab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhargab"/>
    <language>en</language>
    <item>
      <title>How I Automated My GitHub Profile README With GitHub Actions (And How You Can Automate Anything Too)</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Mon, 01 Dec 2025 22:01:16 +0000</pubDate>
      <link>https://dev.to/bhargab/how-i-automated-my-github-profile-readme-with-github-actions-and-how-you-can-automate-anything-too-1lkm</link>
      <guid>https://dev.to/bhargab/how-i-automated-my-github-profile-readme-with-github-actions-and-how-you-can-automate-anything-too-1lkm</guid>
      <description>&lt;p&gt;At the end of last year, I started writing a few more articles on Dev.to. I’d been posting for a while, but I’m honestly too lazy to keep updating my GitHub Profile README every time. Opening the repo, editing the README, adding links… it wasn’t difficult, just boring. And boring tasks always make me wonder why I’m still doing them manually.&lt;/p&gt;

&lt;p&gt;So I looked around, tried a few things, and with a bit of help from AI, I finally found a way to automate it. After some quick experiments, everything clicked. My profile started updating itself with my latest Dev.to articles. No manual edits. No drama. Just simple automation doing its job.&lt;/p&gt;

&lt;p&gt;It was a small win, but it felt great. And that’s what pushed me to share how I did it and what else you can automate using GitHub Actions.&lt;/p&gt;

&lt;h2&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%2Fxq6lk00fepxklwpn91xv.png" alt=" " width="800" height="445"&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Before You Start: The Special GitHub Profile Repository
&lt;/h2&gt;

&lt;p&gt;This part is important.&lt;/p&gt;

&lt;p&gt;GitHub only displays your README as your public profile if you create a repo &lt;strong&gt;exactly named after your username&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Username → &lt;code&gt;yourusername&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Profile repo → also &lt;code&gt;yourusername&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don’t have this repository yet, create a new &lt;strong&gt;public&lt;/strong&gt; one with the same name as your username. Once it exists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the README becomes your profile README&lt;/li&gt;
&lt;li&gt;you can add workflows in &lt;code&gt;.github/workflows&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;automation will run exactly like in any other repo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the repo where I added my workflow last year, and it’s still running perfectly.&lt;/p&gt;




&lt;h2&gt;
  
  
  How I Automated My &lt;a href="https://github.com/the-wrong-guy/the-wrong-guy" rel="noopener noreferrer"&gt;Profile&lt;/a&gt; &lt;a href="https://github.com/the-wrong-guy/the-wrong-guy/blob/main/README.md" rel="noopener noreferrer"&gt;README &lt;/a&gt;to Show My Latest Dev.to Articles
&lt;/h2&gt;

&lt;p&gt;The goal was simple:&lt;br&gt;
&lt;strong&gt;Show the 3 latest posts from Dev.to in my README without updating anything manually.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I changed my workflow so that it fetches only the latest 3 articles and rewrites the section in my README.&lt;/p&gt;

&lt;p&gt;Here’s the complete GitHub Actions workflow.&lt;/p&gt;


&lt;h2&gt;
  
  
  GitHub Actions Workflow (Shows Only 3 Latest Dev.to Articles)
&lt;/h2&gt;

&lt;p&gt;Create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.github/workflows/update-readme.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Update README with latest Dev.to Post&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;schedule&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cron&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*'&lt;/span&gt;
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;update-readme&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout Repository&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v3&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Fetch Latest Dev.to Posts (replace the username)&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;fetch_dev_posts&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;DEVTO_USERNAME="bhargab"&lt;/span&gt;
          &lt;span class="s"&gt;API_RESPONSE=$(curl -s "https://dev.to/api/articles?username=${DEVTO_USERNAME}")&lt;/span&gt;

          &lt;span class="s"&gt;# Get the titles, URLs, and publish dates of the latest 3 posts&lt;/span&gt;
          &lt;span class="s"&gt;TITLE_1=$(echo $API_RESPONSE | jq -r '.[0].title')&lt;/span&gt;
          &lt;span class="s"&gt;URL_1=$(echo $API_RESPONSE | jq -r '.[0].url')&lt;/span&gt;
          &lt;span class="s"&gt;DATE_1=$(echo $API_RESPONSE | jq -r '.[0].published_at' | cut -d'T' -f1)&lt;/span&gt;

          &lt;span class="s"&gt;TITLE_2=$(echo $API_RESPONSE | jq -r '.[1].title')&lt;/span&gt;
          &lt;span class="s"&gt;URL_2=$(echo $API_RESPONSE | jq -r '.[1].url')&lt;/span&gt;
          &lt;span class="s"&gt;DATE_2=$(echo $API_RESPONSE | jq -r '.[1].published_at' | cut -d'T' -f1)&lt;/span&gt;

          &lt;span class="s"&gt;TITLE_3=$(echo $API_RESPONSE | jq -r '.[2].title')&lt;/span&gt;
          &lt;span class="s"&gt;URL_3=$(echo $API_RESPONSE | jq -r '.[2].url')&lt;/span&gt;
          &lt;span class="s"&gt;DATE_3=$(echo $API_RESPONSE | jq -r '.[2].published_at' | cut -d'T' -f1)&lt;/span&gt;

          &lt;span class="s"&gt;# Pass these values to the GitHub environment for use in the next steps&lt;/span&gt;
          &lt;span class="s"&gt;echo "title_1=$TITLE_1" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
          &lt;span class="s"&gt;echo "url_1=$URL_1" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
          &lt;span class="s"&gt;echo "date_1=$DATE_1" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;

          &lt;span class="s"&gt;echo "title_2=$TITLE_2" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
          &lt;span class="s"&gt;echo "url_2=$URL_2" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
          &lt;span class="s"&gt;echo "date_2=$DATE_2" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;

          &lt;span class="s"&gt;echo "title_3=$TITLE_3" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
          &lt;span class="s"&gt;echo "url_3=$URL_3" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
          &lt;span class="s"&gt;echo "date_3=$DATE_3" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Update README&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;README_FILE="./README.md"&lt;/span&gt;
          &lt;span class="s"&gt;# Build the new content for the latest 3 posts&lt;/span&gt;
          &lt;span class="s"&gt;NEW_CONTENT="- [${{ env.title_1 }}](${{ env.url_1 }}) - Published on ${{ env.date_1 }}\n\- [${{ env.title_2 }}](${{ env.url_2 }}) - Published on ${{ env.date_2 }}\n\- [${{ env.title_3 }}](${{ env.url_3 }}) - Published on ${{ env.date_3 }}"&lt;/span&gt;

          &lt;span class="s"&gt;# Replace the placeholder section using a different delimiter (|)&lt;/span&gt;
          &lt;span class="s"&gt;sed -i '/&amp;lt;!-- LATEST_DEVTO_POST --&amp;gt;/{n;N;N;s|.*|'"$NEW_CONTENT"'|}' $README_FILE&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Commit Changes&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;git config --local user.name "github-actions[bot]"&lt;/span&gt;
          &lt;span class="s"&gt;git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"&lt;/span&gt;
          &lt;span class="s"&gt;git add README.md --force&lt;/span&gt;
          &lt;span class="s"&gt;git commit -m "Update README with latest 3 Dev.to posts" || echo "No changes to commit"&lt;/span&gt;
          &lt;span class="s"&gt;git push&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in your README:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- ARTICLES_START --&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;!-- ARTICLES_END --&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub Actions replaces everything between those two markers every time it runs.&lt;/p&gt;

&lt;p&gt;Your profile stays updated. You don’t touch anything.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Else Can You Automate With GitHub Actions?
&lt;/h2&gt;

&lt;p&gt;Once you get comfortable with this, you’ll realize GitHub Actions can automate pretty much anything you can script. Here are some actually useful examples:&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Automatically Add Your Latest YouTube Videos
&lt;/h3&gt;

&lt;p&gt;If you upload tutorials or dev content, this keeps your profile fresh.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Fetch YouTube videos&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
    &lt;span class="s"&gt;curl -s "https://www.googleapis.com/youtube/v3/search?key=$YT_API_KEY&amp;amp;channelId=$CHANNEL_ID&amp;amp;part=snippet&amp;amp;order=date&amp;amp;maxResults=5" &amp;gt; videos.json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. Auto-Update GitHub Stats or Badges
&lt;/h3&gt;

&lt;p&gt;You can show live data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;follower count&lt;/li&gt;
&lt;li&gt;stars&lt;/li&gt;
&lt;li&gt;views&lt;/li&gt;
&lt;li&gt;repo metrics
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;curl -s https://api.github.com/users/YOUR_USER | jq '.followers'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then embed that into badges or markdown.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Use GitHub Actions as a Free Cron Server
&lt;/h3&gt;

&lt;p&gt;Run scheduled tasks without having any server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;schedule&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cron&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*/6&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*"&lt;/span&gt;  &lt;span class="c1"&gt;# Every 6 hours&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cleaning old branches&lt;/li&gt;
&lt;li&gt;updating analytics&lt;/li&gt;
&lt;li&gt;regenerating documentation&lt;/li&gt;
&lt;li&gt;syncing data&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4. Create Your Own Personal Automation Bots
&lt;/h3&gt;

&lt;p&gt;This part is way more fun than it sounds.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get a Telegram/Discord ping when someone stars your repo&lt;/li&gt;
&lt;li&gt;generate daily GitHub activity summaries&lt;/li&gt;
&lt;li&gt;sync posts from X/Twitter or LinkedIn&lt;/li&gt;
&lt;li&gt;rebuild your portfolio site every night&lt;/li&gt;
&lt;li&gt;generate OG images&lt;/li&gt;
&lt;li&gt;track metrics for projects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can script it, GitHub Actions can run it for free.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Simple Automation Template You Can Reuse
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;My Automation&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;schedule&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cron&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*"&lt;/span&gt;
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;run-script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3 script.py&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Drop your logic into &lt;code&gt;script.py&lt;/code&gt; and you’re done.&lt;/p&gt;




&lt;p&gt;If you’ve been meaning to automate your own workflows or make your profile more dynamic, this is a great place to start. Try it, break it, tweak it, and make it your own hand✌️.&lt;/p&gt;

</description>
      <category>github</category>
      <category>contentwriting</category>
      <category>automation</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Optimizing Performance in Next.js and React.js: Best Practices and Strategies</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Tue, 25 Feb 2025 11:24:23 +0000</pubDate>
      <link>https://dev.to/bhargab/optimizing-performance-in-nextjs-and-reactjs-best-practices-and-strategies-1j2a</link>
      <guid>https://dev.to/bhargab/optimizing-performance-in-nextjs-and-reactjs-best-practices-and-strategies-1j2a</guid>
      <description>&lt;p&gt;Performance optimization forms the backbone of great web experiences, and it’s a critical focus when building with React.js and Next.js. A fast app keeps users satisfied, reduces load times, enhances SEO, and scales effectively. Drawing from practical projects, this guide explores detailed strategies to maximize performance in both frameworks, blending actionable tips with lessons learned from real-world applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  0. Table of Contents
&lt;/h2&gt;

&lt;p&gt;To navigate directly to a specific optimization strategy, use the links below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. Identifying Performance Bottlenecks&lt;/li&gt;
&lt;li&gt;2. Image Optimization&lt;/li&gt;
&lt;li&gt;3. Code Splitting and Tree Shaking&lt;/li&gt;
&lt;li&gt;4. Server-Side and Client-Side Caching&lt;/li&gt;
&lt;li&gt;5. Load Balancing for Scalability&lt;/li&gt;
&lt;li&gt;6. Lazy Loading and Intersection Observer&lt;/li&gt;
&lt;li&gt;7. Preventing Unnecessary Re-Renders and Managing State Efficiently&lt;/li&gt;
&lt;li&gt;8. Server-Side Performance Optimization&lt;/li&gt;
&lt;li&gt;9. Purging CSS&lt;/li&gt;
&lt;li&gt;10. Inlining Critical CSS&lt;/li&gt;
&lt;li&gt;11. Optimizing Fonts and Third-Party Scripts&lt;/li&gt;
&lt;li&gt;12. Optimizing Third-Party API Calls&lt;/li&gt;
&lt;li&gt;13. Offloading Third-Party Scripts with Partytown&lt;/li&gt;
&lt;li&gt;14. Using Next Bundle Analyzer to Identify Bundle Size Issues&lt;/li&gt;
&lt;li&gt;15. Rendering Huge Lists with Virtual Lists and Infinite Scroll&lt;/li&gt;
&lt;li&gt;16. Continuous Monitoring and Performance Improvements&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1. Identifying Performance Bottlenecks
&lt;/h2&gt;

&lt;p&gt;Optimization begins with pinpointing issues. Tools like &lt;strong&gt;Google PageSpeed Insights&lt;/strong&gt; and &lt;strong&gt;Lighthouse&lt;/strong&gt;, prove invaluable for assessing metrics such as Largest Contentful Paint (LCP)—the time until main content appears—Cumulative Layout Shift (CLS)—how much the page shifts during loading—and Time to First Byte (TTFB)—the server’s response speed. For React.js, the &lt;strong&gt;React Developer Tools Profiler&lt;/strong&gt; highlights components that re-render excessively or load slowly. Skipping this step once led to hours wasted on misdirected efforts—profiling first has since become a non-negotiable starting point.&lt;/p&gt;




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

&lt;p&gt;Images often hinder performance but offer significant optimization potential. Here’s how they’re managed effectively:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: The &lt;code&gt;next/image&lt;/code&gt; component streamlines the process by resizing images, converting them to efficient formats like WebP, and lazy-loading them by default. For above-the-fold images, such as homepage banners, setting &lt;code&gt;loading="eager"&lt;/code&gt; and &lt;code&gt;fetchpriority="high"&lt;/code&gt; prioritizes them for LCP. Here’s an example from a recent project:
&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="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="s2"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;HeroSection&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="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;"/banner.jpg"&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;1200&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;600&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;fetchpriority&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"high"&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;"Main banner"&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: Without Next.js’s built-in tools, libraries like &lt;code&gt;react-lazy-load-image-component&lt;/code&gt; or manual &lt;code&gt;loading="lazy"&lt;/code&gt; attributes on &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tags handle the task. It’s less automated but effective.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extras&lt;/strong&gt;: Serving images via a CDN (e.g., Cloudflare) speeds up delivery. Modern formats like WebP reduce file sizes dramatically—transforming a 2MB PNG into 200KB proved this point. Skeleton screens or blurred placeholders (auto-generated in Next.js with &lt;code&gt;blurDataURL&lt;/code&gt;) prevent CLS, enhancing the loading experience noticeably.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Code Splitting and Tree Shaking
&lt;/h2&gt;

&lt;p&gt;Large JavaScript bundles silently degrade performance, but modern bundlers like Webpack mitigate this with code splitting and tree shaking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt; enable on-demand component loading. In a dashboard app, lazy-loading a chart component not visible on initial load cut the main bundle by 300KB:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Chart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Chart&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Dashboard&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="nc"&gt;Suspense&lt;/span&gt; &lt;span class="na"&gt;fallback&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;Loading chart...&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="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Chart&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;Suspense&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: &lt;code&gt;next/dynamic&lt;/code&gt; offers similar benefits, often with SSR disabled for client-heavy components. For example:
&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="nx"&gt;dynamic&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/dynamic&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="nb"&gt;Map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/Map&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;ssr&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next.js also splits code by page automatically, a boon for multi-page apps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tree Shaking&lt;/strong&gt;: Modern bundlers like Webpack enable tree shaking by default in production mode, eliminating unused code. Using named imports (e.g., &lt;code&gt;import { useEffect } from 'react'&lt;/code&gt;) instead of &lt;code&gt;import *&lt;/code&gt; maximizes this feature. A bloated bundle from a library’s &lt;code&gt;import *as&lt;/code&gt; once ballooned a build—switching to specific imports let Webpack trim 150KB of dead code.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Server-Side and Client-Side Caching
&lt;/h2&gt;

&lt;p&gt;Caching eliminates redundant work, boosting efficiency:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: &lt;code&gt;getStaticProps&lt;/code&gt; pre-renders static pages (e.g., blogs), while &lt;code&gt;getServerSideProps&lt;/code&gt; handles dynamic content (e.g., user profiles). Incremental Static Regeneration (ISR) balances static efficiency with freshness, updating pages in the background. An e-commerce site used this to refresh product listings every 10 seconds without full rebuilds:
&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;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticProps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchProducts&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;products&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;10&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: Client-side caching with SWR or React Query keeps API calls lightweight and data current:
&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="nx"&gt;useSWR&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;swr&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;fetcher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="p"&gt;)&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="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="kd"&gt;const&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSWR&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/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fetcher&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;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;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;name&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Extras&lt;/strong&gt;: Static assets cached on a CDN with &lt;code&gt;Cache-Control: max-age=31536000&lt;/code&gt; and service workers for offline support dropped one app’s load time from 2 seconds to under 1 second.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Load Balancing for Scalability
&lt;/h2&gt;

&lt;p&gt;Load balancing ensures stability during traffic spikes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: Deploying with Nginx as a reverse proxy distributes requests across servers. Vercel’s auto-scaling handled a flash sale seamlessly with zero downtime. Stateful apps (e.g., chat features) benefit from sticky sessions to maintain user context across servers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: As a client-side framework, this applies more to hosting or backend setups, where platforms like Netlify or Cloudflare balance static assets automatically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lesson&lt;/strong&gt;: Neglecting this once crashed a site during a surge—planning for scale is now standard practice.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Lazy Loading and Intersection Observer
&lt;/h2&gt;

&lt;p&gt;Lazy loading minimizes initial load overhead:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: The Intersection Observer API loads content as it enters the viewport. An infinite scroll feature built this way runs smoothly:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isVisible&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsVisible&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;false&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;ref&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;observer&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;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;entry&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;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isIntersecting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setIsVisible&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="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&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="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ref&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="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="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ref&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;isVisible&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;BigVideo&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Skeleton&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;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: Pairing &lt;code&gt;next/dynamic&lt;/code&gt; with Intersection Observer defers heavy components, like footers, until needed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Preventing Unnecessary Re-Renders and Managing State Efficiently
&lt;/h2&gt;

&lt;p&gt;Excessive re-renders slow apps, but they’re manageable with the right techniques and state management choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;React.js&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;React.memo&lt;/code&gt; prevents re-renders unless props change, halving render time for a list item component:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ListItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;text&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&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;text&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;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useMemo&lt;/code&gt; and &lt;code&gt;useCallback&lt;/code&gt; optimize costly operations. A filtering app avoided lag by memoizing a sorted array:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;),&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleFilter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&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;applyFilter&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;ul&gt;
&lt;li&gt;Avoiding inline JSX functions (e.g., &lt;code&gt;&amp;lt;button onClick={() =&amp;gt; foo()}&amp;gt;&lt;/code&gt;) prevents unnecessary recreations that disrupt child components.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Next.js&lt;/strong&gt;: These techniques carry over, with SSR and SSG offloading some rendering from the client, reducing client-side re-render pressure.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;State Management&lt;/strong&gt;: Context suits small apps due to its simplicity, but larger Next.js projects benefit from Zustand or Redux for performance and scalability. A key reason to prefer Redux over &lt;code&gt;useContext&lt;/code&gt; lies in re-rendering behavior: with &lt;code&gt;useContext&lt;/code&gt;, every component consuming the context re-renders whenever any part of the context value changes, even if it doesn’t use the updated state. For example, updating a single user property in a context triggers re-renders across all consumers:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&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;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;NameDisplay&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;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Re-renders on any context change&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;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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redux, by contrast, uses a store with selectors (e.g., via &lt;code&gt;reselect&lt;/code&gt;) to ensure components only re-render when their specific slice of state changes:&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;selectUserName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&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;state&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;NameDisplay&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="nf"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selectUserName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Only re-renders if name changes&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;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;name&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This granularity avoids unnecessary re-renders, critical in complex apps with frequent state updates. Normalizing state (e.g., flat objects) further enhances efficiency. Switching from Context to Zustand or Redux once transformed a sluggish UI into a responsive one by eliminating wasteful renders.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Server-Side Performance Optimization
&lt;/h2&gt;

&lt;p&gt;Server-side enhancements accelerate Next.js apps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load balancers (noted above) pair with optimized database queries—Prisma avoids N+1 issues, cutting TTFB from 500ms to 100ms in one case.&lt;/li&gt;
&lt;li&gt;HTTP/2 or HTTP/3, enabled via Nginx, speeds up asset delivery.&lt;/li&gt;
&lt;li&gt;Gzip or Brotli compression reduced a docs site’s payload from 1MB to 300KB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: This applies more to API servers, where lean fetch calls maintain client-side efficiency.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  9. Purging CSS
&lt;/h2&gt;

&lt;p&gt;CSS bloat quietly undermines performance, making purging essential:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: Tailwind CSS’s built-in purging (via &lt;code&gt;purgecss&lt;/code&gt;) strips unused classes at build time. Configuring &lt;code&gt;tailwind.config.js&lt;/code&gt; to scan relevant files cut a portfolio site’s CSS from 500KB to 20KB:
&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;content&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;./pages/**/*.{js,ts,jsx,tsx}&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;./components/**/*.{js,ts,jsx,tsx}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;extend&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;plugins&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: Integrating &lt;strong&gt;PurgeCSS&lt;/strong&gt; or &lt;strong&gt;cssnano&lt;/strong&gt; into Webpack works well:
&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;PurgeCSSPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;purgecss-webpack-plugin&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;glob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;glob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&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;PurgeCSSPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sync&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;__dirname&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/src/**/*`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;nodir&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="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Impact&lt;/strong&gt;: Unpurged CSS from libraries once bloated a React app with 1MB of unused Bootstrap styles—purging became a must after that.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  10. Inlining Critical CSS
&lt;/h2&gt;

&lt;p&gt;Inlining critical CSS—styles for above-the-fold content—accelerates first paint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: Tools like &lt;strong&gt;Critical&lt;/strong&gt; or &lt;strong&gt;Penthouse&lt;/strong&gt; extract critical CSS for inlining in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;. Running Critical in the build process:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npx critical pages/index.html &lt;span class="nt"&gt;--base&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;--inline&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; pages/index-critical.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then injecting it with &lt;code&gt;&amp;lt;Head&amp;gt;&lt;/code&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;import&lt;/span&gt; &lt;span class="nx"&gt;Head&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/head&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;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Head&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;style&lt;/span&gt; &lt;span class="na"&gt;dangerouslySetInnerHTML&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="na"&gt;__html&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`body { margin: 0; } .hero { font-size: 2rem; }`&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="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Head&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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hero"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome!&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;/&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;Non-critical CSS loads async via &lt;code&gt;&amp;lt;link rel="stylesheet"&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: The &lt;strong&gt;critical-css-webpack-plugin&lt;/strong&gt; automates this in Webpack:
&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;CriticalCssPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;critical-css-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&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;CriticalCssPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;index.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;inline&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Approach&lt;/strong&gt;: Lighthouse identifies critical rendering path blockers, then Critical or Penthouse extracts styles for headers or hero sections. Inlining 2KB of critical CSS on a landing page dropped FCP from 1.5s to 0.8s, deferring the rest.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  11. Optimizing Fonts and Third-Party Scripts
&lt;/h2&gt;

&lt;p&gt;Fonts and scripts subtly impact performance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: &lt;code&gt;next/font&lt;/code&gt; hosts fonts locally, with &lt;code&gt;font-display: swap&lt;/code&gt; ensuring text visibility during load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: Self-hosting critical fonts with &lt;code&gt;&amp;lt;link rel="preload"&amp;gt;&lt;/code&gt; and deferring others avoids blocking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scripts&lt;/strong&gt;: Third-party tools (e.g., analytics) use &lt;code&gt;async&lt;/code&gt; or &lt;code&gt;defer&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"tracking.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deferring a chat widget once shaved off a 1-second delay.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. Optimizing Third-Party API Calls
&lt;/h2&gt;

&lt;p&gt;Third-party APIs can bottleneck apps if mishandled:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: Batching requests with &lt;code&gt;axios&lt;/code&gt; or &lt;code&gt;Promise.all&lt;/code&gt; and debouncing rapid calls (e.g., autocomplete) conserve resources:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&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;let&lt;/span&gt; &lt;span class="nx"&gt;timeout&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="nx"&gt;args&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;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&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;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchSuggestions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;debounce&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;query&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="s2"&gt;`/api/suggestions?q=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;query&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="nf"&gt;setSuggestions&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;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="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: Pre-fetching in &lt;code&gt;getStaticProps&lt;/code&gt; or &lt;code&gt;getServerSideProps&lt;/code&gt; bakes data into pages, while SWR handles client-side updates:
&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="nx"&gt;useSWR&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;swr&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;LiveData&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;data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSWR&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/live&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fetcher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;refreshInterval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&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;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;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;value&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Takeaway&lt;/strong&gt;: Setting 5-second timeouts on fetches and falling back to cached data when APIs fail halved one app’s load time after over-fetching issues surfaced.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  13. Offloading Third-Party Scripts with Partytown
&lt;/h2&gt;

&lt;p&gt;Third-party scripts like analytics or ads often bog down the main thread, but Partytown offloads them to web workers for better performance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js with App Router&lt;/strong&gt;: Introduced in Next.js 12.1 as an experimental feature, the &lt;code&gt;next/script&lt;/code&gt; component’s &lt;code&gt;strategy="worker"&lt;/code&gt; uses Partytown to run scripts in a web worker. Enable it in &lt;code&gt;next.config.js&lt;/code&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;experimental&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;nextScriptWorkers&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;Then, in a layout or page:&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="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="s2"&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;RootLayout&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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;html&lt;/span&gt; &lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"en"&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;body&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;children&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;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://www.googletagmanager.com/gtag/js?id=G-XXXX"&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;"worker"&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;body&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;html&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;Note: As of February 22, 2025, this remains experimental and unsupported in the Next.js 13+ App Router due to stability issues. Testing revealed Google Analytics offloading worked but took time to register hits, suggesting patience or alternative strategies like &lt;code&gt;lazyOnload&lt;/code&gt; for now.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js&lt;/strong&gt;: Partytown integrates manually via &lt;code&gt;@builder.io/partytown/react&lt;/code&gt;. Install it (&lt;code&gt;npm install @builder.io/partytown&lt;/code&gt;), then add scripts with &lt;code&gt;type="text/partytown"&lt;/code&gt; and the Partytown component:
&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;Partytown&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;@builder.io/partytown/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="nt"&gt;head&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;script&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text/partytown"&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="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;script&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;Partytown&lt;/span&gt; &lt;span class="na"&gt;forward&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dataLayer.push&lt;/span&gt;&lt;span class="dl"&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;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;head&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;My App&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;/&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;Copy Partytown’s worker files to &lt;code&gt;public/~partytown&lt;/code&gt; for serving.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Benefits and Caveats&lt;/strong&gt;: Offloading frees the main thread, improving responsiveness—Lighthouse scores jumped from 69 to 85 in one test after moving Google Tag Manager to a worker. However, not all scripts (e.g., those needing synchronous DOM access) work seamlessly. Partytown’s trade-offs documentation advises proxying requests for CORS issues, a common snag with some APIs.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  14. Using Next Bundle Analyzer to Identify Bundle Size Issues
&lt;/h2&gt;

&lt;p&gt;Large bundles can silently inflate load times, and Next Bundle Analyzer provides clarity on what’s taking up space in Next.js apps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Setup&lt;/strong&gt;: Install &lt;code&gt;@next/bundle-analyzer&lt;/code&gt; (&lt;code&gt;npm install @next/bundle-analyzer&lt;/code&gt;) and configure it in &lt;code&gt;next.config.js&lt;/code&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withBundleAnalyzer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@next/bundle-analyzer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)({&lt;/span&gt;
    &lt;span class="na"&gt;enabled&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;ANALYZE&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&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;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withBundleAnalyzer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="c1"&gt;// Other Next.js config options&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it with &lt;code&gt;ANALYZE=true next build&lt;/code&gt; to generate a visual report.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Usage&lt;/strong&gt;: After building, the analyzer opens a browser window with an interactive treemap, showing bundle sizes for pages, components, and dependencies. Each block’s size and color indicate its contribution—larger, darker blocks highlight heavy modules. For example, a bloated third-party library like &lt;code&gt;moment.js&lt;/code&gt; might dominate the treemap, signaling a need to switch to a lighter alternative like &lt;code&gt;day.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React.js Context&lt;/strong&gt;: While Next Bundle Analyzer is Next.js-specific, React.js projects can use &lt;code&gt;webpack-bundle-analyzer&lt;/code&gt; similarly. Add it to Webpack config:
&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;BundleAnalyzerPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webpack-bundle-analyzer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;BundleAnalyzerPlugin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&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;BundleAnalyzerPlugin&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;Running the build generates a comparable report.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Practical Impact&lt;/strong&gt;: Analyzing one Next.js app revealed a 1.2MB bundle from an unused charting library imported across pages—lazy-loading it with &lt;code&gt;next/dynamic&lt;/code&gt; slashed the initial load by 800KB. Regularly checking bundle sizes ensures optimization efforts target the biggest culprits, like oversized dependencies or unminified code.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  15. Rendering Huge Lists with Virtual Lists and Infinite Scroll
&lt;/h2&gt;

&lt;p&gt;Rendering large datasets, like thousands of items, can cripple performance, but virtual lists and infinite scroll optimize this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React.js with Virtual Lists&lt;/strong&gt;: Libraries like &lt;code&gt;react-virtualized&lt;/code&gt; or &lt;code&gt;react-window&lt;/code&gt; render only visible items in the viewport, not the entire list. For a 10,000-item list, &lt;code&gt;react-window&lt;/code&gt; keeps DOM nodes minimal:
&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;FixedSizeList&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-window&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;Row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;style&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Item &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;VirtualList&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="nc"&gt;FixedSizeList&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;400&lt;/span&gt;&lt;span class="si"&gt;}&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;itemCount&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;itemSize&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;35&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;Row&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;FixedSizeList&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 approach dropped render time from seconds to milliseconds in a data-heavy app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Infinite Scroll&lt;/strong&gt;: Building on Intersection Observer (see #6), infinite scroll fetches more items as users scroll. Here’s an example fetching 20 items at a time:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setItems&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;page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPage&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;1&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;loader&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;observer&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;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entries&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;entries&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;isIntersecting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;setPage&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="mi"&gt;1&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="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.0&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;loader&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="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loader&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="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="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&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="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="s2"&gt;`/api/items?page=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;page&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="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="p"&gt;)&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="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="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setItems&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="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="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="p"&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;page&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;items&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;item&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="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;i&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;item&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="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="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;loader&lt;/span&gt;&lt;span class="si"&gt;}&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;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;div&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt;: Combine &lt;code&gt;next/dynamic&lt;/code&gt; with virtual lists for client-side rendering of large datasets, or pre-fetch initial items with &lt;code&gt;getStaticProps&lt;/code&gt;. Testing showed a 50,000-item list rendered smoothly with &lt;code&gt;react-virtualized&lt;/code&gt;, while infinite scroll kept server requests efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why It Matters&lt;/strong&gt;: Rendering all items naively spikes memory usage and slows scrolling—virtualization and infinite scroll ensure smooth performance even with massive datasets.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  16. Continuous Monitoring and Performance Improvements
&lt;/h2&gt;

&lt;p&gt;Optimization requires ongoing effort:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Weekly Lighthouse runs and Web Vitals logging catch regressions—CLS spikes once flagged an image issue.&lt;/li&gt;
&lt;li&gt;Google Analytics reveals navigation patterns, guiding rendering tweaks like preloading popular pages.&lt;/li&gt;
&lt;li&gt;Keeping Next.js and React updated leverages performance fixes—a minor update once shaved 100ms off load time effortlessly.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;From tackling slow React re-renders to leveraging Next.js’s SSR and ISR, optimization blends framework features with disciplined coding. Tools like &lt;code&gt;React.memo&lt;/code&gt;, &lt;code&gt;next/image&lt;/code&gt;, ISR, CSS purging, inlined critical CSS, Partytown offloading, bundle analysis, virtual lists, and API optimizations, paired with caching and monitoring, transform apps into efficient, user-friendly experiences. Applying these strategies in projects consistently yields noticeable improvements.&lt;/p&gt;




&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;This article will be continuously updated as new optimization techniques, tools, or insights emerge. Check back periodically for the latest strategies to keep Next.js and React.js applications performing at their peak.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>nextjs</category>
      <category>web</category>
    </item>
    <item>
      <title>The Intricacies of MongoDB Aggregation Pipeline: Challenges and Insights from Implementing It with Go</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Sat, 04 Jan 2025 10:21:56 +0000</pubDate>
      <link>https://dev.to/bhargab/the-intricacies-of-mongodb-aggregation-pipeline-challenges-and-insights-from-implementing-it-with-3go0</link>
      <guid>https://dev.to/bhargab/the-intricacies-of-mongodb-aggregation-pipeline-challenges-and-insights-from-implementing-it-with-3go0</guid>
      <description>&lt;p&gt;MongoDB’s aggregation pipeline is a powerful framework for data transformation and computation. It is especially valuable for developers working with NoSQL databases, offering unparalleled flexibility to handle complex data manipulation tasks. However, implementing this feature in a statically typed language like Go presents unique challenges. This article explores the aggregation pipeline's core functionality, underlying mechanics, and the challenges I faced while integrating it with Go. Along the way, I share solutions, recommendations, and practical insights to guide developers in similar scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Aggregation Pipeline
&lt;/h2&gt;

&lt;p&gt;MongoDB’s aggregation pipeline is designed to process data in stages, each performing a specific operation. By chaining these stages, developers can create highly complex queries. Some of the most commonly used stages include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$match&lt;/code&gt;&lt;/strong&gt;: Filters documents to include only those matching specified conditions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$group&lt;/code&gt;&lt;/strong&gt;: Aggregates data by a specified field, applying operations like sum, average, and count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$sort&lt;/code&gt;&lt;/strong&gt;: Orders documents by specified fields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$project&lt;/code&gt;&lt;/strong&gt;: Modifies the structure of documents, including or excluding fields as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;$lookup&lt;/code&gt;&lt;/strong&gt;: Performs a left outer join with another collection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These stages operate independently, enabling MongoDB to optimize execution through indexing and parallel processing. Understanding these components is crucial for crafting efficient queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Aggregation Pipeline Works Internally
&lt;/h2&gt;

&lt;p&gt;Internally, MongoDB’s aggregation pipeline relies on a systematic process to maximize efficiency:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Execution Plan Generation&lt;/strong&gt;: The pipeline is parsed into an optimized execution plan, leveraging indexes and reordering stages for efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sequential Data Flow&lt;/strong&gt;: Data passes through each stage sequentially, with the output of one stage feeding into the next.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimization Techniques&lt;/strong&gt;: MongoDB merges compatible stages and pushes operations like &lt;code&gt;$match&lt;/code&gt; and &lt;code&gt;$sort&lt;/code&gt; earlier to minimize processed data volume.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parallel Processing&lt;/strong&gt;: For large datasets, MongoDB distributes tasks across multiple threads, enhancing scalability.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By understanding these internal mechanisms, developers can design pipelines that efficiently leverage MongoDB’s processing capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of Implementing the Aggregation Pipeline with Go
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Schema-less Nature of MongoDB
&lt;/h3&gt;

&lt;p&gt;MongoDB’s flexible schema can complicate integration with Go, which relies on strict typing. Constructing dynamic aggregation stages in such an environment can be challenging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Using the &lt;code&gt;bson.M&lt;/code&gt; and &lt;code&gt;bson.D&lt;/code&gt; types from the MongoDB Go driver allowed dynamic construction of pipelines. However, careful validation was necessary to ensure consistency, as strict type safety was partially sacrificed.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Complex Query Construction
&lt;/h3&gt;

&lt;p&gt;Aggregation pipelines often involve deeply nested structures, making query construction cumbersome and error-prone in Go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Helper functions were created to encapsulate repetitive stages like &lt;code&gt;$group&lt;/code&gt;. This modular approach improved code readability and reduced the risk of errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Debugging and Error Handling
&lt;/h3&gt;

&lt;p&gt;Error messages from aggregation pipelines can be vague, making it difficult to identify issues in specific stages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Logging the JSON representation of pipelines and testing them in MongoDB Compass simplified debugging. Additionally, the Go driver’s error-wrapping features helped trace issues more effectively.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Performance Bottlenecks
&lt;/h3&gt;

&lt;p&gt;Stages like &lt;code&gt;$lookup&lt;/code&gt; and &lt;code&gt;$group&lt;/code&gt; are resource-intensive and can slow down performance, especially with large datasets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Using MongoDB’s &lt;code&gt;explain&lt;/code&gt; function helped pinpoint inefficiencies. Optimizing indexes, reordering stages, and introducing batch processing significantly improved performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Concurrency Management
&lt;/h3&gt;

&lt;p&gt;Running multiple aggregation queries simultaneously can strain resources, leading to latency and connection pool saturation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Adjusting connection pool parameters and implementing context-based timeouts ensured better resource management. Monitoring throughput allowed for dynamic scaling, preventing bottlenecks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recommendations for Efficient Usage
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run Aggregation Pipelines in Cron Jobs&lt;/strong&gt;: Aggregation pipelines are resource-intensive and can impact real-time services. Scheduling them as separate cron jobs ensures better system stability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define Indexes Clearly&lt;/strong&gt;: Carefully choose which fields to index to optimize performance. Regularly review query patterns and adjust indexes as needed to reduce execution time.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Leverage Debugging Tools
&lt;/h3&gt;

&lt;p&gt;Tools like MongoDB Compass and the &lt;code&gt;explain&lt;/code&gt; function are invaluable for visualizing query execution plans and identifying bottlenecks.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Optimize Pipeline Order
&lt;/h3&gt;

&lt;p&gt;Place filtering and sorting stages like &lt;code&gt;$match&lt;/code&gt; and &lt;code&gt;$sort&lt;/code&gt; early in the pipeline to minimize data volume processed by subsequent stages.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Encapsulate Pipeline Logic
&lt;/h3&gt;

&lt;p&gt;Modularizing commonly used pipeline stages into reusable components simplifies maintenance and reduces duplication.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Monitor System Resources
&lt;/h3&gt;

&lt;p&gt;Regularly track connection pool usage, query execution times, and overall system performance. Implement resource thresholds and alerts to avoid service disruptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Thoughts 💭
&lt;/h2&gt;

&lt;p&gt;Integrating MongoDB’s aggregation pipeline with Go is both challenging and rewarding. The combination of MongoDB’s dynamic schema and Go’s strict typing requires thoughtful planning and problem-solving. By understanding the pipeline’s mechanics and applying best practices, developers can overcome these challenges to achieve scalable, efficient solutions.&lt;/p&gt;

</description>
      <category>go</category>
      <category>mongodb</category>
      <category>backend</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Art of Prefetching and Preloading: Enhancing Web Performance</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Mon, 30 Dec 2024 16:08:54 +0000</pubDate>
      <link>https://dev.to/bhargab/the-art-of-prefetching-and-preloading-enhancing-web-performance-2ef8</link>
      <guid>https://dev.to/bhargab/the-art-of-prefetching-and-preloading-enhancing-web-performance-2ef8</guid>
      <description>&lt;p&gt;In today’s fast-paced digital world, speed is everything. A slow-loading website doesn’t just frustrate users—it costs you engagement, conversions, and credibility. Enter &lt;strong&gt;prefetching&lt;/strong&gt; and &lt;strong&gt;preloading&lt;/strong&gt;, two often-underappreciated techniques that can elevate your web performance from "meh" to "wow."  &lt;/p&gt;

&lt;p&gt;But what are these magic tricks, and how can you wield them effectively? Let’s break them down.  &lt;/p&gt;




&lt;h3&gt;
  
  
  🎯 Understanding Prefetching and Preloading
&lt;/h3&gt;

&lt;p&gt;At a high level, both techniques help browsers fetch resources &lt;strong&gt;before&lt;/strong&gt; they’re explicitly needed, but their purposes differ:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prefetching&lt;/strong&gt;: Think of it as a browser’s crystal ball. It predicts which resources you might need next (like a linked page) and loads them in the background.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preloading&lt;/strong&gt;: This focuses on resources needed &lt;em&gt;right now&lt;/em&gt; (like fonts, images, or critical scripts) and ensures they’re prioritized during page rendering.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s a simple analogy:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefetching is like prepping ingredients for tomorrow’s meal.
&lt;/li&gt;
&lt;li&gt;Preloading is making sure the stove is hot before you start cooking today’s dinner.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🚀 Why You Should Care
&lt;/h3&gt;

&lt;p&gt;Your site’s performance isn’t just about speed—it’s about the &lt;strong&gt;perception of speed&lt;/strong&gt;. Prefetching and preloading make your website feel faster, even when the total load time stays the same.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits include:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster navigation and rendering.
&lt;/li&gt;
&lt;li&gt;Reduced latency for future interactions.
&lt;/li&gt;
&lt;li&gt;Improved user experience, especially on slower networks.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔧 How to Implement Prefetching
&lt;/h3&gt;

&lt;p&gt;Prefetching is all about anticipating user behavior. You can use it to pre-load assets like the next page’s HTML, CSS, and JavaScript.  &lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"prefetch"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"next-page.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;The browser fetches &lt;code&gt;next-page.html&lt;/code&gt; in the background.
&lt;/li&gt;
&lt;li&gt;When the user clicks the link, the page is loaded almost instantly.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pro Tips for Prefetching&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Focus on High-Probability Actions&lt;/strong&gt;: Use analytics to predict the user’s next step. For instance, e-commerce sites can prefetch product details after users browse categories.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;rel="prerender"&lt;/code&gt; for Entire Pages&lt;/strong&gt;: When confident, use prerendering to fully load the next page in a hidden tab.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;   &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"prerender"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"checkout.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Overloading the Network&lt;/strong&gt;: Fetching too many resources can hurt overall performance.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔥 Mastering Preloading
&lt;/h3&gt;

&lt;p&gt;Preloading helps you prioritize &lt;strong&gt;essential resources&lt;/strong&gt; for the current page.  &lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"styles.css"&lt;/span&gt; &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"style"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;as&lt;/code&gt; attribute specifies the type of resource (e.g., &lt;code&gt;style&lt;/code&gt;, &lt;code&gt;script&lt;/code&gt;, &lt;code&gt;font&lt;/code&gt;), so the browser can handle it efficiently.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use Preloading&lt;/strong&gt;  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Critical Fonts&lt;/strong&gt;: Avoid the dreaded “flash of invisible text” (FOIT) by preloading your web fonts.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;   &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"my-font.woff2"&lt;/span&gt; &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Above-the-Fold Images&lt;/strong&gt;: Ensure the hero image is visible as soon as the page loads.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Scripts&lt;/strong&gt;: For JavaScript critical to page interactivity.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  🛠 Best Practices for Both
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Monitor Resource Usage&lt;/strong&gt;: Tools like Chrome DevTools and Lighthouse can help identify what to preload or prefetch.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combine with Frameworks&lt;/strong&gt;: If you’re using React or Next.js, these frameworks often have built-in support for prefetching.
&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="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;prefetch&lt;/span&gt;&lt;span class="o"&gt;=&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;About&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Test and Iterate&lt;/strong&gt;: Not every resource needs prefetching or preloading. A/B test to find the sweet spot.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🧪 Case Study: Boosting Navigation Speed
&lt;/h3&gt;

&lt;p&gt;Let’s say you’re running an e-commerce site:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;strong&gt;prefetching&lt;/strong&gt; to load product pages as users hover over links in the product grid.
&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;preloading&lt;/strong&gt; to prioritize the checkout button’s JavaScript and styles.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Result&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users experience near-instant page transitions.
&lt;/li&gt;
&lt;li&gt;Cart abandonment rates drop because the checkout process feels seamless.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ⚙️ Tools to Measure Impact
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Google Lighthouse&lt;/strong&gt;: Evaluate preloading efficiency and identify unused resources.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebPageTest&lt;/strong&gt;: See how prefetching impacts real-world performance.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Tab in DevTools&lt;/strong&gt;: Check which resources are being preloaded or prefetched.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🎨 The Art of Balance
&lt;/h3&gt;

&lt;p&gt;Prefetching and preloading aren’t silver bullets. They require careful tuning to avoid bloated resource usage or negative impacts on slower connections.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use prefetching to anticipate user behavior strategically.
&lt;/li&gt;
&lt;li&gt;Apply preloading sparingly for critical assets.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When done right, these techniques can create a fast, frictionless experience that users love.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;The art of prefetching and preloading lies in understanding your users and optimizing their journey. It’s not just about technical finesse—it’s about delivering a digital experience that feels intuitive, smooth, and almost magical.  &lt;/p&gt;

&lt;p&gt;Now that you know the ropes, it’s time to put these techniques into action. Happy optimizing!  &lt;/p&gt;




&lt;p&gt;Let me know your thoughts or any questions in the comments. 👇  &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>webperf</category>
      <category>react</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Optimizing Docker Image Size for Go Applications</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Sat, 26 Oct 2024 09:29:16 +0000</pubDate>
      <link>https://dev.to/bhargab/reducing-docker-image-size-a-journey-of-discovery-2cd2</link>
      <guid>https://dev.to/bhargab/reducing-docker-image-size-a-journey-of-discovery-2cd2</guid>
      <description>&lt;p&gt;In the world of software development, I’m constantly trying to improve my applications. Recently, I faced a challenge: my Docker images for a Go application were bloated and unwieldy. This not only slowed down deployment times but also put unnecessary strain on my resources. Specifically, the size of the images was causing issues, as they filled the available disk space of my EC2 instance, which had a limit of 1GB. After optimization, the image size dramatically decreased from approximately 800MB (compressed) to just 7.95MB (compressed). Join me as I share my journey to trim down those images, the culprits I identified, and the valuable lessons I learned along the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Initial Dockerfile: The Culprit Unveiled
&lt;/h3&gt;

&lt;p&gt;At first glance, my Dockerfile seemed straightforward. Here’s how it looked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use the latest golang base image&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; golang:1.21.6&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy go.mod and go.sum&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; go.mod go.sum ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;go mod download

&lt;span class="c"&gt;# Copy the entire application code&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;# Build the Go application&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;go build &lt;span class="nt"&gt;-o&lt;/span&gt; main .

&lt;span class="c"&gt;# Expose port 4040&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 4040&lt;/span&gt;

&lt;span class="c"&gt;# Command to run the executable&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["./main"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While it worked, it became painfully clear that this approach led to massive image sizes. The inclusion of unnecessary files and the use of a bulky base image were the main offenders.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Unveiling: What Went Wrong?
&lt;/h3&gt;

&lt;p&gt;During my investigation, I identified several culprits contributing to my oversized Docker images:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;All Files, No Filters&lt;/strong&gt;: My initial setup copied everything from the project directory, including test files, documentation, and any stray files that had no business being in a production environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Heavyweight Base Image&lt;/strong&gt;: I was using the full Go image for both building and running my application. This included a ton of tools and libraries that I simply didn’t need in production.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Missing Multi-Stage Builds&lt;/strong&gt;: My Dockerfile didn’t leverage multi-stage builds, which allow you to build an application in one image and then use a minimal image for running it. This meant my final image included build dependencies and tools that were unnecessary for execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Transformation: Streamlining the Dockerfile
&lt;/h3&gt;

&lt;p&gt;To address these issues, I rolled up my sleeves and made some significant changes. Here’s the revamped Dockerfile that emerged from my efforts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Stage 1: Build the Go application&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;golang:1.21.6&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy go.mod and go.sum files&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; go.mod go.sum ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;go mod download

&lt;span class="c"&gt;# Copy the source code&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;# Build the Go application with optimizations&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nv"&gt;CGO_ENABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64 go build &lt;span class="nt"&gt;-ldflags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"-w -s"&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; main .

&lt;span class="c"&gt;# Stage 2: Create a minimal runtime image with CA certificates&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; alpine:latest&lt;/span&gt;

&lt;span class="c"&gt;# Install CA certificates&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apk add &lt;span class="nt"&gt;--no-cache&lt;/span&gt; ca-certificates

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy the binary from the builder stage&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /app/main .&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 4040&lt;/span&gt;

&lt;span class="c"&gt;# Command to run the executable&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["./main"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Key Changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-Stage Builds&lt;/strong&gt;: I split my Dockerfile into two stages. The first stage is dedicated to building the application, while the second stage uses a minimal base image (in this case, Alpine) to run it. This separation allowed me to leave behind all the unnecessary build tools and dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Selective Copying&lt;/strong&gt;: I started being selective about what files I copied into the image. This is where using a &lt;code&gt;.dockerignore&lt;/code&gt; file became invaluable. I ensured only the necessary files made it into my Docker image.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of a &lt;code&gt;.dockerignore&lt;/code&gt; file&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;# Ignore test files and directories
*.test
testdata/

# Ignore version control directories
.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Optimizing the Go Binary&lt;/strong&gt;: By setting &lt;code&gt;CGO_ENABLED=0&lt;/code&gt; and using build flags &lt;code&gt;-ldflags="-w -s"&lt;/code&gt;, I produced a smaller, statically linked binary.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why I Added CA Certificates
&lt;/h3&gt;

&lt;p&gt;In my final image, I installed CA certificates using the command &lt;code&gt;RUN apk add --no-cache ca-certificates&lt;/code&gt;. This step was crucial for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Secure Connections&lt;/strong&gt;: My application communicates with external services, including databases and APIs, that require secure connections over HTTPS. Installing CA certificates enables the Go application to verify the TLS/SSL certificates presented by these services, ensuring secure communication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimized Vulnerabilities&lt;/strong&gt;: Using CA certificates helps prevent man-in-the-middle attacks. By validating certificates, I can trust that the data exchanged with external services is secure and not being intercepted.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Understanding &lt;code&gt;CGO_ENABLED=0&lt;/code&gt; and Build Flags
&lt;/h3&gt;

&lt;p&gt;I included &lt;code&gt;CGO_ENABLED=0&lt;/code&gt; and the flags &lt;code&gt;-ldflags="-w -s"&lt;/code&gt; for several reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Static Binary Creation&lt;/strong&gt;: Setting &lt;code&gt;CGO_ENABLED=0&lt;/code&gt; tells the Go compiler to build a statically linked binary. This means the resulting executable does not rely on any shared libraries from the host system. This is particularly beneficial in a Docker environment where I may not have the same libraries available as I do on my local machine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Image Size&lt;/strong&gt;: A statically linked binary is generally smaller because it includes only the code and libraries necessary to run the application. This reduction in size is crucial when deploying to environments like Docker, where every byte counts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimized Performance&lt;/strong&gt;: The &lt;code&gt;-ldflags="-w -s"&lt;/code&gt; flags strip debug information and symbol tables from the binary. This not only reduces the binary size further but also enhances startup performance, which is beneficial in containerized environments where quick startup times are essential.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Lessons Learned: More Than Just Size
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Right Base Image Matters&lt;/strong&gt;: The choice of base image can make or break your Docker image size. Going with minimal images not only helps in reducing size but also enhances security by limiting the attack surface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Power of Multi-Stage Builds&lt;/strong&gt;: These builds are a game-changer. They help streamline the build process and keep the final image clean and lean.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Be Mindful of Your Copying&lt;/strong&gt;: Always scrutinize what you include in your Docker images. Using &lt;code&gt;.dockerignore&lt;/code&gt; helps keep your builds clean.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuous Optimization is Key&lt;/strong&gt;: Image optimization isn’t a one-time task. Regular reviews and improvements will help maintain a lean codebase and efficient images.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Through this journey, I successfully reduced my Docker image sizes, leading to faster deployments and improved resource efficiency. This experience taught me not just about the technical aspects of Docker but also reinforced the importance of mindful practices in software development.&lt;/p&gt;

&lt;p&gt;In the world of DevOps, every byte counts, and I'm excited to continue this journey of optimization and discovery!&lt;/p&gt;

</description>
      <category>go</category>
      <category>docker</category>
      <category>productivity</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Understanding Parallelism vs Concurrency in Go</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Wed, 09 Oct 2024 07:18:00 +0000</pubDate>
      <link>https://dev.to/bhargab/understanding-parallelism-vs-concurrency-in-go-47ch</link>
      <guid>https://dev.to/bhargab/understanding-parallelism-vs-concurrency-in-go-47ch</guid>
      <description>&lt;p&gt;When we talk about &lt;strong&gt;parallelism&lt;/strong&gt; and &lt;strong&gt;concurrency&lt;/strong&gt;, it's easy to confuse the two because they both involve executing multiple tasks at the same time. However, they are fundamentally different concepts with distinct applications, especially in Go, a language designed with these features at its core. Let's break down what each means, their differences, and how we can achieve them in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concurrency: Dealing with Lots of Tasks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Concurrency&lt;/strong&gt; is the ability to handle multiple tasks at once, even if they aren't being executed simultaneously. Think of it as juggling several tasks, but not necessarily completing them at the same time.&lt;/p&gt;

&lt;p&gt;Imagine you’re working on a report while also replying to emails. You aren't writing the report and typing emails simultaneously, but you’re switching back and forth between the two. This switching allows both tasks to make progress without waiting for one to be completed before starting the other. Similarly, concurrency in computing lets a system handle multiple operations or tasks by switching between them, giving the illusion of simultaneous execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Go&lt;/strong&gt;, concurrency is achieved using &lt;strong&gt;goroutines&lt;/strong&gt;. A goroutine is a lightweight thread of execution, allowing Go to run functions concurrently. For example, you can have multiple goroutines that make network calls, process data, or run other functions at the same time.&lt;/p&gt;

&lt;p&gt;Here's a simple example of concurrency in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"time"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;printNumbers&lt;/span&gt;&lt;span class="p"&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;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&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;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Millisecond&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;func&lt;/span&gt; &lt;span class="n"&gt;printLetters&lt;/span&gt;&lt;span class="p"&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;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="sc"&gt;'E'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&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;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Millisecond&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;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;printNumbers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// Run printNumbers concurrently&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;printLetters&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// Run printLetters concurrently&lt;/span&gt;

    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Wait to ensure both goroutines finish&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;printNumbers&lt;/code&gt; and &lt;code&gt;printLetters&lt;/code&gt; are run as goroutines, meaning they execute concurrently. The system alternates between running them, resulting in interleaved output, even though the two functions aren’t running at the exact same time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parallelism: Doing Multiple Things at Once
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Parallelism&lt;/strong&gt;, on the other hand, is about actually performing multiple tasks simultaneously. This only occurs when you have multiple processors or cores. Imagine you and a friend are both typing two different documents at the same time. That's parallelism—two tasks happening at the same time on different CPUs or cores.&lt;/p&gt;

&lt;p&gt;Parallelism is a subset of concurrency. All parallelism is concurrency, but not all concurrency is parallelism. Parallelism requires hardware that supports simultaneous task execution, whereas concurrency is more about structuring tasks to improve responsiveness and throughput, regardless of whether they run in parallel.&lt;/p&gt;

&lt;p&gt;In Go, parallelism is achieved when goroutines are distributed across multiple CPU cores. Go allows you to control the number of cores that can execute goroutines using the &lt;code&gt;GOMAXPROCS&lt;/code&gt; setting. By default, Go will run goroutines across as many CPU cores as are available, making it easy to take advantage of parallelism when the hardware allows it.&lt;/p&gt;

&lt;p&gt;Here’s an example of how you can use &lt;code&gt;GOMAXPROCS&lt;/code&gt; to enable parallelism in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"runtime"&lt;/span&gt;
    &lt;span class="s"&gt;"sync"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WaitGroup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Done&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Task %d is running&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&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;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GOMAXPROCS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Set the number of OS threads (cores) to use&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;wg&lt;/span&gt; &lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WaitGroup&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;task&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;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Run tasks concurrently and in parallel (if available)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;wg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// Wait for all tasks to complete&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the &lt;code&gt;GOMAXPROCS&lt;/code&gt; function sets the number of CPU cores available to run goroutines in parallel. With 4 cores, Go can execute up to 4 tasks simultaneously, fully utilizing the available hardware.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Differences Between Concurrency and Parallelism
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt; is about &lt;strong&gt;dealing with multiple tasks&lt;/strong&gt; at the same time, but not necessarily running them at the same instant. It’s more about structuring the program to make progress on many tasks by rapidly switching between them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallelism&lt;/strong&gt; is about &lt;strong&gt;executing multiple tasks simultaneously&lt;/strong&gt; on different processors or cores.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of concurrency as juggling many balls (tasks) by switching focus between them, while parallelism is juggling many balls by using multiple hands (cores).&lt;/p&gt;

&lt;h2&gt;
  
  
  Achieving Concurrency and Parallelism in Go
&lt;/h2&gt;

&lt;p&gt;Go makes both concurrency and parallelism simple to implement, thanks to its goroutine model and built-in support for multi-core systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt; is achieved by using &lt;strong&gt;goroutines&lt;/strong&gt; to run tasks independently without waiting for each to complete.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallelism&lt;/strong&gt; happens when these goroutines are distributed across multiple CPU cores, which Go handles automatically but can be fine-tuned using &lt;code&gt;GOMAXPROCS&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go's design philosophy emphasizes making concurrency easy to work with through its channels and goroutines, and it automatically optimizes for parallelism when the underlying hardware supports it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Life Examples of Concurrency and Parallelism
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web Server (Concurrency)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Imagine running an &lt;strong&gt;e-commerce website&lt;/strong&gt; like Amazon. Multiple users are browsing, placing orders, and checking out simultaneously. The web server handles each request concurrently, ensuring that no single user is blocked by another. The server can switch between handling requests for displaying product pages, adding items to the cart, or processing payment, without waiting for each to complete sequentially.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Video Streaming Platform (Parallelism)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In a video-sharing platform like &lt;strong&gt;YouTube&lt;/strong&gt;, users upload videos that need to be transcoded into multiple formats (1080p, 720p, etc.). Instead of processing each video sequentially, parallelism allows the platform to transcode several videos at the same time using multiple CPU cores. This speeds up processing, allowing quicker availability of videos for streaming.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat Application (Concurrency)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A &lt;strong&gt;real-time chat application&lt;/strong&gt; like WhatsApp needs to handle messages from many users concurrently. Each user sends and receives messages independently, so the system processes each user’s messages concurrently, ensuring a smooth real-time experience for everyone without messages being delayed or blocked by others.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Machine Learning (Parallelism)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In machine learning, training models on large datasets can take hours. By splitting the dataset into chunks, a system can use parallelism to train on multiple data chunks simultaneously across different CPU or GPU cores. This speeds up the training process, enabling faster model development for tasks like image recognition or recommendation engines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Traffic Management System (Concurrency)&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In a &lt;strong&gt;city traffic management system&lt;/strong&gt; where cameras at multiple intersections send updates to a central server, concurrency is used to process real-time data from multiple locations at once. The system can concurrently handle traffic signal data, vehicle counts, and accident reports, ensuring smooth city-wide traffic flow without delay in processing data from different intersections.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;In Go, concurrency is about managing multiple tasks and making progress on them, while parallelism takes that one step further by executing them simultaneously. Both are crucial for building efficient, scalable programs, and Go makes it incredibly easy to implement them with minimal overhead. Understanding the distinction allows developers to choose the right approach based on the problem they are solving—whether it’s to improve the responsiveness of an application with concurrency or to fully leverage CPU power with parallelism.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Playground:&lt;/strong&gt; &lt;a href="https://codesandbox.io/p/devbox/2qg64j" rel="noopener noreferrer"&gt;https://codesandbox.io/p/devbox/2qg64j&lt;/a&gt;&lt;/p&gt;

</description>
      <category>learning</category>
      <category>go</category>
      <category>performance</category>
      <category>parallelism</category>
    </item>
    <item>
      <title>Rollup.js Made Easy: A Step-by-Step Guide to Building and Publishing NPM Packages</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Wed, 04 Sep 2024 15:17:48 +0000</pubDate>
      <link>https://dev.to/bhargab/rollupjs-made-easy-a-step-by-step-guide-to-building-and-publishing-npm-packages-1c1k</link>
      <guid>https://dev.to/bhargab/rollupjs-made-easy-a-step-by-step-guide-to-building-and-publishing-npm-packages-1c1k</guid>
      <description>&lt;p&gt;Rollup.js is a fantastic tool for bundling JavaScript and TypeScript libraries, especially when you’re aiming for top-notch performance and smooth integration. Unlike other bundlers, Rollup focuses on creating smaller, optimized code by utilizing ES modules. If you’re looking to build efficient packages, Rollup is your go-to choice.&lt;/p&gt;

&lt;p&gt;In this post, we’ll explore Rollup’s key features, guide you through setting it up for package building, show you how to test your package locally, then publishing it to NPM. We’ll also highlight some essential plugins to enhance your build process, handle CSS files and images, and configure TypeScript for your project.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What is Rollup.js?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Rollup.js is a module bundler for JavaScript and TypeScript that compiles small pieces of code into something larger, like a library or application. It takes advantage of ES modules to streamline your code, removing unused elements and resulting in bundles that are smaller and faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Features of Rollup.js&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Tree-Shaking:&lt;/strong&gt; Automatically removes unused code, slimming down the final bundle and boosting efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ES Module Support:&lt;/strong&gt; Rollup uses ES module syntax (&lt;code&gt;import&lt;/code&gt; and &lt;code&gt;export&lt;/code&gt;), which is ideal for modern JavaScript development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plugins:&lt;/strong&gt; Easily extensible with plugins that add functionality, such as handling non-JS files or minifying output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Splitting:&lt;/strong&gt; Allows splitting of bundles, which is particularly useful for larger applications or libraries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output Formats:&lt;/strong&gt; Supports a variety of output formats like CommonJS, ES Modules, UMD, and IIFE, making it versatile for different project needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Optimizations:&lt;/strong&gt; Rollup produces highly optimized code with smaller bundle sizes, making it perfect for performance-critical applications.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Setting Up Rollup.js for Building Packages&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s dive into setting up Rollup.js to build a package with React.js and TypeScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Install Rollup and Basic Dependencies&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;First, let’s get Rollup and some essential plugins installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; rollup @rollup/plugin-node-resolve @rollup/plugin-commonjs @rollup/plugin-typescript rollup-plugin-peer-deps-external rollup-plugin-postcss tslib @rollup/plugin-image rollup-plugin-terser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what each plugin does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@rollup/plugin-node-resolve&lt;/code&gt;&lt;/strong&gt;: Helps Rollup find third-party modules in &lt;code&gt;node_modules&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@rollup/plugin-commonjs&lt;/code&gt;&lt;/strong&gt;: Converts CommonJS modules to ES modules so Rollup can handle them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@rollup/plugin-typescript&lt;/code&gt;&lt;/strong&gt;: Compiles TypeScript files seamlessly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rollup-plugin-peer-deps-external&lt;/code&gt;&lt;/strong&gt;: Automatically marks peer dependencies as external, keeping your bundle light.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rollup-plugin-postcss&lt;/code&gt;&lt;/strong&gt;: Processes and bundles CSS, which is crucial for handling styles in React components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@rollup/plugin-image&lt;/code&gt;&lt;/strong&gt;: Allows images to be imported directly into your components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rollup-plugin-terser&lt;/code&gt;&lt;/strong&gt;: Minifies the output, reducing file size and improving load times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;tslib&lt;/code&gt;&lt;/strong&gt;: This is a runtime library for TypeScript that contains all of the TypeScript helper functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;2. Setting Up TypeScript Configuration&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Create a &lt;code&gt;tsconfig.json&lt;/code&gt; in your project root to configure TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ES6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ESNext"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"jsx"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"declaration"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"declarationDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"rootDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"outDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./dist"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"strict"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"skipLibCheck"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"forceConsistentCasingInFileNames"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sourceMap"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"include"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/**/*"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exclude"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"node_modules"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration ensures your TypeScript code is compiled correctly and efficiently.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;3. Create a Basic Rollup Configuration&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Next, create a &lt;code&gt;rollup.config.js&lt;/code&gt; file in your project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;resolve&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;@rollup/plugin-node-resolve&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;commonjs&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;@rollup/plugin-commonjs&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;typescript&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;@rollup/plugin-typescript&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;peerDepsExternal&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;rollup-plugin-peer-deps-external&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;postcss&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;rollup-plugin-postcss&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;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;@rollup/plugin-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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;terser&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;rollup-plugin-terser&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src/index.ts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;output&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;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist/index.cjs.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;sourcemap&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="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist/index.esm.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;esm&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;sourcemap&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="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;peerDepsExternal&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;commonjs&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;typescript&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;tsconfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tsconfig.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;declaration&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="na"&gt;declarationDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;rootDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src&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;postcss&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;image&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;terser&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;external&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;react&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;react-dom&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;
  
  
  &lt;strong&gt;Building and Watching with Rollup&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To make your development process smoother, add these scripts to your &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rollup -c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"watch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rollup -c --watch"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can build your package or watch for changes and rebuild automatically during development.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Testing Your Rollup Package Locally&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before publishing, it’s essential to test your package locally. Two effective ways are &lt;code&gt;npm pack&lt;/code&gt; and &lt;code&gt;npm link&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Testing with &lt;code&gt;npm pack&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm pack&lt;/code&gt; simulates the npm install process, letting you test how your package would behave once published.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm pack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;This creates a &lt;code&gt;.tgz&lt;/code&gt; file in your project directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To test, install it in another project:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install&lt;/span&gt; ../path-to-your-package/your-package-name-version.tgz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. Testing with &lt;code&gt;npm link&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;npm link&lt;/code&gt; lets you link your package globally and use it across projects without publishing.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inside your package project, run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;link&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Then, in the project where you want to use the package, run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;link &lt;/span&gt;your-package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Handling CSS Files and Images in Rollup&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here’s how to handle CSS and images in your Rollup setup:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Handling CSS&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Using &lt;code&gt;rollup-plugin-postcss&lt;/code&gt;, you can process and bundle CSS files, even extracting them into separate files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;postcss&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;rollup-plugin-postcss&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;postcss&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;extract&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="na"&gt;minimize&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="na"&gt;modules&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="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;
  
  
  &lt;strong&gt;2. Handling Images&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;@rollup/plugin-image&lt;/code&gt; makes it easy to import images directly, and you can optimize them with additional plugins like &lt;code&gt;imagemin&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@rollup/plugin-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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;image&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="c1"&gt;// Add further image optimization here if needed&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;
  
  
  &lt;strong&gt;Publishing Your Package to npm&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;With Rollup set up, the final step is to publish your package on npm:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Prepare Your &lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Ensure that your package.json includes essential details such as name, version, main, and files. Additionally, always use &lt;a href="https://semver.org/" rel="noopener noreferrer"&gt;semantic versioning&lt;/a&gt; for versioning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"my-awesome-package"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;The&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;your&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;package&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Version&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;number&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"A description of what your package does."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/index.cjs.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Entry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;CommonJS&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/index.esm.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Entry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;ES&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Modules&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/types/index.d.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Entry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;TypeScript&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;declarations&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rollup -c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"watch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rollup -c --watch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"files"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Specify&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;which&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;files&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;npm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;package&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"dist"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"repository"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"git"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"git+https://github.com/your-username/your-repo.git"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"microfrontend"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rollup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"typescript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Your Name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MIT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"bugs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://github.com/your-username/your-repo/issues"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"homepage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://github.com/your-username/your-repo#readme"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react-dom"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.0.0"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@rollup/plugin-commonjs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^26.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@rollup/plugin-image"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^3.0.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@rollup/plugin-node-resolve"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^15.2.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@rollup/plugin-typescript"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^11.1.6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"rollup"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.79.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"rollup-plugin-peer-deps-external"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.2.4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"rollup-plugin-postcss"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.0.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"rollup-plugin-terser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.0.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"tslib"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.7.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"typescript"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.5.4"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. Log In and Publish to npm&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;If you’re not logged in, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, build your package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And publish it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm publish &lt;span class="nt"&gt;--access&lt;/span&gt; public
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your package is live on npm, ready for anyone to install with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;my-awesome-package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  GitHub Repository:
&lt;/h3&gt;

&lt;p&gt;To access the codebase, please visit the dedicated GitHub repository:&lt;/p&gt;

&lt;p&gt;GitHub Repository: &lt;a href="https://github.com/the-wrong-guy/create-your-own-package" rel="noopener noreferrer"&gt;create-your-own-package&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to clone the repository, explore the code, and use it as a reference for your own projects. If you encounter any issues or have questions, don't hesitate to open an issue on the repository. Your feedback and contributions are highly appreciated.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Rollup.js is a powerhouse for building efficient and optimized packages. Its modularity, plugin support, and ES module compatibility make it invaluable for developers. Whether you're crafting a simple package or tackling complex micro-frontends, Rollup.js provides the tools you need for streamlined, high-quality builds.&lt;/p&gt;

&lt;p&gt;You can apply the same configuration approach to build a &lt;a href="https://micro-frontends.org/" rel="noopener noreferrer"&gt;micro-frontend&lt;/a&gt;, leveraging Rollup's flexibility to create modular and reusable components. Just be sure to research the plugins you include to avoid unnecessary dependencies and keep your build process lean and efficient.&lt;/p&gt;

&lt;p&gt;And there you have it! With Rollup.js in your toolkit, you’re all set to bundle your projects like a pro. As you navigate the sea of code, remember to enjoy the journey—after all, it's not just about the destination, but how you bundle along the way! 🚀&lt;/p&gt;

&lt;p&gt;Happy bundling, and may your code be ever concise and your builds ever smooth! 😁&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>rollupjs</category>
      <category>npm</category>
      <category>react</category>
    </item>
    <item>
      <title>How to implement Oauth in Go?</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Mon, 11 Mar 2024 10:44:18 +0000</pubDate>
      <link>https://dev.to/bhargab/how-to-implement-oauth-in-go-part-1-1kfo</link>
      <guid>https://dev.to/bhargab/how-to-implement-oauth-in-go-part-1-1kfo</guid>
      <description>&lt;p&gt;Hi guys, this article will guide you through the process of implementing OAuth in Go, leveraging the power of Gin for routing, Redis for session management, and Goth for handling OAuth providers. Before you begin, make sure to install and set up a Redis server on your local machine by following this &lt;a href="https://redis.io/docs/install/install-redis/" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are going to use the below packages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gin - github.com/gin-gonic/gin&lt;/li&gt;
&lt;li&gt;Redis - github.com/redis/go-redis/v9&lt;/li&gt;
&lt;li&gt;Goth - github.com/markbates/goth&lt;/li&gt;
&lt;li&gt;Gorilla Session - github.com/gorilla/sessions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting Up Redis for Session Storage:
&lt;/h3&gt;

&lt;p&gt;To store user sessions, we'll initialize a Redis client and define functions for setting and retrieving key-value pairs. Ensure to replace the placeholder values with your own Redis configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import (
    "context"

    "github.com/redis/go-redis/v9"
)

var ctx = context.Background()

// Replace these values with your own configuration
const (
    redisAddr     = "localhost:6379"
    redisPassword = ""
)

var rdb *redis.Client

func InitRedis() {
    // Set up Redis session store
    rdb = redis.NewClient(&amp;amp;redis.Options{
        Addr:     redisAddr,
        Password: redisPassword, // no password set
        DB:       0,             // use default DB
    })
}

func GetRedisClient() *redis.Client {
    return rdb
}

func SetValue(key string, value string) {
    err := rdb.Set(ctx, key, value, 0).Err()
    if err != nil {
        panic(err)
    }
}

func GetValue(key string) (string, error) {
    val, err := rdb.Get(ctx, key).Result()
    if err != nil {
        return "", err
    }
    return val, nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Session Management Helper Functions:
&lt;/h3&gt;

&lt;p&gt;Next, implement helper functions for managing user sessions. These functions create, retrieve, and delete sessions, ensuring a seamless authentication experience.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package helpers

import (
    "context"
    "encoding/json"
    "time"

    "auth-go/model"

    "github.com/google/uuid"
    "github.com/markbates/goth"
    "github.com/redis/go-redis/v9"
)

var ctx = context.Background()

func CreateSession(client *redis.Client, user *goth.User) (*model.Session, error) {
    sessionID := generateSessionID() // Generate a unique ID
    session := &amp;amp;model.Session{
        ID:           sessionID,
        UserID:       user.UserID,
        RefreshToken: user.RefreshToken,
        ExpiresAt:    time.Now().Add(time.Hour * 24), // Set expiration time
    }

    sessionData, err := json.Marshal(session)
    if err != nil {
        return nil, err
    }

    err = client.Set(ctx, sessionID, sessionData, session.ExpiresAt.Sub(time.Now())).Err()
    if err != nil {
        return nil, err
    }

    return session, nil
}

func GetSession(client *redis.Client, sessionID string) (*model.Session, error) {
    sessionData, err := client.Get(ctx, sessionID).Result()
    if err == redis.Nil {
        return nil, nil // Session not found
    } else if err != nil {
        return nil, err
    }

    var session model.Session
    err = json.Unmarshal([]byte(sessionData), &amp;amp;session)
    if err != nil {
        return nil, err
    }

    // Check if session is expired
    if session.ExpiresAt.Before(time.Now()) {
        return nil, nil // Session expired
    }

    return &amp;amp;session, nil
}

func DeleteSession(client *redis.Client, sessionID string) error {
    return client.Del(ctx, sessionID).Err()
}

// Helper functions
func generateSessionID() string {
    return uuid.New().String()
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Initializing Authentication Configuration:
&lt;/h3&gt;

&lt;p&gt;Now, let's set up the authentication configuration using the Goth library and configure Google as the OAuth provider. The Goth package supports a variety of providers, allowing you to explore and integrate additional providers according to your needs. This flexibility is crucial for handling user authentication seamlessly.&lt;/p&gt;

&lt;p&gt;Before proceeding, ensure you have created a project in the Google Console and obtained the GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET. For testing purposes, make sure to add test users to the "Test Users" section in the Google Console, as this step is essential for the proper functioning of the authentication.&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%2Fncg6uig28u0ydw45tlfe.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%2Fncg6uig28u0ydw45tlfe.png" alt=" " width="667" height="296"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package auth

import (
    "os"

    "github.com/markbates/goth"
    "github.com/markbates/goth/gothic"
    gothGoogle "github.com/markbates/goth/providers/google"
    "github.com/gorilla/sessions"
)

func InitAuth() {
    // Replace with your SESSION_SECRET or a secure secret key
    sessionSecretKey := "Secret-session-key"

    // Session configuration
    maxAge := 86400 * 30 // 30 days
    isProd := false      // Set to true when serving over HTTPS

    // Create a new CookieStore for session management
    store := sessions.NewCookieStore([]byte(sessionSecretKey))
    store.Options.Path = "/"
    store.Options.HttpOnly = true // HttpOnly should always be enabled
    store.Options.Secure = isProd
    store.MaxAge(maxAge)

    // Set the store for the Goth library
    gothic.Store = store

    // Configure Google OAuth provider
    googleClientID := os.Getenv("GOOGLE_CLIENT_ID")
    googleClientSecret := os.Getenv("GOOGLE_CLIENT_SECRET")

    goth.UseProviders(
        gothGoogle.New(googleClientID, googleClientSecret, "http://localhost:3000/auth/google/callback", "email", "profile"),
    )

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

&lt;/div&gt;



&lt;p&gt;}&lt;/p&gt;

&lt;h3&gt;
  
  
  Middleware Setup:
&lt;/h3&gt;

&lt;p&gt;Two essential middlewares are introduced – CORS middleware for handling cross-origin requests and an authentication middleware to verify user sessions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CORS Middleware
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package middleware

import (
    "github.com/gin-gonic/gin"
)

func CORSMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
        c.Writer.Header().Set("Access-Control-Max-Age", "86400")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-auth")
        c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(200)
        } else {
            c.Next()
        }
    }
}

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Auth Middleware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to the authentication middleware, consider enhancing your security by adding access-token (JWT) verification in the headers. It adds an extra layer of validation, ensuring the integrity and authenticity of the tokens.&lt;/p&gt;

&lt;p&gt;It's essential to note a key concept of JWT – the ability to validate the token without contacting the issuer every time. By checking the ID and verifying the signature of the token with the known public key of the certificate Google used to sign the token, you can achieve efficient and secure validation. This approach eliminates the need to call Google APIs for validation, enhancing the performance of your authentication system.&lt;/p&gt;

&lt;p&gt;For a detailed guide on consuming a Google ID token from a server, you can refer to this insightful &lt;a href="https://ncona.com/2015/02/consuming-a-google-id-token-from-a-server/" rel="noopener noreferrer"&gt;article&lt;/a&gt;. The article provides valuable insights into the JWT validation process and can serve as a helpful resource in implementing secure token verification.&lt;/p&gt;

&lt;p&gt;Ensure that you adapt the JWT validation process based on your specific requirements and security considerations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package middleware

import (
    "net/http"
    "auth-go/model"
    "auth-go/redis"
    "auth-go/utils/ids"
    "auth-go/utils/messages"

    "github.com/gin-gonic/gin"
)

func AuthMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        sessionId, err := c.Request.Cookie("session_id")
        if err != nil {
            c.AbortWithStatusJSON(http.StatusBadRequest, model.BaseResponse{Message: messages.INVALID_SESSION, Code: ids.INVALID_SESSION})
        }
        // Verify session data in Redis
        _, err = redis.GetValue(sessionId.Value)
        if err != nil {
            c.AbortWithStatusJSON(http.StatusBadRequest, model.BaseResponse{Message: messages.INVALID_SESSION, Code: ids.INVALID_SESSION})
        }
        // Session is valid, continue processing
        c.Next()
    }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Routing Setup:
&lt;/h3&gt;

&lt;p&gt;Now, let's define routes and initialize the necessary components in the main function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func init() {
    // Load environment variables from .env file
    err := godotenv.Load()
    if err != nil {
        fmt.Println("Error loading .env file:", err)
        return
    }
    // initialize redis client
    redis.InitRedis()
}

func main() {

    router := gin.Default()

    auth.InitAuth()

    router.Use(middleware.CORSMiddleware())
    router.GET("/auth/:provider/callback", controllers.HandleAuthCallback)
    router.GET("/auth/:provider", controllers.HandleAuthProvider)
    router.GET("/logout/:provider", controllers.LogoutHandler)
    router.Use(middleware.AuthMiddleware())
    router.GET("/check", controllers.CheckHandler)

    fmt.Println("Listening on localhost:3000")
    if err := router.Run(":3000"); err != nil {
        fmt.Println("Error starting server:", err)
    }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Authentication Controllers:
&lt;/h3&gt;

&lt;p&gt;Implement controllers for handling authentication callbacks, provider initiation, and user logout.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package controllers

import (
    "context"

    "net/http"
    "auth-go/helpers"
    "auth-go/model"
    "auth-go/redis"
    "auth-go/utils/ids"
    "auth-go/utils/messages"

    "github.com/gin-gonic/gin"
    "github.com/markbates/goth/gothic"
)

func HandleAuthCallback(c *gin.Context) {
    res := c.Writer
    req := c.Request
    // Get the value of the "provider" path parameter
    provider := c.Param("provider")
    // Set the provider value in the request context
    ctx := context.WithValue(req.Context(), "provider", provider)
    req = req.WithContext(ctx)
    user, err := gothic.CompleteUserAuth(res, req)
    if err != nil {
        // c.String(http.StatusInternalServerError, fmt.Sprintf("Authentication error: %s", err))
        c.AbortWithStatusJSON(http.StatusInternalServerError, model.BaseResponse{Message: messages.AUTH_ERROR, Code: ids.AUTH_ERROR})
        return
    }
    session, err := helpers.CreateSession(redis.GetRedisClient(), &amp;amp;user)
    if err != nil {
        // Handle error
        c.AbortWithStatusJSON(http.StatusInternalServerError, model.BaseResponse{Message: messages.WENT_WRONG, Code: ids.WENT_WRONG})
    }
    // Set the session ID in the cookie
    c.SetCookie("session_id", session.ID, 60*60, "/", "localhost", true, false)
    // disable-caching
    c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
    // user.AccessToken
    c.Redirect(http.StatusFound, "http://localhost:5173")
}

func HandleAuthProvider(c *gin.Context) {
    res := c.Writer
    req := c.Request
    provider := c.Param("provider")
    // Set the provider value in the request context
    ctx := context.WithValue(req.Context(), "provider", provider)
    req = req.WithContext(ctx)
    gothic.BeginAuthHandler(res, req)
}

func LogoutHandler(c *gin.Context) {
    sessionId, err := c.Request.Cookie("session_id")
    if err != nil {
        c.AbortWithStatusJSON(http.StatusBadRequest, model.BaseResponse{Message: messages.INVALID_SESSION, Code: ids.INVALID_SESSION})
    }
    helpers.DeleteSession(redis.GetRedisClient(), sessionId.Value)
    c.JSON(http.StatusOK, model.BaseResponse{Message: messages.SUCCESS, Code: ids.SUCCESS})
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check Endpoint Controller:
&lt;/h3&gt;

&lt;p&gt;Finally, a controller is provided to check if the authentication is working after login.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package controllers

import (
    "net/http"

    "auth-go/model"
    "auth-go/utils/ids"
    "auth-go/utils/messages"

    "github.com/gin-gonic/gin"
)

func CheckHandler(c *gin.Context) {
    c.JSON(http.StatusOK, model.BaseResponse{Message: messages.SUCCESS, Code: ids.SUCCESS})
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  GitHub Repository:
&lt;/h3&gt;

&lt;p&gt;To access the complete codebase for this OAuth implementation in Go, including the backend and frontend components, please visit the dedicated GitHub repository:&lt;/p&gt;

&lt;p&gt;GitHub Repository - &lt;a href="https://github.com/the-wrong-guy/go-auth" rel="noopener noreferrer"&gt;OAuth in Go&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to clone the repository, explore the code, and use it as a reference for your own projects. If you encounter any issues or have questions, don't hesitate to open an issue on the repository. Your feedback and contributions are highly appreciated.&lt;/p&gt;

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

&lt;p&gt;Congratulations on successfully implementing OAuth in Go using Gin, Redis, and Goth! We've covered the essential steps for authentication, set up middleware, and even explored the concept of JWT token verification for enhanced security.&lt;/p&gt;

&lt;p&gt;If you have any questions, need clarification, or want to discuss any aspect of the implementation, feel free to reach out. Your feedback is valuable, and I'm here to assist you in any way possible.&lt;/p&gt;

&lt;p&gt;Stay tuned for the next installment, and happy coding!&lt;/p&gt;

</description>
      <category>go</category>
      <category>oauth</category>
      <category>backenddevelopment</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to upload images to Firebase like a Pro?</title>
      <dc:creator>BHARGAB KALITA</dc:creator>
      <pubDate>Thu, 11 Feb 2021 13:59:32 +0000</pubDate>
      <link>https://dev.to/bhargab/how-to-upload-images-to-firebase-like-a-pro-1f4f</link>
      <guid>https://dev.to/bhargab/how-to-upload-images-to-firebase-like-a-pro-1f4f</guid>
      <description>&lt;p&gt;So the first question in which database should you use to upload the images? 🤔, cause we have both &lt;code&gt;Firestore&lt;/code&gt; and &lt;code&gt;Realtime Database&lt;/code&gt; in the Firebase, but we are not going to upload the images directly to the Firestore or Realtime Database due to &lt;code&gt;limited size for each "Document" which is "1MB" 😬&lt;/code&gt; set by Firebase, hence we are going to upload the image files to the &lt;code&gt;Firebase Storage&lt;/code&gt; and then will take the Download URL and add that URL to the Firestore (or Realtime DB) 😋&lt;/p&gt;

&lt;h3&gt;
  
  
  TLDR;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Firebase from 'Firebase'
import { v4 as uuid } from "uuid";


  export default function Somefunction(){

    const storage = firebase.storage()
    const db = firebase.firestore()

    const upload = () =&amp;gt; {
      const storageRef = storage.ref();
      const Imageref = storageRef.child(
          `images/${uuid()}-${Image.name}` 
        );
      await Imageref.put(Image);
      const ImageFileUrl = await Imageref.getDownloadURL();

      await db.collection("ImageUrls").add({
        image : ImageFileUrl
      })
    }

  return()
  }  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;PS: we are going to use React for this example&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Step&lt;/strong&gt;: Setting our main file&lt;br&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%2Fi%2Fyzgvdwuila4jt6c8sm4p.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%2Fi%2Fyzgvdwuila4jt6c8sm4p.png" alt="image" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second Step&lt;/strong&gt;: Writing our upload function&lt;br&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%2Fi%2Fi3f9xvqyt0m1yjm161if.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%2Fi%2Fi3f9xvqyt0m1yjm161if.png" alt="image" width="800" height="652"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Third Step&lt;/strong&gt;: Now we have a problem of repetition of the Image References if we upload images of same names or uploading the same image multiple times while will lead to failure in fetching Images with same names, so to fix that we will be using &lt;code&gt;uuid&lt;/code&gt; (NPM Package)&lt;br&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%2Fi%2Fs0b5z4i86lls517jjmrj.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%2Fi%2Fs0b5z4i86lls517jjmrj.png" alt="image" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fourth Step&lt;/strong&gt;: Since all the things are done, now the only thing left is to add the &lt;code&gt;ImageFileUrl&lt;/code&gt; to Firestore&lt;br&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%2Fi%2Fnvg6ql1mfryjeeqc69em.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%2Fi%2Fnvg6ql1mfryjeeqc69em.png" alt="image" width="800" height="764"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yayyyyy, you have successfully uploaded the image and added it to the Firestore 🎉&lt;/p&gt;




&lt;p&gt;Thanks for reading this far, I hope y'all will love this 😊&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>react</category>
      <category>firstpost</category>
      <category>imagepload</category>
    </item>
  </channel>
</rss>
