<?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: Jose Coego</title>
    <description>The latest articles on DEV Community by Jose Coego (@hoshdev).</description>
    <link>https://dev.to/hoshdev</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%2F2634468%2F70e10519-eec6-48b1-bbbc-5696b6715a7f.png</url>
      <title>DEV Community: Jose Coego</title>
      <link>https://dev.to/hoshdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hoshdev"/>
    <language>en</language>
    <item>
      <title>TECHNICAL DEBT: BE REALISTIC!! YOU CAN NOT AVOID IT.</title>
      <dc:creator>Jose Coego</dc:creator>
      <pubDate>Thu, 09 Jan 2025 17:58:49 +0000</pubDate>
      <link>https://dev.to/hoshdev/technical-debt-be-realistic-you-can-not-avoid-it-2b0o</link>
      <guid>https://dev.to/hoshdev/technical-debt-be-realistic-you-can-not-avoid-it-2b0o</guid>
      <description>&lt;p&gt;Technical debt itself isn’t inherently bad. It’s a natural part of the development process, especially when deadlines are tight or when the system is evolving rapidly. However, the real problem arises when technical debt is ignored, neglected, or mishandled. If left unmanaged, it accumulates over time and can lead to significant problems that affect the project's long-term health.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Does Technical Debt Arise?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Deadlines&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Every project has deadlines, and more often than not, they are tight. To meet these deadlines, developers are often forced to prioritize speed over quality. This typically means taking shortcuts and skipping best practices, leading to the accumulation of technical debt. While this may be necessary in some cases, it can have long-term consequences if not managed properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Evolving Knowledge Over Time&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you start working on a new feature or project, your understanding of the problem is limited to the initial requirements. As the project progresses, you may uncover new insights or encounter unexpected challenges that were not part of the original plan. As systems and features evolve, new complexities emerge, and adapting the code to these changes can create technical debt if the code isn't refactored to accommodate the new needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Developer Expertise&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Technical debt doesn’t always stem from a lack of experience, and even senior developers can contribute to it. When faced with unfamiliar challenges or technologies, even skilled developers may resort to quick, imperfect solutions to make progress. While experience certainly helps reduce technical debt by promoting better practices, the complexity of modern systems means that technical debt is often unavoidable, regardless of skill level.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Changing Requirements&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As projects develop, the original scope often changes. New requirements are added, old ones are modified, or priorities shift, which can lead to rushed changes in the code. These changes might not align with the architecture or best practices, creating technical debt. Managing changing requirements carefully and allowing time for proper adjustments can help minimize this.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Lack of Proper Documentation or Knowledge Sharing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Sometimes, technical debt arises not only from writing imperfect code but also from a lack of proper documentation or knowledge transfer within the team. If features or systems are developed without clear documentation or if knowledge is siloed, future developers may struggle to understand the code, leading them to make quick fixes that add to the debt.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Legacy Systems or Third-Party Dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In many cases, technical debt accumulates because teams work with legacy systems or third-party libraries that are difficult to modify or upgrade. These systems may have been designed with outdated technology or poor architectural decisions, and the cost of refactoring them can seem too high compared to just adding quick fixes. Over time, this contributes to technical debt.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Is It Essential to Address Technical Debt?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;It Can Cause Slower Development in the Future&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As the debt piles up, it can make future development more difficult. New developers may struggle to understand the codebase, and even experienced developers may find it hard to implement new features due to the complexity of the existing code.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;It Can Impact Performance and Stability&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Technical debt can also result in performance issues or bugs that become harder to fix as the system grows. What might have been a small issue can snowball into a major problem, affecting both the user experience and the stability of the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;It Can Increase Costs Over Time&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Initially, taking shortcuts may seem like a good idea to meet deadlines, but over time, the cost of managing technical debt increases. Regular maintenance, troubleshooting, and the need for more complex updates can drain resources and delay progress.&lt;/p&gt;




&lt;p&gt;Technical debt is inevitable but manageable. Recognizing its causes and consequences can help teams address it proactively, ensuring long-term project health and stability.&lt;/p&gt;

&lt;p&gt;For any questions, feel free to contact me on &lt;a href="https://hoshdev.com/contact" rel="noopener noreferrer"&gt;personal website&lt;/a&gt; or in &lt;a href="https://x.com/jcoegom" rel="noopener noreferrer"&gt;X&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Setting up Jest and React Testing Library with Webpack</title>
      <dc:creator>Jose Coego</dc:creator>
      <pubDate>Sat, 04 Jan 2025 09:59:40 +0000</pubDate>
      <link>https://dev.to/hoshdev/setting-up-jest-and-react-testing-library-with-webpack-55dh</link>
      <guid>https://dev.to/hoshdev/setting-up-jest-and-react-testing-library-with-webpack-55dh</guid>
      <description>&lt;p&gt;Although Webpack might not be your preferred bundler, with tools like Vite gaining significant community support, you may still encounter projects where configuring Jest and React Testing Library with Webpack is necessary.&lt;/p&gt;

