<?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: saber-mekki</title>
    <description>The latest articles on DEV Community by saber-mekki (@sabermekki).</description>
    <link>https://dev.to/sabermekki</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%2F906024%2F8e11a11c-16d7-4c37-bef0-c88905a2bdc9.jpeg</url>
      <title>DEV Community: saber-mekki</title>
      <link>https://dev.to/sabermekki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sabermekki"/>
    <language>en</language>
    <item>
      <title>Automated Deployment of a Next.js Application on Ubuntu server with Git Hooks</title>
      <dc:creator>saber-mekki</dc:creator>
      <pubDate>Mon, 24 Jun 2024 13:51:58 +0000</pubDate>
      <link>https://dev.to/sabermekki/automated-deployment-of-a-nextjs-application-on-ubuntu-with-git-hooks-30fk</link>
      <guid>https://dev.to/sabermekki/automated-deployment-of-a-nextjs-application-on-ubuntu-with-git-hooks-30fk</guid>
      <description>&lt;p&gt;Deploying a Next.js application can be streamlined by automating the process with Git hooks and managing the application lifecycle with PM2. In this article, I'll walk you through setting up your server, transferring your application.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Step 1: Set Up the Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install Node.js, npm, pm2, git:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh your-username@serverIp&lt;/code&gt;&lt;br&gt;
&lt;code&gt;sudo apt update&lt;/code&gt;&lt;br&gt;
&lt;code&gt;sudo apt install -y nodejs npm&lt;/code&gt;&lt;br&gt;
&lt;code&gt;sudo npm install -g pm2&lt;/code&gt;&lt;br&gt;
&lt;code&gt;sudo apt install -y git&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Transfer Your Next.js Application to the Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On your local machine:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Add the Remote Repository&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;git remote add deploy ssh://your-username@serverIp/your-username/home/apps/my-nextjs-app.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;On the server:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create the Bare Repository&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p /your-username/home/apps/my-nextjs-app.git
cd /your-username/home/apps/my-nextjs-app.git
git init --bare
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Set Up the Post-Receive Hook&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /your-username/home/apps/my-nextjs-app.git/hooks
nano post-receive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Add the following script to the post-receive hook:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
REPO_PATH=/your-username/home/my-nextjs-app.git
APP_PATH=/your-username/home/apps/my-nextjs-app
NODE_ENV=production

GIT_WORK_TREE=$APP_PATH git checkout -f main

cd $APP_PATH

npm install
npm run build

pm2 restart nextjs-app || pm2 start npm --name "nextjs-app" -- start

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Make the hook executable:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x post-receive

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

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Step 3: Deploy Your Application&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;On your local machine, push your code:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push deploy main

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

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, deploying a Next.js application on Ubuntu can be efficiently managed through manual deployment, CI/CD pipelines, containerization, cloud services, PaaS solutions, static site export, or third-party hosting services, each offering unique benefits based on scalability, ease of use, cost, and complexity&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Pure Functions in Next.js</title>
      <dc:creator>saber-mekki</dc:creator>
      <pubDate>Fri, 21 Jun 2024 10:46:35 +0000</pubDate>
      <link>https://dev.to/sabermekki/pure-functions-in-nextjs-4ni4</link>
      <guid>https://dev.to/sabermekki/pure-functions-in-nextjs-4ni4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While working with Next.js, a very famous React framework for building server-side rendered and static web applications, one of the very important concepts you are going to meet is a pure function. Pure functions are among those principal topics one has to grasp in functional programming and take a huge part in making your code reliable and serviceable. We shall look into what exactly a pure function is, why it is important, and how to use one effectively within a Next.js application.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What is a Pure Function?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A pure function could be defined as a function that meets the following two basic requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deterministic&lt;/strong&gt;: It always returns the same output for a given input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Side-effect Free&lt;/strong&gt;: It has no side effects; that is, it does not &lt;br&gt;