&lt;p&gt;To configure Jest and React Testing Library in this application, follow these steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install Jest and React Testing Library
&lt;/h3&gt;

&lt;p&gt;Install Jest and React Testing Library as development dependencies:&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;--save-dev&lt;/span&gt; jest @testing-library/react @testing-library/jest-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Configure Babel
&lt;/h3&gt;

&lt;p&gt;Ensure Babel is set up correctly to work with Jest. Add or update your &lt;code&gt;babel.config.js&lt;/code&gt; or &lt;code&gt;.babelrc&lt;/code&gt; file with the following:&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;"presets"&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;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-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;"@babel/preset-typescript"&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;Install the necessary Babel presets if not already 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;--save-dev&lt;/span&gt; @babel/preset-env @babel/preset-react @babel/preset-typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Add a Jest Configuration File
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;jest.config.js&lt;/code&gt; file in the root of your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;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;testEnvironment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jsdom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;moduleNameMapper&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="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.(css|scss)$&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;identity-obj-proxy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;transform&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;^.+&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;.(js|jsx|ts|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;babel-jest&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;
  
  
  4. Install Additional Dependencies
&lt;/h3&gt;

&lt;p&gt;Install the following dependencies for handling CSS modules and Jest transformations:&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;--save-dev&lt;/span&gt; babel-jest identity-obj-proxy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Create Test Files
&lt;/h3&gt;

&lt;p&gt;Ensure your test files follow the naming convention &lt;code&gt;*.test.js&lt;/code&gt; or &lt;code&gt;*.test.tsx&lt;/code&gt;. Place them in the same directories as your components or within a dedicated &lt;code&gt;__tests__&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;src/App.test.js&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&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;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&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;./App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;renders the App component&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&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;App&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;linkElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Task Timer/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;linkElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&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;
  
  
  6. Update Package.json Scripts
&lt;/h3&gt;

&lt;p&gt;Add a script for running tests in 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;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest"&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;Run your tests 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;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Configure Webpack for CSS Modules
&lt;/h3&gt;

&lt;p&gt;Ensure your Webpack configuration supports CSS modules and Jest compatibility:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;module&lt;/span&gt;&lt;span class="se"&gt;\.(&lt;/span&gt;&lt;span class="sr"&gt;css|scss&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;use&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;style-loader&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;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;css-loader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&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="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;localIdentName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[name]__[local]__[hash:base64:5]&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;esModule&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sass-loader&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;
  
  
  8. Verify the Setup
&lt;/h3&gt;

&lt;p&gt;Run your tests to ensure everything is working:&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;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix any issues or errors encountered during this step.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Optional: Add a Coverage Report
&lt;/h3&gt;

&lt;p&gt;Enable code coverage by adding the following to your Jest configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;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;collectCoverage&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;collectCoverageFrom&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;src/**/*.{js,jsx,ts,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;!src/index.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;coverageReporters&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;html&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;text&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;p&gt;Run tests with coverage:&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;test&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--coverage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate a coverage report that you can review for untested parts of your codebase.&lt;/p&gt;

&lt;h4&gt;
  
  
  Something to Discuss?
&lt;/h4&gt;

&lt;p&gt;Have questions, ideas, or feedback? Let's talk! Visit &lt;a href="https://hoshdev.com/contact" rel="noopener noreferrer"&gt;&lt;strong&gt;hoshdev.com/contact&lt;/strong&gt;&lt;/a&gt; and start the conversation.  &lt;/p&gt;

&lt;p&gt;I'm here to help you tackle challenges and create amazing solutions.  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Server side Rendering Vs Server components. Explanation with example.</title>
      <dc:creator>Jose Coego</dc:creator>
      <pubDate>Tue, 31 Dec 2024 13:46:15 +0000</pubDate>
      <link>https://dev.to/hoshdev/server-side-rendering-vs-server-components-explanation-with-example-683</link>
      <guid>https://dev.to/hoshdev/server-side-rendering-vs-server-components-explanation-with-example-683</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Context: Evolution of Rendering Approaches&lt;/strong&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Client-Side Rendering (CSR)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; Everything is rendered in the browser.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Fast interactivity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Heavy initial load and poor SEO.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; Content is rendered on the server and sent to the client, where hydration enables interactivity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Improved SEO.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Still requires JavaScript in the browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Static Site Generation (SSG)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; Content is pre-rendered at build time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Fast initial load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Limited content flexibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Incremental Static Regeneration (ISR)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; Content is rendered statically and revalidated at predefined intervals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Combines static performance with periodic updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Updates aren’t real-time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;React Server Components (RSC)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How it works:&lt;/strong&gt; Content is rendered on the server with &lt;strong&gt;no need for JavaScript&lt;/strong&gt; on the client (except for client components).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Smaller bundle size&lt;/li&gt;
&lt;li&gt;Faster initial load&lt;/li&gt;
&lt;li&gt;Interactivity supported via client components&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Comparing SSR and RSC&lt;/strong&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;SSR Example:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In this example, we have an application that displays the title and description of posts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clone the repository:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git clone https://github.com/hoshdev/poc-nextjs-rsc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install dependencies:&lt;/strong&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;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Switch to the &lt;em&gt;1-rcc&lt;/em&gt; branch:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git checkout 1-rcc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Open &lt;code&gt;App.tsx&lt;/code&gt;.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You’ll notice it's a client component (using the &lt;code&gt;use client&lt;/code&gt; directive at the top).&lt;/p&gt;

&lt;p&gt;In this example, we retrieve post content inside a &lt;code&gt;useEffect&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;browserUseEffect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="nf"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;posts&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;setPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;err&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Observe:&lt;/strong&gt; All JavaScript is sent to the client.&lt;/li&gt;
&lt;/ol&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%2Fvib0su0sm2kp074pwfso.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%2Fvib0su0sm2kp074pwfso.png" alt="SSR Example" width="800" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s a simple example with a small amount of data, but imagine this in a real application. Here's the flow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The content is created on the server (the first &lt;code&gt;console.log&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Then, it's sent to the browser (the next &lt;code&gt;console.log&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Finally, once hydrated, the component becomes interactive, and the &lt;code&gt;console.log&lt;/code&gt; inside the &lt;code&gt;useEffect&lt;/code&gt; is displayed in the browser.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;RSC Example:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Switch to the &lt;em&gt;2-rsc&lt;/em&gt; branch:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git checkout 2-rsc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;In &lt;code&gt;App.tsx&lt;/code&gt;, you’ll see a server component.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It doesn't have the &lt;code&gt;use client&lt;/code&gt; directive at the top. Since it's a server component, it runs on the server.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;async/await&lt;/code&gt; to retrieve data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PostsPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&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;getPosts&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;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can access &lt;strong&gt;any Node.js feature&lt;/strong&gt; in server components, like interacting with a database, the file system, or using &lt;code&gt;process.env&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Observe:&lt;/strong&gt; No JavaScript for server components is sent to the client. Compare this with the SSR example screenshot.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&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%2F927iwgc8cmr2csdn282s.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%2F927iwgc8cmr2csdn282s.png" alt="RSC Example" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Adding Interactivity in RSC:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;If you need interactivity, you can include &lt;strong&gt;client components&lt;/strong&gt;. These are sent to the browser, allowing for dynamic behavior.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Switch to the &lt;em&gt;3-rsc-rcc&lt;/em&gt; branch:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git checkout 3-rsc-rcc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Here, we added a client component&lt;/strong&gt; that handles interactivity. So, the JavaScript for this component is sent to the browser,l but just client component.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Some Final Tips&lt;/strong&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Server Environment:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Leverage Node.js features:&lt;/strong&gt; You can use Node.js-specific functionality like &lt;code&gt;async/await&lt;/code&gt;, &lt;code&gt;process.env&lt;/code&gt;, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No hooks in server components:&lt;/strong&gt; You cannot use React hooks (such as &lt;code&gt;useState&lt;/code&gt; or &lt;code&gt;useEffect&lt;/code&gt;) in server components since they are meant to be client-side only.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Event Handling:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use client components for interactivity:&lt;/strong&gt; Server components are responsible for rendering, but for event handling (like clicks or form submissions), you must use client components to manage the interactivity.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;By understanding the evolution of rendering approaches—from &lt;strong&gt;CSR&lt;/strong&gt; to &lt;strong&gt;SSR&lt;/strong&gt;, &lt;strong&gt;SSG&lt;/strong&gt;, &lt;strong&gt;ISR&lt;/strong&gt;, and &lt;strong&gt;RSC&lt;/strong&gt;—you can make more informed decisions about the architecture of your application, improving both performance and SEO.&lt;/p&gt;

&lt;h4&gt;
  
  
  Something to Discuss?
&lt;/h4&gt;

&lt;p&gt;Have questions, ideas, or feedback? Let's talk! Visit &lt;a href="https://hoshdev.com/contact" rel="noopener noreferrer"&gt;&lt;strong&gt;hoshdev.com/contact&lt;/strong&gt;&lt;/a&gt; and start the conversation.  &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