modify any state or have an effect on the world outside.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next example will be a pure function. JavaScript provides a very good example of such a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b) {
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No matter which a and b are passed to this function, it will always yield the same value and has no side effects, nor does it mutate any external state.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Why Use Pure Functions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pure functions have the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Predictability&lt;/strong&gt;: Since pure functions always return the same output for any given input, they become more predictable and easier to reason about during debugging. It means that pure functions can be tested in isolation without mocking external dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt;: There are no side effects inside pure functions, resulting in much safer, more reliable code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Pure Functions in Next.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pure functions can be utilized in various areas of your Next.js app, from components and utility functions to data transformations.&lt;/p&gt;

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

&lt;p&gt;React components can also be pure functions. In fact, all functional components are pure functions where props are passed as an argument returning a React element as the result. The following is an example of a pure functional component in Next.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

function SayHello(name) {
  return &amp;lt;h1&amp;gt;Hello, {name}!&amp;lt;/h1&amp;gt;;
};

export default SayHello;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component is pure because it always returns the same output for the same props and has no side effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All utility functions used in your Next.js application should be pure as well&lt;/p&gt;

&lt;p&gt;A utility function that capitalizes the first letter of a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function is pure because it consistently returns the same output for the same input without modifying any external state.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Fetching Data and Side Effects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While pure functions are a cornerstone, not all functions in a Next.js application can or should be pure. This is true, for example, of data fetching functions. By definition, they have side effects – they make requests to external APIs or databases.&lt;/p&gt;

&lt;p&gt;In Next.js, data fetching generally occurs through functions like &lt;strong&gt;getStaticProps **or **getServerSideProps&lt;/strong&gt;, which are explicitly designed to run side effects. Such functions are not pure, but they isolate side effects to parts of your application so that the rest is more tractable.&lt;/p&gt;

&lt;p&gt;As for a concrete example of data fetching in Next.js, here it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
export async function getStaticProps() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();

  return {
    props: {
      data,
    },
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Combining Pure and Impure Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In practice, a Next.js application will contain pure and impure functions. However, what is important is that you isolate the impure ones and keep the maximum portion of your codebase pure. This will bring semantic epithets and ensure maintainability for your app.&lt;/p&gt;

&lt;p&gt;As such, you can isolate concerns into pure functions to format data fetched from an API the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Impure function 
export async function fetchData() {
  // fetches data from an API
}

// Pure function
export function formatData(data) {
  //formats the fetched data
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Writing pure functions is one of the cornerstones of functional programming, which can bring a huge potential improvement in the quality of your Next.js applications. With them, you guarantee that your code will become more predictable, hence more testable, and less vulnerable to bugs. Totally writing pure functions is not quite possible, but isolating side effects and keeping most of your codebase pure will make for a more maintainable and robust application.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Git for Beginners</title>
      <dc:creator>saber-mekki</dc:creator>
      <pubDate>Sat, 13 Aug 2022 12:39:00 +0000</pubDate>
      <link>https://dev.to/sabermekki/git-for-beginners-4ja7</link>
      <guid>https://dev.to/sabermekki/git-for-beginners-4ja7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Birth of Git&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Until April 2005 Linus Torvalds was using BitKeeper for version control of the Linux Kernel development. He had a large number of volunteer developers working on the Linux Kernel and their contributions had to be managed. BitKeeper was a nice tool for managing the enormous contribution by the developers. The Linux developers used the tool for free after an agreement between the two parties as BitKeeper was a proprietary source control management system which means you had to pay for the use of the tool. There came a conflict of interest after Andrew Tridgell created an open-source client for accessing the Bitkeeper version control system by reverse-engineering the BitKeeper protocols. This caused the copyright holder to withdrawal the free-to-use policy that they had earlier agreed upon. Many developers of the Linux kernel gave up access to the BitKeeper. &lt;/p&gt;

&lt;p&gt;Linux knew he had to act fast to replace the version control system that he knew and loved so he took a working vacation to decide on what to do as the current free-to-use version control systems could not solve his problems at the time. The result of his vacation was the birth of a new version control system named Git. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strengths of git&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Works in multi-platform.&lt;/li&gt;
&lt;li&gt;Small and quick.&lt;/li&gt;
&lt;li&gt;Distributed development.&lt;/li&gt;
&lt;li&gt;Strong support for non-linear development.&lt;/li&gt;
&lt;li&gt;Automatic Garbage Collection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why Use It?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Git is useful to anyone who writes code or track changes to files, it is the most commonly used version control system.&lt;br&gt;
Git lets multiple developers easily work together on the same project with speed and efficiency.Tracking and history are available, even when off-line.&lt;br&gt;
Git is used for tracking and managing changes in files (In reality, git doesn't save the file but save the history of changes in a file).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B5YUnurW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66egdfdwxh7fte42afr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B5YUnurW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66egdfdwxh7fte42afr0.png" alt="Image description" width="880" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;On Linux&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Using apt:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get update&lt;br&gt;
sudo apt-get install git-all&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using dnf:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo dnf install git-all&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How use it
&lt;/h2&gt;

&lt;p&gt;After creating the project we will start by :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an empty Git repository or reinitialize an existing one, in this stage you will be in "default" branch called "master",it is a naming convention for a branch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git status 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Used to display the state of the repository and staging area(display the change in your project)&lt;/p&gt;

&lt;p&gt;Tow floors appear to us after use this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Untracked&lt;/code&gt;: This file exists locally, but isn’t a part of the Git repository. The file’s change history will not be recorded and it will not be pushed to remote.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Tracked&lt;/code&gt;: Git tracks the file’s change history and it will be pushed to remote copies when running git push. At this point, the are two situations:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Unstaged changes":&lt;br&gt;
 Exist in your working directory, but Git hasn’t recorded them into its version history yet.&lt;/p&gt;

&lt;p&gt;"Staged changes":&lt;br&gt;
  Are a lot like unstaged changes, except that they’ve been marked to be committed the next time you run git commit.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add file contents to the index. The status of your changes goes from "Untracked and Unstaged" to "Staged".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Record changes to the repository. before committing you must know the "Conventional Commits", it mean you will try to add a human and machine readable meaning to commit messages see:&lt;a href="https://www.conventionalcommits.org/en/v1.0.0/#specification"&gt;Conventional Commits&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;git push 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To integrate the branch to the remote repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log --oneline --decorate --all --graph
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Show commit logs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Extensions must have in VS Code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GitLens&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git Graph&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git History&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope you were able to gain something from this article.&lt;br&gt;
If you have any questions do not hesitate to contact me &lt;a href="https://www.linkedin.com/in/mekki-saber/"&gt;saber mekki&lt;/a&gt;&lt;/p&gt;

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