<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: LordCodex</title>
    <description>The latest articles on Forem by LordCodex (@devmirx).</description>
    <link>https://forem.com/devmirx</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%2F1016810%2Fbc77d5b9-ed9d-4807-851a-eed4c8e468a9.jpg</url>
      <title>Forem: LordCodex</title>
      <link>https://forem.com/devmirx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/devmirx"/>
    <language>en</language>
    <item>
      <title>Stellar Project Challenge</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Mon, 19 Aug 2024 04:05:54 +0000</pubDate>
      <link>https://forem.com/devmirx/stellar-project-challenge-40g3</link>
      <guid>https://forem.com/devmirx/stellar-project-challenge-40g3</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for the &lt;a href="https://dev.to/challenges/stellar"&gt;Build Better on Stellar: Smart Contract Challenge &lt;/a&gt;: Create a Tutorial&lt;/em&gt;&lt;/p&gt;

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

&lt;p&gt;I created a simple platform with js-stellar-sdk where users can propose a project and vote with their stellar tokens &lt;/p&gt;

&lt;h2&gt;
  
  
  Demo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://superb-torrone-c83f12.netlify.app" rel="noopener noreferrer"&gt;https://superb-torrone-c83f12.netlify.app&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;Here is the repo link:&lt;br&gt;
&lt;a href="https://github.com/LordCodex164/Stellar-Voting-frontend/tree/master" rel="noopener noreferrer"&gt;https://github.com/LordCodex164/Stellar-Voting-frontend/tree/master&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Journey
&lt;/h2&gt;

&lt;p&gt;My research process involved learning about decentralized applications and how blockchain technology can provide transparent voting systems. I focused on the Stellar network due to its speed and low transaction costs, which are essential for a voting application. The js-stellar-sdk was particularly appealing as it simplifies interactions with the Stellar blockchain.&lt;/p&gt;

&lt;p&gt;Experience with the Ecosystem&lt;br&gt;
My experience with the Stellar ecosystem has been enriching. The community is supportive, and the documentation provided by Stellar has been incredibly helpful. Engaging with other developers and users has opened my eyes to the potential of blockchain beyond just cryptocurrencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation Behind My Submission
&lt;/h2&gt;

&lt;p&gt;The motivation behind this project came from a desire to empower communities by providing a platform where users can propose projects and vote using Stellar tokens. I believe that decentralized voting can enhance transparency and trust in decision-making processes&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Am Particularly Proud Of
&lt;/h2&gt;

&lt;p&gt;I am particularly proud of the app's user-friendly interface, which allows users to easily propose and vote on projects&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Aspirations
&lt;/h2&gt;

&lt;p&gt;Moving forward, I hope to expand this project by incorporating more features, such as project tracking and reward systems for participants. I also aim to explore further integrations with other blockchain technologies, enhancing the app's functionality and reach.&lt;/p&gt;

&lt;p&gt;&lt;a href="mailto:adenirandaniel565@gmail.com"&gt;adenirandaniel565@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devchallenge</category>
      <category>stellarchallenge</category>
      <category>blockchain</category>
      <category>web3</category>
    </item>
    <item>
      <title>Next.js 15 RC Unveiled: A Close Look at the Framework's Latest Features</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Sun, 26 May 2024 13:39:40 +0000</pubDate>
      <link>https://forem.com/devmirx/nextjs-15-rc-unveiled-a-close-look-at-the-frameworks-latest-features-4lc1</link>
      <guid>https://forem.com/devmirx/nextjs-15-rc-unveiled-a-close-look-at-the-frameworks-latest-features-4lc1</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nextjs.org" rel="noopener noreferrer"&gt;Next.js&lt;/a&gt;, a react framework recently released its RC(release candidate) 15th version; this release candidate version introduces new features that will change how you build high-quality and performance web applications. &lt;/p&gt;

&lt;p&gt;These new features touch on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nextjsstarter.com/blog/nextjs-hydration-errors-causes-fixes-tips/" rel="noopener noreferrer"&gt;Hydration Errors&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nextjs.org/docs/app/building-your-application/caching" rel="noopener noreferrer"&gt;Caching&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nextjs.org/blog/next-14#partial-prerendering-preview" rel="noopener noreferrer"&gt;Partial Prerendering(Experimental)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nextjs.org/blog/next-15-rc#executing-code-after-a-response-with-nextafter-experimental" rel="noopener noreferrer"&gt;Next/After&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nextjs.org/docs/app/api-reference/create-next-app" rel="noopener noreferrer"&gt;Create-next-app&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://nextjs.org/blog/next-15-rc#optimizing-bundling-of-external-packages-stable" rel="noopener noreferrer"&gt;Bundling external packages (stable)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a developer, it is important to understand the key features, and how they can enhance your web development projects. In this article, I will be explaining what is new in Next15 so that you can start implementing them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that these Nextjs features are still being tested rigorously and extensively before its final release. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;A Working Knowledge of React&lt;/p&gt;

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

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

&lt;p&gt;Next.js also known as next is a framework that is built on top of React and offers additional features like Server-Side Rendering (SSR), allowing you to create high-quality and performant web applications with the power of React components. It also offers powerful features like Built-in Optimizations, Dynamic HTML Streaming, React Server Components, and many more!&lt;/p&gt;

&lt;h2&gt;
  
  
  A Sneak Peek at the Latest Features
&lt;/h2&gt;

&lt;h3&gt;
  
  
  React:
&lt;/h3&gt;

&lt;p&gt;Since Nextjs was built on top of the React canary channel, developers had to use and give feedback on the React APIs before the release of React 19 RC. The Release played a huge part in the Next Support for the React 19 Rc version, including new features for both the client and server-like actions.&lt;/p&gt;

&lt;h3&gt;
  
  
  React Compiler:
&lt;/h3&gt;

&lt;p&gt;With this newly built-in feature, developers can now save the time of manually memoizing values with the use of useCallback and useMemo to solve re-rendering issues. &lt;/p&gt;

&lt;p&gt;Also with Nextjs support for the react compiler, developers can now focus on creating and maintaining code that is easier to maintain, and less error-prone.&lt;/p&gt;

&lt;p&gt;To use the compiler, it is done by just installing &lt;code&gt;babel-plugin-react-compiler&lt;/code&gt; with the command as shown below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install babel-plugin-react-compiler&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Then, add the experimental.reactCompiler option in your next.config.js file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 javascript
const nextConfig = {

experimental: {

reactCompiler: true,

},

};

module.exports = nextConfig;


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

&lt;/div&gt;

&lt;p&gt;However, If you are looking to configure your compiler further to run in “opt-in” mode, add this option as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 javascript
const nextConfig = {

experimental: {

reactCompiler: {

compilationMode: 'annotation',

},

},

};

module.exports = nextConfig;


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

&lt;/div&gt;

&lt;p&gt;With this, you now have your compiler set up and you don't have to worry about re-rendering issues because the compiler is handling that for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hydration error improvements
&lt;/h3&gt;

&lt;p&gt;A Hydration error is a common error in next that stems from the mismatch of the server and client’s component states and generated markup.  However, Next15 made massive improvements to the hydration errors, by providing the source code of the error and also ways to solve the error. isn’t that great?&lt;/p&gt;

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

&lt;p&gt;It has now greatly improved from what we have above 👆to this 👇:&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Caching Updates
&lt;/h3&gt;

&lt;p&gt;Before now, next js used to have opinionated default caching defaults. These defaults were implemented to provide a performant approach for the next application; However, this approach didn't suit the needs of projects like Partial Prerendering (PPR) and third-party libraries like fetch. However, after careful consideration and revaluation of the caching heuristics, the defaulting default is now uncached by default in the Next15 version.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is important to note the team is still making improvements to caching in Next in the coming months&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Fetch Requests are no longer cached by default
&lt;/h3&gt;

&lt;p&gt;Nextjs allows us to configure how our server-side requests interact with the framework’s persistent HTTP cache. However, we are provided with options like the force-cache and no-store method, which determine the behavior of the resource fetched from the server that gets updated in the cache or not:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 javascript
fetch('https://...', { cache: 'force-cache' | 'no-store' });


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Force-cache&lt;/strong&gt;: This option defaults in next14; it involves fetching the resource from the cache (if it exists) or a remote server and updating the cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No-store&lt;/strong&gt;: When a request is made, the web fetch API retrieves the resource from a remote server based on that request and does not update the cache. This option now comes by default in Next 15.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Route Handlers
&lt;/h3&gt;

&lt;p&gt;In Next 15, get route handlers will no longer be cached as well. So, you only decide to opt into caching by using a static route config option such as &lt;code&gt;export dynamic = ‘force&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Client Route Cache
&lt;/h3&gt;

&lt;p&gt;Also, on the client route updates, it will also not be cached by default. Therefore, as you navigate back and forth in your app, the active page components will continue to get the latest data from the server and not from the cache. This is because the staleTimes flag that allows custom configuration of the router cache is now 0 for page segments as shown in the config below.&lt;/p&gt;

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

//next.config.js file

const nextConfig = {
  experimental: {
    staleTimes: {
      dynamic: 0,
    },
  },
};

module.exports = nextConfig;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Incremental adoption of Partial Prerendering
&lt;/h3&gt;

&lt;p&gt;Partial prerendering is a way of storing some static portions of the pages of a website in a cache with dynamic holes streamed in so that when the user requests for it, it gets served to the user at a faster rate. The dynamic holes in question can be the latest news or personal information that needs to be updated, meaning they still have to run in the background while the page has loaded.&lt;/p&gt;

&lt;p&gt;Next 14 introduced PPR which was a way of optimizing pages with static and dynamic rendering. The dynamic pages get wrapped in a Suspense boundary; therefore, allowing it to be streamed when a request is made. However, in the same HTTP Request, the static pages get served as a static HTML shell.&lt;/p&gt;

&lt;p&gt;In Next 15, PPR can now be set for specific pages and layouts by simply adding an experimental_ppr route config option&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import { Suspense } from "react"

import { StaticComponent, DynamicComponent } from "@/app/ui"

//
export const experimental_ppr = true

export default function Page() {

return {

&amp;lt;&amp;gt;

&amp;lt;StaticComponent /&amp;gt;

&amp;lt;Suspense fallback={...}&amp;gt;

&amp;lt;DynamicComponent /&amp;gt;

&amp;lt;/Suspense&amp;gt;

&amp;lt;/&amp;gt;

};

}


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

&lt;/div&gt;

&lt;p&gt;To enable the option used in the page above, you are expected to set the &lt;code&gt;experimental.ppr&lt;/code&gt; config in your next.config.js file to 'incremental':&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 javascript
const nextConfig = {

experimental: {

ppr: 'incremental',

},

};


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

&lt;/div&gt;

&lt;p&gt;module.exports = nextConfig;&lt;/p&gt;

&lt;h3&gt;
  
  
  Executing Code after a response with next/after
&lt;/h3&gt;

&lt;p&gt;In Next 15, there is now a way to perform tasks after a response has been served to the user. These tasks can include logging, analytics, and other external system synchronization. Although, performing this type of task can be very challenging. Using &lt;code&gt;after()&lt;/code&gt; as an experimental API solves the problem; allowing you to schedule work to be processed after streaming your response.&lt;/p&gt;

&lt;p&gt;To enable it in your project, add this to your config file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 javascript
const nextConfig = {

experimental: {

after: true,

},

};

module.exports = nextConfig;


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

&lt;/div&gt;

&lt;p&gt;After that, import the function in your server component as shown below:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;br&gt;
 javascript&lt;br&gt;
import { unstable_after as after } from 'next/server';&lt;br&gt;
import { log } from '@/app/utils';

&lt;p&gt;export default function Layout({ children }) {&lt;br&gt;
  // Secondary task&lt;br&gt;
  after(() =&amp;gt; {&lt;br&gt;
    log();&lt;br&gt;
  });&lt;/p&gt;

&lt;p&gt;// Primary task&lt;br&gt;
  return &amp;lt;&amp;gt;{children}&amp;lt;/&amp;gt;;&lt;br&gt;
}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Create-next-app updates&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Some major updates have also been made to the design of the create-next-app homepage. When you open create-next-app on your localhost, you get to see the new design as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flhxqjj5u1cxpk3u5xgt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flhxqjj5u1cxpk3u5xgt0.png" alt="Create-next-app new design"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Running create-next-app
&lt;/h4&gt;

&lt;p&gt;Also while running create-next-app, there is a new prompt that now asks for the enabling of Turbopack for local development which defaults to no.&lt;/p&gt;

&lt;p&gt;You can enable Turbopack using the –turbo flag&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-next-app@rc --turbo&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimizing bundling of external packages
&lt;/h3&gt;

&lt;p&gt;In Next 14, packages are only bundled in the App router and not on the page router by default.&lt;/p&gt;

&lt;p&gt;So to bundle external packages in the Page router, the transpilePackages config is used; however, you may need to specify each package.&lt;/p&gt;

&lt;p&gt;If there was a need to opt out of a specific package in an App router, it was done using the serverExternalPackages config option.&lt;/p&gt;

&lt;p&gt;In Next 15, a new option is now introduced which is called bundlePagesRouterDependencies;  this option allows you to unify the configuration between the App and Pages router which matches with the default automatic bundling of the App router in Next14. You also decide to use the serverExternalPackages option if you want to opt-out specific packages.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;br&gt;
 javascript&lt;br&gt;
const nextConfig = {

&lt;p&gt;// Automatically bundle external packages in the Pages Router:&lt;/p&gt;

&lt;p&gt;bundlePagesRouterDependencies: true,&lt;/p&gt;

&lt;p&gt;// Opt specific packages out of bundling for both App and Pages Router:&lt;/p&gt;

&lt;p&gt;serverExternalPackages: ['package-name'],&lt;/p&gt;

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

&lt;p&gt;module.exports = nextConfig;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;With these new features coming, it will surely be a game changer in web development. So, now that you have a basic understanding of the new features, you can start implementing them in your projects. But note that the work on Next 15 is still in progress before its final release. If you are looking to learn more about the features, you can check their docs &lt;a href="https://nextjs.org/docs" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Happy Coding!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>How to leverage Jest in your JavaScript codebase: A guide to Snapshot Testing</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Sun, 19 May 2024 12:12:58 +0000</pubDate>
      <link>https://forem.com/devmirx/how-to-leverage-jest-in-your-javascript-codebase-a-guide-to-snapshot-testing-p5k</link>
      <guid>https://forem.com/devmirx/how-to-leverage-jest-in-your-javascript-codebase-a-guide-to-snapshot-testing-p5k</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;Outline&lt;/p&gt;

&lt;p&gt;What is jest&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Features &lt;/li&gt;
&lt;li&gt;How does it work? &lt;/li&gt;
&lt;li&gt;What does it do? &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Snapshot testing&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implementing Snapshot testing with jest &lt;/li&gt;
&lt;li&gt;Updating Reference Snapshot&lt;/li&gt;
&lt;li&gt;Implementing Inline Snapshots&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Outline
&lt;/h2&gt;

&lt;p&gt;There are big challenges that come with testing robust features in production, especially when working with a large codebase. However, When these features are not extensively tested thoroughly, They result in bugs occurring in production. Bugs are very hard to spot. But with unit and comprehensive testing, they can be prevented! In this article,  you will learn how you can leverage jest to test functions and components in a JavaScript codebase. Also, you will learn how to implement snapshot testing for your web application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You must have Node.Js installed on your system at least version 14 or higher. &lt;/li&gt;
&lt;li&gt;You must be familiar with the basic concepts of JavaScript and React.Js.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Jest &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Jest is a delightful javascript framework that helps you as a developer test your javascript codebase easily. However, It is a technology that works with different project types like react, babel, typescript, angular, and vue due to its flexibility.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Features of Jest
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Zero Config: Jest doesn’t require any config, which makes it easier to set up in any project.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Isolated: jest runs tests in a parallelized format, causing them to run simultaneously and at the same time bringing about faster execution and improved performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Great Api: The Jest Library offers a set of Apis and assertions that focus on test simplicity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Snapshot testing: With jest, you can check for any unexpected change of a UI component and compare it to a reference snapshot file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncvwl8nym1z0u5pe8b1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncvwl8nym1z0u5pe8b1v.png" alt="snapshot-file" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Fast and safe: With its parallelized nature, jest makes running tests fast and secure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code Coverage: Jest can also provide extensive test reports of an entire project by just simply adding the —flag coverage. No additional setup is required&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9da79wffw45f15csznxz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9da79wffw45f15csznxz.png" alt="code-coverage" width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy Mocking: You can use the MockApi from jest to inspect some specific calls and then the parameters passed to the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does it work
&lt;/h2&gt;

&lt;p&gt;Jest offers a set of APIs and built-in functions that make testing operations easier to carry out. They include “Expect”,  “toBe” and many more! Additionally, these functions enable us to provide assertions and determine behaviors for our javascript code. Let's see how we can apply them.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Basic Example
&lt;/h2&gt;

&lt;p&gt;In this first example, create a file called sum.js. Inside the file, create a javascript function that adds two numbers and then export it as a module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//sum.js file&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sum&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&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;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="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After it is exported, create a test file and name it sum.test.js; the test file is where you will utilize jest to test the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//sum.test.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&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;./sum&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="s1"&gt;adds 1 + 2 to equal 3&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside a typical test file, built-in functions are imported for test accessibility; however, these functions make assertions about how our code should behave. In the code above, we first call the test function. Then we specify the function expectation by providing the expected value of the sum function.&lt;/p&gt;

&lt;p&gt;After that, add this to your package.json&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;"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;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;Then type this command on your terminal to run your test:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After the test is run successfully, you will see this on your terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxruhetpqepripoqxjxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxruhetpqepripoqxjxa.png" alt="test-success" width="678" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations, you have just run your first test!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Another Example
&lt;/h2&gt;

&lt;p&gt;Let's consider another example. In this example, create a multiply.js file. Inside the file, create a javascript function that multiplies two numbers as shown in the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//multiply.js file

function multiply(a, b) {
  return a * b;
}
module.exports = multiply;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a test file where you will use jest to test&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//multiply.test.js
const multiply = require('./multiply');

test('multiply 1 and 2 to equal 2', () =&amp;gt; {
  expect(multiply(1, 2)).toBe(2);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the code provided above, we first import the multiply file. Then, we run the test cases for the imported javascript function which in this case is &lt;code&gt;multiply.js&lt;/code&gt;. After that, we call the &lt;code&gt;test&lt;/code&gt; function and then specify the function expectation by providing the expected value of the multiply function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Snapshot Testing: &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;It is a form of testing that checks for an unexpected change in the render of a UI component. The test case renders the UI component, takes a snapshot, and then compares the output to the reference snapshot file that was created by Jest on the initial test. If two snapshots don’t match each other, the test fails. This could either be caused by an unexpected change in the render of the UI component or the reference snapshot having an old implementation that was meant to be updated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Snapshot testing with jest
&lt;/h3&gt;

&lt;p&gt;Let's consider react as a project example for testing. In React, there is something known as the dom renderer. This is a tool that does the work of rendering the whole application. However, if we are looking to test the render of the UI component, there will not be a need to render the whole application, but only the UI component for performance reasons. To Render only the UI component that we will implement with snapshot testing, we will use a test renderer! The test renderer creates a lightweight serializable representation of the react tree, preventing the rendering of the entire dom, and allowing the test to be executed faster.&lt;/p&gt;

&lt;p&gt;The code below illustrates the implementation of the test renderer with snapshot testing.&lt;/p&gt;

&lt;p&gt;import renderer from 'react-test-renderer';&lt;br&gt;
import Link from '../Link';&lt;/p&gt;

&lt;p&gt;it('renders correctly', () =&amp;gt; {&lt;br&gt;
 const tree = renderer&lt;br&gt;
   .create(Hashnode)&lt;br&gt;
   .toJSON();&lt;br&gt;
 expect(tree).toMatchSnapshot();&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;From the code above, the test renderer creates a serializable value for our Link Component (which represents the “react tree”) and matches it with the reference snapshot file.&lt;/p&gt;

&lt;p&gt;On the initial test run, this is how the created reference snapshot looks like:&lt;/p&gt;

&lt;p&gt;exports[&lt;code&gt;renders correctly 1&lt;/code&gt;] = `&lt;br&gt;
&amp;lt;a&lt;br&gt;
 className="normal"&lt;br&gt;
 href="&lt;a href="http://www.hashnode.com"&gt;http://www.hashnode.com&lt;/a&gt;"&lt;br&gt;
 onMouseEnter={[Function]}&lt;br&gt;
 onMouseLeave={[Function]}&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hashnode&lt;br&gt;
&lt;br&gt;
`;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After it is created, the snapshot artifact gets committed alongside code changes and code review. Therefore, On subsequent test runs, jest compares the rendering output with the previous snapshot to check for an unexpected change.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating Reference Snapshot
&lt;/h3&gt;

&lt;p&gt;There are different reasons why they can be failed test cases for the rendering of the UI. It could either be due to an unmatched snapshot or an Intentional change in UI implementation. Now, let's talk about the intentional change in the UI implementation. &lt;/p&gt;

&lt;p&gt;Let’s take this as an example. Here, we update the href address for the Link component as shown in the code below:&lt;/p&gt;

&lt;p&gt;import renderer from 'react-test-renderer';&lt;br&gt;
import Link from '../Link';&lt;/p&gt;

&lt;p&gt;it('renders correctly', () =&amp;gt; {&lt;br&gt;
 const tree = renderer&lt;br&gt;
   .create(Devto)&lt;br&gt;
   .toJSON();&lt;br&gt;
 expect(tree).toMatchSnapshot();&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;However, when we decide to run a test, the test is expected to fail because the href address in the rendered output doesn’t match the one in the previous snapshot. &lt;/p&gt;

&lt;p&gt;The test output can be shown in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Few9okz1x4qpgt46jtfe3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Few9okz1x4qpgt46jtfe3.png" alt="Snapshot_failed" width="770" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, to resolve this, we are expected to update the snapshot artifact by simply running jest with a flag that tells jest to update the previous snapshot:&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Inline Snapshots
&lt;/h3&gt;

&lt;p&gt;With Jest, you can create inline snapshots for your source code without having to go to an external file with the .snap extension. Inline snapshots are kinda similar to external snapshots, But they generate automatic inline snapshots when you perform subsequent tests.&lt;/p&gt;

&lt;h4&gt;
  
  
  A Basic Example
&lt;/h4&gt;

&lt;p&gt;In this example; write the test function that tests the render of the ui component or tree as shown in the code below: &lt;/p&gt;

&lt;p&gt;it('renders correctly', () =&amp;gt; {&lt;br&gt;
 const tree = renderer&lt;br&gt;
   .create(Example Site)&lt;br&gt;
   .toJSON();&lt;br&gt;
 expect(tree).toMatchInlineSnapshot();&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;However, when we run the test; the generated snapshot is automatically saved alongside the code changes. Therefore, the generated snapshot is then written as an argument inside the toMatchInlineSnapshot function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnxptjqyolocs5hutwiz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnxptjqyolocs5hutwiz.png" alt="inline-match" width="800" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next time you decide to run the test, the rendered output of the tree gets compared to the generated snapshot inside the toMatchInlineSnapshot function.&lt;/p&gt;

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

&lt;p&gt;I hope you now understand what snapshot testing in jest is all about and how it can be implemented in a javascript codebase. If you are looking to learn more about Jest, you can check out their documentation &lt;a href="https://jestjs.io/"&gt;here&lt;/a&gt;. Happy Coding!! &lt;/p&gt;

</description>
      <category>testing</category>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Create Simple Sketching Art with p5.js: A Step-by-Step Tutorial</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Wed, 01 May 2024 18:48:00 +0000</pubDate>
      <link>https://forem.com/devmirx/how-to-create-simple-sketching-art-with-p5js-a-step-by-step-tutorial-ama</link>
      <guid>https://forem.com/devmirx/how-to-create-simple-sketching-art-with-p5js-a-step-by-step-tutorial-ama</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;Outline&lt;br&gt;
What is p5.js&lt;br&gt;
What does it do&lt;br&gt;
Setting up your environment&lt;br&gt;
P5.js simple coding examples&lt;br&gt;
Interaction and Event Listeners&lt;br&gt;
Create Interactive Sketching Art with the mouse movements&lt;br&gt;
Conclusion&lt;/p&gt;

&lt;h2&gt;
  
  
  Outline &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine creating interactive visuals, photo filters, simulations, and generative art with just a few lines of code 😱?&lt;/p&gt;

&lt;p&gt;Cool, right? 😅&lt;/p&gt;

&lt;p&gt;p5.js makes this possible with pre-written functions making coding accessible to everyone. &lt;/p&gt;

&lt;p&gt;In this article, I will be guiding you through how to create simple interactive art in p5.js &lt;/p&gt;

&lt;h2&gt;
  
  
  What is p5.js &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

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

&lt;p&gt;It is an open-source javascript library that covers the part of creative coding making coding accessible and inclusive to not just developers but also to designers, artists, educators, and everyone obsessed with art.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does p5.js do? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Data Visualization: with p5.js, you can create data visualizations, charts, and graphs that present complex information in a more engaging manner.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Art and Animation: it is also used to create fine art and interactive storytelling, enabling artists to express their creativity only through code.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Simulation and Modelling: p5.js can be used to create simulations and models usually for scientific or educational purposes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fhg6skch3hmdnjkjnygom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fhg6skch3hmdnjkjnygom.png" alt="Simulation and Modelling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generative Art: p5.js allows you to generate mesmerizing visuals just by the use of maths concepts and algorithms.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Image Processing and Photo Filters: p5.js also allows you as a photographer to turn digital images into Pointillist masterpieces!&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  How does p5.js work?
&lt;/h2&gt;

&lt;p&gt;Before we start the coding start, let's delve into how p5.js works. &lt;/p&gt;

&lt;p&gt;If you have been reading from the start or have any basic knowledge of the p5.js library, you should already know that p5.js provides a seamless way of creating interactive and fine art. &lt;/p&gt;

&lt;p&gt;But do you know how it works?&lt;/p&gt;

&lt;p&gt;p5.js provides two primary built-in functions &lt;code&gt;setup&lt;/code&gt; and &lt;code&gt;draw&lt;/code&gt;, making it inclusive to even those without a technical background. p5.js uses these primary functions to make sketches and drawings. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fex2mj0x492oboqnv9t5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fex2mj0x492oboqnv9t5f.png" alt="P5.js Builtin-Funtions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a breakdown of the primary functions and how they work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup: This Built-In Function is responsible for the canvas configuration, With this function, you can set the canvas width and height.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(400, 400);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Draw: with the &lt;code&gt;draw&lt;/code&gt; function, you can create sketches, draw objects, and add styles to the background of the canvas.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
  background(220);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Setting up your Environment &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;p5.js already has a web editor for you where you can make your first sketch. You can click &lt;a href="https://editor.p5js.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to create your first sketch.&lt;/p&gt;
&lt;h2&gt;
  
  
  p5.js Coding Examples &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In this section, I will be providing simple examples that will get you started with p5.js&lt;/p&gt;

&lt;p&gt;For the first sample, I will be showing you how to draw a circle&lt;/p&gt;
&lt;h3&gt;
  
  
  Drawing a Circle with p5.js
&lt;/h3&gt;

&lt;p&gt;There are varieties of shapes that you can create with p5.js. They are normally created by the call of the shape name as a function. However, for this part, we will be creating a circle.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Very important: To test out the code samples, ensure to use the p5.js web editor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To draw a circle,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create your setup function.  Inside your setup function, initialize the createCanvas function and pass it parameters that serve as the width and height of the canvas as shown below. &lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
createCanvas(400, 400)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;After that, create a draw function. This function is where you create your circle sketch. &lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
 //make your drawings here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Inside your draw function, Create your circle by calling the name as a function. Next, create a background for your circle with the background function, and then a describe function that serves as a screen reader-accessible description of the canvas.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
  circle(50, 50, 25)
describe("A white circle with black outline in the middle of a gray canvas.")
}

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

&lt;/div&gt;

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

&lt;p&gt;That’s it!! You now have a basic white circle with a gray background.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interaction and Event Listeners &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Interactions are created in p5.js when a function is invoked as a response to a user action. The function invoked can also be called an &lt;strong&gt;event listener&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;For example. When a mouse is pressed over an element, we call that an action or event. This action invokes a function(event listener) known as “mousePressed”. &lt;/p&gt;

&lt;p&gt;There are other event listeners that are used in p5.js, examples of them include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;mouseOver&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mouseEnter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mouseLeave&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating a basic interaction
&lt;/h2&gt;

&lt;p&gt;In this section, you will learn how interactions are created with eventListeners in p5.js. &lt;/p&gt;

&lt;p&gt;To create an interaction in p5.js, follow the instructions as shown below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a setup function. inside the setup function, create a canvas function, pass it parameters, and assign it to a variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  // Create a canvas element and
  // assign it to cnv.
  let cnv = createCanvas(100, 100);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;After that, create a background for your canvas inside the function. &lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  // Create a canvas element and
  // assign it to cnv.
  let cnv = createCanvas(100, 100);

  background(200);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Then, create the mouseOver event for your canvas variable and pass it a function called "randomColor" which we will create later:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  // Create a canvas element and
  // assign it to cnv.
  let cnv = createCanvas(100, 100);

  background(200);

  // Call randomColor() when the
  // mouse moves onto the canvas.
  cnv.mouseOver(randomColor);

  describe('A gray square changes color when the mouse moves onto the canvas.');
}

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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Finally, create the randomColor function that is invoked or triggered due to the event or action.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function randomColor() {
  let c = random(['red', 'yellow', 'blue', 'green']);
  background(c);
}

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

&lt;/div&gt;

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

&lt;p&gt;There you have it!! You now have a canvas that changes color when the mouse moves onto the canvas.&lt;/p&gt;

&lt;p&gt;Now that you have understood how to build a simple interactive visual in p5.js, let's delve further!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating simple interactive sketching Art with the mouse movements &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In this section, I will walk you through the process of creating simple interactive sketches with the mouse position. &lt;/p&gt;

&lt;p&gt;There are specific system variables that keep track of the mouse position in P5.js. These positions can either be horizontal or vertical.&lt;/p&gt;

&lt;p&gt;Examples of the system variables include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;MouseX: it keeps track of the horizontal position&lt;/li&gt;
&lt;li&gt;MouseY: it keeps track of the vertical Position.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By default, in 2d mode, the mouse variables keep track of the mouse position relative to the top-left corner of the canvas. When it is in WEBL mode, it keeps track of the mouse’s position relative to the center of the canvas. Still a bit confused? don’t worry, I will explain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2D Mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's take this code for an example in 2d mode as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(100, 100);

  describe("A vertical black line moves left and right following the mouse's x-position.");
}

function draw() {
  background(200);

  // Draw a vertical line that follows the mouse's x-coordinate.
  line(mouseX, 0, mouseX, 100);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you try this out on the web editor, you will notice that the line moves with the mouse position from the left edge to the right edge of the canvas. &lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;The top left corner of the canvas coordinate is initially taken to be (0,0). So, when you move the position of the mouse on the canvas from the horizontal position, the line moves from 0 to 100 which was passed as parameters to the line on the code provided above.&lt;/p&gt;

&lt;p&gt;To display the line coordinates in 2d mode, add these few lines of code to the draw function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
  background(200);

  // Style the text.
  textAlign(CENTER);
  textSize(16);

  // Draw a vertical line that follows the mouse's x-coordinate.
  line(mouseX, 0, mouseX, 100);

  // Display the mouse's coordinates.
  text(`x: ${mouseX} y: ${mouseY}`, 50, 50);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;WEBL mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While in the WEBL mode, this is how the code looks like: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(100, 100, WEBGL);

  describe("A vertical black line moves left and right following the mouse's x-position.");
}

function draw() {
  background(200);

let mx = mouseX - 50;

  // Draw a vertical line that follows the mouse's x-coordinate.
  line(mx, -50, mx, 50);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you try this out on the web editor, you will notice that the line moves with the mouse position from the center of the canvas. &lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;The center of the canvas coordinate is initially taken to be (0,0). So, when you move the position of the mouse on the canvas from the horizontal position, the line moves from -50 to 0 and then to 50 which was passed as parameters to the line on the code provided above.&lt;/p&gt;

&lt;p&gt;To also display the line coordinates in WEBL mode, add these few lines of code to the draw function.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {&lt;br&gt;
  background(200);

&lt;p&gt;// Style the text.&lt;br&gt;
  textAlign(CENTER);&lt;br&gt;
  textSize(16);&lt;/p&gt;

&lt;p&gt;let mx = mouseX - 50&lt;/p&gt;

&lt;p&gt;// Draw a vertical line that follows the mouse's x-coordinate.&lt;br&gt;
  line(mx, -50, mx, 50);&lt;/p&gt;

&lt;p&gt;// Display the mouse's coordinates.&lt;br&gt;
  text(&lt;code&gt;x: ${mouseX} y: ${mouseY}&lt;/code&gt;, 50, 50);&lt;br&gt;
}&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion &lt;a&gt;&lt;/a&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;With p5.js, you can create fine art and make beautiful sketches whether you are a developer or not. Again if you are looking for a suitable environment to code in p5.js syntax and show your creative skills, you should try out the &lt;a href="https://editor.p5js.org" rel="noopener noreferrer"&gt;p5.js web editor&lt;/a&gt; and check their &lt;a href="https://p5js.org/reference/" rel="noopener noreferrer"&gt;reference page&lt;/a&gt; to learn more. Happy Coding!!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Learn React Hydration in 5 minutes</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Mon, 22 Apr 2024 14:49:59 +0000</pubDate>
      <link>https://forem.com/devmirx/learn-react-hydration-in-5-minutes-1fjh</link>
      <guid>https://forem.com/devmirx/learn-react-hydration-in-5-minutes-1fjh</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;At some point in your journey as a developer, You must have likely heard of the term "hydration", but may not seem to understand how it works fully.  In this article, I will be covering everything you need to know about hydration as a developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is React hydration?
&lt;/h2&gt;

&lt;p&gt;React Hydration is simply the process of adding event listeners to dom nodes and making an application fully interactive on the client side&lt;/p&gt;

&lt;p&gt;In a Nextjs Application, when the client sends a request to the server, the server responds back with a pre-rendered html(server-generated markup) page. The pre-rendered page is then sent to the client side which populates the dom. After it is rendered on the client side, React takes over and adds event listeners to the server pre-rendered html(server-generated markup) populated on the client side bringing about interactivity. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw12v4kvc44uflz884vj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw12v4kvc44uflz884vj.png" alt="Hydration 2" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvnj594zuov2yfglrkn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvnj594zuov2yfglrkn9.png" alt="Hydration2.3" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deeper detail of what happens during Request Time
&lt;/h2&gt;

&lt;p&gt;During Request time, the following things listed below explain clearly what happens behind the scenes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A preview of a non-interactive HTML is initially sent to the client from the server &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React Server component payload: The React Server component which contains the data sent from the server to the client ensures that the client is in sync with the server bringing about the consistency of user interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, the javascript instructions from the client side now hydrate the html page allowing it to be fully interactive.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Common Hydration Errors and how to resolve them
&lt;/h2&gt;

&lt;p&gt;At some point, when you develop an application with nextjs, you might have come across this famous error. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12bu4ckjampi7bkvryrh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F12bu4ckjampi7bkvryrh.png" alt="Hydration Error" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This error can be very hard to debug and fix, especially when it occurs in a big application. &lt;/p&gt;

&lt;h2&gt;
  
  
  What exactly is a hydration error?
&lt;/h2&gt;

&lt;p&gt;The hydration error is the error that stems from the mismatch of the server and client component's states and generated markup.&lt;/p&gt;

&lt;h2&gt;
  
  
  What causes it?
&lt;/h2&gt;

&lt;p&gt;Just Like I explained earlier on hydration, In a Nextjs Application, React components are first rendered on the server before they are transformed into a pre-rendered html that is then sent to the client before the hydration process happens. However, during the process, if the javascript instructions executed on the client don't match the pre-rendered html that is sent from the server, hydration errors can occur.&lt;/p&gt;

&lt;p&gt;There are several causes of hydration errors we face in our nextjs application, let's look at some of them as we proceed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Asynchronous rendering:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This involves trying to asynchronously render a simple component using &lt;code&gt;getServerSideProps&lt;/code&gt; and &lt;code&gt;getInitialProps&lt;/code&gt;. The getInitialProps plays a huge part in delaying the required data before it gets to the client side. if the client-side hydration process happens before the required data is loaded, hydration errors occur.&lt;/p&gt;

&lt;p&gt;Data-Fetching: Nextjs uses React APIs to render the server components into a special data format, the format is fetched and sent to the client. If the data the client expects doesn't match with the one sent from the server, hydration errors occur.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Third-Party Libraries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nextjs as a react framework rely heavily on third-party libraries. These third-party libraries have their lifecycle rendering process which can conflict with nextjs rendering process leading to hydration errors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing code that relies on the window object:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is one of the major causes of hydration error that occurs in a nextjs application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{user.status === "loggedIn" ? (

    {/* condition when the user is logged in*/}

    ) : typeof window !== "undefined" &amp;amp;&amp;amp; window.clientInformation.vendor ? (

    {/* condition when  */}

    ) : (

    &amp;lt;p&amp;gt;
     A user is logged in 
    &amp;lt;/p&amp;gt;
)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you take a look at the code above, you will notice that it relies on a window object to execute a certain action. it is important to know that the window object in question is not available on the server enabling the pre-rendered html page not to match the client javascript code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Implement the UseEffect Logic on the client side: In the problem I shared above before this section, I showed you how the window object can be a cause of hydration error in a nextjs application. I also explained that in the server, the window object does not exist, which causes a mismatch of the server-generated markup and the client javascript functions needed to reconcile that same server-generated markup. So to resolve this issue, a useEffect is needed.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from 'react';

function MyComponent() {
  const [isClientAvailable, setIsClientAvailable] =     useState(false);

  useEffect(() =&amp;gt; {
    if (typeof window !== 'undefined' &amp;amp;&amp;amp; window.clientInformation.vendor) {
      setIsClientAvailable(true);
    }
  }, []);

  return (
    &amp;lt;&amp;gt;
    {user.status === "loggedIn" ? (

    {/* condition when the user is logged in*/}

    ) : isClientAvaliable ? (

    {/* condition when there is client  ] */}

    ) : (

    &amp;lt;p&amp;gt;
     Oops no client
    &amp;lt;/p&amp;gt;
   )}
    &amp;lt;/&amp;gt;
  );
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Disable pre-rendering for certain components: There are certain components that don't need to undergo server-side components, so disabling ssr will be a good option to avoid hydration errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consistency in Fetching data: Ensure the use of Next.js hooks like getStaticProps and getServerSideProps during SSR to fetch data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;So now that you have fully understood hydration, its errors, the causes of those errors, and best practices to avoid them, you will be able to build highly scalable and powerful applications as a developer. Ensure to keep these best practices and guides in mind, and write good tests for your application so that you don't get fazed by hydration errors when debugging in your application. I hope I have been able to simplify everything related to hydration for you. &lt;/p&gt;

&lt;p&gt;I'd love to connect with you on &lt;a href="https://twitter.com/LordCodeX_"&gt;Twitter&lt;/a&gt; | &lt;a href="https://www.linkedin.com/in/daniel-adeniran-lordcodex-2535a31b7/"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://github.com/LordCodex164"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See you in my next blog article. Happy Coding!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nextjs</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Build a Discord Bot with Discord.js V14: A Step-by-Step Guide</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Fri, 12 Apr 2024 12:38:33 +0000</pubDate>
      <link>https://forem.com/devmirx/build-a-custom-discord-bot-with-discordjs-v14-a-step-by-step-guide-3h2f</link>
      <guid>https://forem.com/devmirx/build-a-custom-discord-bot-with-discordjs-v14-a-step-by-step-guide-3h2f</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExdTB1bjV6Y2RiOGM0eTB2Y2t5MnJpYjZpa2diejZ6dW1ia2FvYnNmYSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o7TKEP6YngkCKFofC/giphy-downsized.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExdTB1bjV6Y2RiOGM0eTB2Y2t5MnJpYjZpa2diejZ6dW1ia2FvYnNmYSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3o7TKEP6YngkCKFofC/giphy-downsized.gif" width="480" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes when we are overloaded with too many boring and numerous mundane activities we think of ways to automate them. Consequently, bots and AI tools are created to make our lives easier.&lt;/p&gt;

&lt;p&gt;In this article, I will explore how to create a custom discord bot with Discordv14 that responds to slash messages. &lt;/p&gt;

&lt;p&gt;Sounds interesting? &lt;/p&gt;

&lt;p&gt;Alright let begin!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExdGE0Yzd4azFweDJucTNzN3EyZnY3cjZ4aTZydmdsajhpNTZsZmVweiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/64aBXTVfd90zyUH2da/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExdGE0Yzd4azFweDJucTNzN3EyZnY3cjZ4aTZydmdsajhpNTZsZmVweiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/64aBXTVfd90zyUH2da/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A good knowledge of Javascript&lt;/li&gt;
&lt;li&gt;A basic knowledge of Nodejs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intro To Discord.js.
&lt;/h2&gt;

&lt;p&gt;Before you start building, do you know what discord.js is and what it does?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExNG1tOWUyb3ppdXNlYXN6ZmJsYmczZjhhYndubHlyNDFxZHV4Z2NraSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QfzMP70zmNQiDf5sGP/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExNG1tOWUyb3ppdXNlYXN6ZmJsYmczZjhhYndubHlyNDFxZHV4Z2NraSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QfzMP70zmNQiDf5sGP/giphy.gif" width="540" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No Problem, I will explain:&lt;/p&gt;

&lt;p&gt;Discord.js is a powerful Node.js module that allows you to interact with the Discord API very much easily. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y2yltlc1a1ofhgqsd0s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y2yltlc1a1ofhgqsd0s.png" alt="Discord.js" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzlmsa4yolqrcp7klb30g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzlmsa4yolqrcp7klb30g.png" alt="Image description" width="800" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you aren’t aware, discord.js just pushed their latest release on their docs(discord.js v14). You can check out what's new &lt;a href="https://discordjs.guide/whats-new.html"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hn1vUcLP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://th.bing.com/th/id/OIP.-rrFjvtf_jqw40EikHCbGwHaDt%3Frs%3D1%26pid%3DImgDetMain" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hn1vUcLP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://th.bing.com/th/id/OIP.-rrFjvtf_jqw40EikHCbGwHaDt%3Frs%3D1%26pid%3DImgDetMain" width="474" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing Node.js&lt;/strong&gt;&lt;br&gt;
Very Important: To use discord.js, you'll need to install &lt;a href="https://nodejs.org"&gt;Node.js&lt;/a&gt;. discord.js v14 requires Node v16.11.0 or higher.&lt;/p&gt;

&lt;p&gt;To check if you already have Node installed on your machine (e.g., if you're using a VPS), run &lt;code&gt;node -v&lt;/code&gt; in your terminal. If it outputs v16.11.0 or higher, then you're good to go! &lt;br&gt;
Alright, Let's Continue.&lt;/p&gt;

&lt;p&gt;On Windows, it's as simple as installing any other program. Download the latest version from the &lt;a href="https://nodejs.org/en/download"&gt;Node.js website&lt;/a&gt;, open the downloaded file, and follow the steps from the installer.&lt;/p&gt;

&lt;p&gt;On macOS, either:&lt;/p&gt;

&lt;p&gt;Download the latest version from the &lt;a href="https://nodejs.org"&gt;Node.js website&lt;/a&gt;, open the package installer, and follow the instructions&lt;br&gt;
Use a package manager like &lt;code&gt;Homebrew&lt;/code&gt; with the command&lt;br&gt;
&lt;code&gt;brew install node&lt;/code&gt;&lt;br&gt;
On Linux, you can consult this page to determine how you should install Node.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing the essentials
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;To use discord.js, you'll need to install it via &lt;a href="https://www.npmjs.com"&gt;npm&lt;/a&gt; (Node's package manager). npm comes with every Node installation, so you don't have to worry about installing that. However, before you install anything, you should set up a new project folder.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Navigate to a suitable place on your machine and create a new folder named "discord-bot" (or whatever you want). Next you'll need to open your terminal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Opening the terminal&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you use Visual Studio Code, you can press Ctrl + ` (backtick) to open its integrated terminal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Running on Windows&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On Windows, either:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Shift&lt;/code&gt; + &lt;code&gt;Right-click&lt;/code&gt; inside your project directory and choose the "Open command window here" option&lt;br&gt;
Press &lt;code&gt;Win + R&lt;/code&gt; and run &lt;code&gt;cmd.exe&lt;/code&gt;, and then &lt;code&gt;cd&lt;/code&gt; into your project directory&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running on macOS&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;On macOS, either:&lt;/p&gt;

&lt;p&gt;Open Launchpad or Spotlight and search for "Terminal"&lt;/p&gt;

&lt;p&gt;In your "Applications" folder, under "Utilities", open the Terminal app&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running on Linux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On Linux, you can quickly open the terminal with &lt;code&gt;Ctrl + Alt + T&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the terminal open, run the &lt;code&gt;node -v&lt;/code&gt; command to make sure you've successfully installed Node.js. If it outputs v16.11.0 or higher on the terminal, then you are good to go!!&lt;/p&gt;

&lt;h1&gt;
  
  
  Initiating a project folder with NPM
&lt;/h1&gt;

&lt;p&gt;To start building, ensure to run the following command to set up your project for you.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once you're done with that, you're almost ready to install discord.js!&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing discord.js
&lt;/h2&gt;

&lt;p&gt;Now that you've installed Node.js and know how to open your console and run commands, you can finally install &lt;a href="https://discord.js.org/"&gt;discord.js!&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Next, run the following command in your terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install discord.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it! With all the necessities installed, you're almost ready to start coding your bot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linter Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With linter, it is easier to spot bugs and errors in our code, it is not required but recommended. You can check &lt;a href="https://discordjs.guide/preparations/setting-up-a-linter"&gt;here&lt;/a&gt; on how to install it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup a Bot Application
&lt;/h2&gt;

&lt;p&gt;Now that you have followed the steps outlined above, I guess the real cooking can start!!!.&lt;/p&gt;

&lt;p&gt;To set up your bot application, follow these steps:&lt;/p&gt;

&lt;p&gt;Open the&lt;a href="https://discord.com/developers/applications"&gt;Discord developer portal&lt;/a&gt; and log into your account.&lt;/p&gt;

&lt;p&gt;Click on the &lt;strong&gt;New Application&lt;/strong&gt; button.&lt;br&gt;
Enter a name and confirm the pop-up window by clicking the &lt;strong&gt;Create&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmptpid2qwtjbrm9ih85q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmptpid2qwtjbrm9ih85q.png" alt="Image description" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can edit your application name, description, and avatar however you wish. &lt;/p&gt;

&lt;h3&gt;
  
  
  Getting a Token
&lt;/h3&gt;

&lt;p&gt;The token is basically what your bot uses to log into your discord, you can regenerate it but need your password for it to happen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pls ensure you don’t share your token with anyone to avoid malicious acts&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Try generating the token which will require you to enter the password for your discord account as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xgzzb22p15mmy1cgerr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xgzzb22p15mmy1cgerr.png" alt="generate-token" width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding the bot to servers
&lt;/h3&gt;

&lt;p&gt;You need to add the bot to servers to test and interact with its functionality. To create a bot, an invite link is required. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Invite Link?
&lt;/h3&gt;

&lt;p&gt;An invite link is Discord's standard structure for authorizing an OAuth2 application (such as your bot application) for entry to a Discord server.&lt;/p&gt;

&lt;p&gt;Follow these steps to get your invite link:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Navigate to the MyApps Page Under the application&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the bot created&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Head to the OAuth page &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on OAuth generator page&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Select the bot and &lt;code&gt;application.commands&lt;/code&gt;, when you select them, you are given a list of permissions to configure for your bot&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F266soejbpjentvm8y1dg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F266soejbpjentvm8y1dg.png" alt="app.commands" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Grab the link via the "Copy" button and enter it in your browser. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F174l6qzf0pgd1noevtib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F174l6qzf0pgd1noevtib.png" alt="copy commands" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see something like this (with your bot's username and avatar):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0hjxst0yo6ghzvrw0vh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0hjxst0yo6ghzvrw0vh.png" alt="bot username" width="800" height="854"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations, you have just added the bot to the server!!. &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Bot
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Creating config files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that you have created your bot and added it to the server, it is time to start the coding part and get it online!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating Config.json&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, create the &lt;code&gt;config.json&lt;/code&gt; file. This is where you will store your private values like your tokens. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating the main File&lt;/strong&gt;&lt;br&gt;
Inside the root of your project, create a file and name it &lt;code&gt;index.js&lt;/code&gt;. Inside the file, copy this code snippet:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
const fs = require('node:fs');&lt;br&gt;
const path = require('node:path');&lt;br&gt;
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');&lt;br&gt;
const { token } = require('./config.json');&lt;/p&gt;

&lt;p&gt;const client = new Client({ intents: [GatewayIntentBits.Guilds] });&lt;/p&gt;

&lt;p&gt;client.once(Events.ClientReady, readyClient =&amp;gt; {&lt;br&gt;
    console.log(&lt;code&gt;Ready! Logged in as ${readyClient.user.tag}&lt;/code&gt;);&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;client.login(token);&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fts72psa4ppk93o46vvyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fts72psa4ppk93o46vvyw.png" alt="creating main file" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above, here are the basic things to understand:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A client instance is created for the bot  so that it logins the bot in with a token&lt;/li&gt;
&lt;li&gt;The GatewayIntentBits allows the discord.js client to work the way it is expected to&lt;/li&gt;
&lt;li&gt;The intent allows you to define events that discord send to your bot when triggered.
&lt;/li&gt;
&lt;li&gt;Guild is your server id&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that you have understood these basic things, hopefully, you are ready for the next phrase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Your Application
&lt;/h2&gt;

&lt;p&gt;To run your application, open your terminal inside your visual code editor. Press this command to run your project&lt;/p&gt;

&lt;p&gt;&lt;code&gt;node index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Congratulations, you have just successfully run your project. The next step is to create slashing commands&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Slash Commands
&lt;/h2&gt;

&lt;p&gt;With the Discord Client, you can create slash commands&lt;/p&gt;

&lt;p&gt;Slash commands provide a huge number of benefits over manual message parsing, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Integration with the Discord client interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Automatic command detection and parsing of the associated options/arguments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Typed argument inputs for command options, e.g. "String", "User", or "Role".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validated or dynamic choices for command options.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In-channel private responses (ephemeral messages).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pop-up form-style inputs for capturing additional information.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Before Going Further
&lt;/h3&gt;

&lt;p&gt;This is how the structure of your project should look like:&lt;/p&gt;

&lt;p&gt;discord-bot/&lt;br&gt;
├── node_modules&lt;br&gt;
├── config.json&lt;br&gt;
├── index.js&lt;br&gt;
├── package-lock.json&lt;br&gt;
└── package.json&lt;/p&gt;

&lt;h2&gt;
  
  
  Individual Command files
&lt;/h2&gt;

&lt;p&gt;In this section, you will learn how to create command files that help you register and handle commands. Here are important instructions you should follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new folder named commands &lt;/li&gt;
&lt;li&gt;Create a subfolder named utility, which is where you store all your utility command files. &lt;/li&gt;
&lt;li&gt;Now, create a &lt;code&gt;server.js&lt;/code&gt; file inside the utility sub-folder, &lt;code&gt;server.js&lt;/code&gt; is an example of a utility file where your slash commands are executed. After that, copy this command below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
const { SlashCommandBuilder } = require('discord.js');&lt;/p&gt;

&lt;p&gt;module.exports = {&lt;br&gt;
    data: new SlashCommandBuilder()&lt;br&gt;
        .setName('server')&lt;br&gt;
        .setDescription('Provides information about the server.'),&lt;br&gt;
    async execute(interaction) {&lt;br&gt;
        await interaction.reply(&lt;code&gt;This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.&lt;/code&gt;);&lt;br&gt;
    },&lt;br&gt;
};&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fij1ki4megpzjy5viprr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fij1ki4megpzjy5viprr3.png" alt="slash-commands" width="800" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the code snippet above, &lt;br&gt;
The data property provides the command definition with its description.&lt;br&gt;
The &lt;strong&gt;execute&lt;/strong&gt; is the asynchronous function that contains the functionality to run from our event handler when the command defined in the data is used.&lt;br&gt;
Then, they are placed inside the module.exports so they are read by other files&lt;/p&gt;

&lt;p&gt;That is it for your basic ping command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Command handling
&lt;/h2&gt;

&lt;p&gt;If you are working on a big project, you are most likely to use command handling. So, let's get started on that!&lt;/p&gt;

&lt;h3&gt;
  
  
  Loading Command files
&lt;/h3&gt;

&lt;p&gt;After you have created your command files, make these new additions inside the &lt;code&gt;index.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;client.commands = new Collection();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yx57pxnm3wcsrzxwefi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yx57pxnm3wcsrzxwefi.png" alt="Loading Command files" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Important: it is recommended to add a commands Property to your client instance so that you can access your commands in other files.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;here are the things to know about the new additions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The Fs is node’s native file system module. It is used to read the commands directory and identify the files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The path is Node’s native path utility module. path helps construct paths to access files and directories. One of the advantages of the path module is that it automatically detects the operating system and uses the appropriate joiners.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Collection class extends JavaScript's native Map class, and includes more extensive, useful functionality. Collection is used to store and efficiently retrieve commands for execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Retrieving your command files
&lt;/h2&gt;

&lt;p&gt;Next, after using the imported modules above, make a few more additions to the index.js file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
for (const folder of commandFolders) {&lt;br&gt;
    const commandsPath = path.join(foldersPath, folder);&lt;br&gt;
    const commandFiles = fs.readdirSync(commandsPath).filter(file =&amp;gt; file.endsWith('.js'));&lt;br&gt;
    for (const file of commandFiles) {&lt;br&gt;
        const filePath = path.join(commandsPath, file);&lt;br&gt;
        const command = require(filePath);&lt;br&gt;
        if ('data' in command &amp;amp;&amp;amp; 'execute' in command) {&lt;br&gt;
            client.commands.set(command.data.name, command);&lt;br&gt;
        } else {&lt;br&gt;
            console.log(&lt;/code&gt;[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.&lt;code&gt;);&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8eosae6uienefgmg5bgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8eosae6uienefgmg5bgo.png" alt="Retrieving Command files" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are few things to know about these additions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The path.join helps in constructing the root path to the command directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The first fs.readdirSync reads the path that is joined to the directory(in this case, the command directory), returns an array of the folder the directory contains, currently [“utility”]&lt;br&gt;
When the first iteration is done inside the commandFolders, you take out each folder from the array(in this case, utility), use the path.join to construct the path to each folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, the second fs.readditSync reads inside of the folder(in ths case, the utility folder that has been constructed to the path and checks for what the utility folder contains, currently [“ping.js”]. To ensure only commands file are only read, in this case javascript files, Array.filter() ensures non-javascript files are removed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, set each command into the client.commands collection&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Receiving and handling command interactions
&lt;/h2&gt;

&lt;p&gt;In this section, you will create event listeners that execute and handle commands whenever there is interaction in your discord application. Inside the index.js file, add the following code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;client.on(Events.InteractionCreate, async interaction =&amp;gt; {&lt;br&gt;
    console.log(interaction)&lt;br&gt;
});&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrd80fh3jz8j4h6p6v2a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkrd80fh3jz8j4h6p6v2a.png" alt="Receiving Commands" width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in discord.js, not every interaction is a slash command, for example, the messageComponent interactions.  So to ensure your code listens for only slash commands,&lt;br&gt;
You will make use of the BaseInteraction#isChatInputCommand() method to handle slash commands and exit the handler if another command type is encountered. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
client.on(Events.InteractionCreate, async interaction =&amp;gt; {&lt;br&gt;
  if (!interaction.isChatInputCommand()) return;&lt;br&gt;
    console.log(interaction)&lt;br&gt;
});&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp8r9voyg0417j8i6ywy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp8r9voyg0417j8i6ywy2.png" alt="commandType" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Executing Commands
&lt;/h3&gt;

&lt;p&gt;When executing commands, we check out the client.commands collection and see if it contains the interaction commandName before executing the code.&lt;/p&gt;

&lt;p&gt;Again, if you check the server.js command file, you will notice there is a data and execute asynchronous  function property defined in the file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx6xy7cjzwprgjmt0bpb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx6xy7cjzwprgjmt0bpb.png" alt="server.js" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside the data property, it contains the name and description of the command defined. Then in the execute function, it contains what happens after the command gets triggered.&lt;/p&gt;

&lt;p&gt;So like I explained from the beginning of this section, a matching command is gotten from the clients.command collection based on the interaction.commandName, if no match is found, the error is logged out, otherwise you execute the command with the interaction passed as the parameter. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
client.on(Events.InteractionCreate, async interaction =&amp;gt; {&lt;br&gt;
    if (!interaction.isChatInputCommand()) return;&lt;br&gt;
    const command = interaction.client.commands.get(interaction.commandName);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!command) {
    console.error(`No command matching ${interaction.commandName} was found.`);
    return;
}

try {
    await command.execute(interaction);
} catch (error) {
    console.error(error);
    if (interaction.replied || interaction.deferred) {
        await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
    } else {
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa273yidujnhylhncg8yf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa273yidujnhylhncg8yf.png" alt="commandName" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats, you have just initiated the method to respond and execute slash commands. Next, you will create a deployment script that registers the slash commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Registering Slash Commands
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Command registration
&lt;/h3&gt;

&lt;p&gt;Command registration is done in two ways; in a specific guild, or in every guild the bot is in. so let get started with single-guild registration first which is the easier way to develop and test commands before a global deployment.&lt;/p&gt;

&lt;p&gt;N.B:   &lt;/p&gt;

&lt;p&gt;Your application will need the &lt;code&gt;applications.commands&lt;/code&gt; scope authorized in a guild for any of its slash commands to appear, and to be able to register them in a specific guild without error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guild Commands
&lt;/h3&gt;

&lt;p&gt;To register and update slash commands for your bot, follow the instructions below &lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;deploy-commands.js&lt;/code&gt; file in your root directory. &lt;br&gt;
Add two more properties to the config.json in your root directory; in this case, your clientId(your application’s client id) and guildId(your development server’s id)&lt;/p&gt;

&lt;p&gt;After following these steps, your &lt;code&gt;config.json&lt;/code&gt; file should look like this following the new additions:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
{&lt;br&gt;
    "token": "your-token-goes-here",&lt;br&gt;
    "clientId": "your-application-id-goes-here",&lt;br&gt;
    "guildId": "your-server-id-goes-here"&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5pkua7ool8vf3r06m79t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5pkua7ool8vf3r06m79t.png" alt="creating the config.json file for discord bot" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With these defined,  copy this deployment script below inside the &lt;code&gt;deploy-command.js&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
const { REST, Routes } = require('discord.js');&lt;br&gt;
const { clientId, guildId, token } = require('./config.json');&lt;br&gt;
const fs = require('node:fs');&lt;br&gt;
const path = require('node:path');&lt;/p&gt;

&lt;p&gt;const commands = [];&lt;br&gt;
// Grab all the command folders from the commands directory you created earlier&lt;br&gt;
const foldersPath = path.join(__dirname, 'commands');&lt;br&gt;
const commandFolders = fs.readdirSync(foldersPath);&lt;/p&gt;

&lt;p&gt;for (const folder of commandFolders) {&lt;br&gt;
    // Grab all the command files from the commands directory you created earlier&lt;br&gt;
    const commandsPath = path.join(foldersPath, folder);&lt;br&gt;
    const commandFiles = fs.readdirSync(commandsPath).filter(file =&amp;gt; file.endsWith('.js'));&lt;br&gt;
    // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment&lt;br&gt;
    for (const file of commandFiles) {&lt;br&gt;
        const filePath = path.join(commandsPath, file);&lt;br&gt;
        const command = require(filePath);&lt;br&gt;
        if ('data' in command &amp;amp;&amp;amp; 'execute' in command) {&lt;br&gt;
            commands.push(command.data.toJSON());&lt;br&gt;
        } else {&lt;br&gt;
            console.log(&lt;code&gt;[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.&lt;/code&gt;);&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// Construct and prepare an instance of the REST module&lt;br&gt;
const rest = new REST().setToken(token);&lt;/p&gt;

&lt;p&gt;// and deploy your commands!&lt;br&gt;
(async () =&amp;gt; {&lt;br&gt;
    try {&lt;br&gt;
        console.log(&lt;code&gt;Started refreshing ${commands.length} application (/) commands.&lt;/code&gt;);&lt;/p&gt;

&lt;p&gt;// The put method is used to fully refresh all commands in the guild with the current set&lt;br&gt;
        const data = await rest.put(&lt;br&gt;
            Routes.applicationGuildCommands(clientId, guildId),&lt;br&gt;
            { body: commands },&lt;br&gt;
        );&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
    // And of course, make sure you catch and log any errors!
    console.error(error);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;})();&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcq9c6816m6egwyc2l5b4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcq9c6816m6egwyc2l5b4.png" alt="deploy-commands" width="800" height="783"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you have copied the script, open another terminal inside your visual code editor. Then press this command &lt;code&gt;node deploy-command.js&lt;/code&gt; to deploy your script.  If you have followed these steps above, you should be able to register your slash commands inside the server your bot is in, run your commands, and see your bot’s response in Discord.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the Bot
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxm379hab129198x7crr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxm379hab129198x7crr.png" alt="Testing the bot" width="800" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;If you have made it this far, give yourself a round of applause. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExNWVxcmh5ajhhMGs5bGt2dmFrbmF2OHB3c3U5MjI2ZHBoZTQ2Z3YydiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QTAVEex4ANH1pcdg16/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExNWVxcmh5ajhhMGs5bGt2dmFrbmF2OHB3c3U5MjI2ZHBoZTQ2Z3YydiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/QTAVEex4ANH1pcdg16/giphy.gif" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, If you aren’t aware, discord recently pushed their latest release on their docs(discord.js v14). You can check &lt;a href="https://discordjs.guide/#before-you-begin"&gt;here&lt;/a&gt; to learn more about what they offer. Happy Coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>bot</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>Conceptual Topics Technical Writers Must Grasp For Effective API Documentation</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Thu, 21 Mar 2024 17:37:41 +0000</pubDate>
      <link>https://forem.com/devmirx/conceptual-topics-technical-writers-must-grasp-for-effective-api-documentation-ldl</link>
      <guid>https://forem.com/devmirx/conceptual-topics-technical-writers-must-grasp-for-effective-api-documentation-ldl</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;One of the things API technical writers must keep in mind when writing API Documentation about a product or service is that users must be able to apply effectively and massively experiment with the information related to the API obtained from the docs. This can only be possible if the API documentation has good conceptual topics - some piece of information also known as the user guide.&lt;br&gt;
In this article, I will be pointing out certain conceptual Topics every technical writer has to include in API documentation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A basic knowledge of &lt;a href="https://www.geeksforgeeks.org/what-is-an-api/"&gt;APIs&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A good understanding of &lt;a href="https://www.bing.com/ck/a?!&amp;amp;&amp;amp;p=6e31720c4290326aJmltdHM9MTcxMDg5MjgwMCZpZ3VpZD0xZDhmZGJjZS1iZGM4LTZiODAtMDFhOS1jODI1YmNkNTZhYTQmaW5zaWQ9NTIwMA&amp;amp;ptn=3&amp;amp;ver=2&amp;amp;hsh=3&amp;amp;fclid=1d8fdbce-bdc8-6b80-01a9-c825bcd56aa4&amp;amp;psq=api+documentation&amp;amp;u=a1aHR0cHM6Ly93d3cucG9zdG1hbi5jb20vYXBpLXBsYXRmb3JtL2FwaS1kb2N1bWVudGF0aW9uLw&amp;amp;ntb=1"&gt;API documentation&lt;/a&gt; is a plus&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding Conceptual Topics in API Documentation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;Conceptual topics are what make up the API Documentation, making it easier for users to navigate through the docs more easily and experiment effectively with the API. &lt;/p&gt;

&lt;h3&gt;
  
  
  Examples of Conceptual Topics in API Documentation
&lt;/h3&gt;

&lt;p&gt;The following are commonly used conceptual topics used in API documentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;API product overview&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Getting started&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authentication and authorization requirements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Status and error codes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rate limiting and thresholds&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Quick reference guide&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;API best practices&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  API Product Overview
&lt;/h2&gt;

&lt;p&gt;This is where a writer talks about the &lt;strong&gt;main&lt;/strong&gt; story of the API. Here are four things you should note when writing a product overview&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is it all about?&lt;/li&gt;
&lt;li&gt;What it does?&lt;/li&gt;
&lt;li&gt;Who is it for?&lt;/li&gt;
&lt;li&gt;Why should I use it as a user?&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep in mind that there are thousands of APIs. If people are browsing your API, their first and most pressing question is, &lt;strong&gt;what information does it provide&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;To tell your product’s story, consider identifying a market need that your product solves one of the main reasons startups fail is their inability to solve a market problem.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcaj9iqzlvxn3v8rj6lpf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcaj9iqzlvxn3v8rj6lpf.png" alt="Product Review Format" width="800" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;This Section is almost the same as a typical &lt;strong&gt;Hello World&lt;/strong&gt; tutorial in a developer documentation platform, but an API is involved in this case. As an API writer, your purpose and goal in this section is to provide end users with a basic use case of the API to produce the &lt;strong&gt;simplest outcome possible&lt;/strong&gt;. So you must allow a new user to have some success with your product, even if the success is small, like getting a one-line value back from an API call. Here are the following topics you can write about in this section:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Signing up for an account&lt;/li&gt;
&lt;li&gt;Getting API keys&lt;/li&gt;
&lt;li&gt;Making a request&lt;/li&gt;
&lt;li&gt;Evaluating the response&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqiay1qnl092590n11b9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqiay1qnl092590n11b9.png" alt="Getting Started Format" width="800" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Authentication and authorization
&lt;/h2&gt;

&lt;p&gt;Before users make requests with your API, they’ll usually need to register for an API key or learn other ways to authenticate, and authorize their requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;First, let’s define the key terms:&lt;br&gt;
&lt;a href="https://www.bing.com/ck/a?!&amp;amp;&amp;amp;p=bcf87edcc1d5bc9fJmltdHM9MTcxMDgwNjQwMCZpZ3VpZD0xZDhmZGJjZS1iZGM4LTZiODAtMDFhOS1jODI1YmNkNTZhYTQmaW5zaWQ9NTI1Ng&amp;amp;ptn=3&amp;amp;ver=2&amp;amp;hsh=3&amp;amp;fclid=1d8fdbce-bdc8-6b80-01a9-c825bcd56aa4&amp;amp;psq=authentication&amp;amp;u=a1aHR0cHM6Ly9hdXRoMC5jb20vaW50cm8tdG8taWFtL3doYXQtaXMtYXV0aGVudGljYXRpb24&amp;amp;ntb=1"&gt;Authentication&lt;/a&gt;: Refers to proving correct identity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.bing.com/ck/a?!&amp;amp;&amp;amp;p=66f630ba05752e21JmltdHM9MTcxMDgwNjQwMCZpZ3VpZD0xZDhmZGJjZS1iZGM4LTZiODAtMDFhOS1jODI1YmNkNTZhYTQmaW5zaWQ9NTIzNg&amp;amp;ptn=3&amp;amp;ver=2&amp;amp;hsh=3&amp;amp;fclid=1d8fdbce-bdc8-6b80-01a9-c825bcd56aa4&amp;amp;psq=authorization&amp;amp;u=a1aHR0cHM6Ly9hdXRoMC5jb20vaW50cm8tdG8taWFtL3doYXQtaXMtYXV0aG9yaXphdGlvbg&amp;amp;ntb=1"&gt;Authorization&lt;/a&gt;: Refers to allowing a certain action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Differences between Authentication and Authorization
&lt;/h2&gt;

&lt;p&gt;An API might authenticate you, but not authorize you to make a certain request. In Authentication, it doesn’t know who you are, thereby, you have to provide an API Key. While in authorization, it knows who you are, but you have to perform a certain action. However, in API documentation, for privacy and security reasons, you don’t need to explain how your authentication works in detail to outside users. In fact, not explaining the internal details of your authentication process is probably a best practice as it would make it harder for hackers to abuse the API.&lt;/p&gt;

&lt;p&gt;However, you do need to explain some necessary information such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How to get API keys&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to authenticate requests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Error messages related to invalid authentication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sensitivity around authentication information&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Token expiration times&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When API requests are authenticated, it helps to prevent malicious users from getting other user-specific data and it also helps to boost a revenue model for companies.&lt;br&gt;
Finally, you could track who is using your API, or what endpoints are most used just by authentication.&lt;/p&gt;

&lt;p&gt;If you have public and private keys, you should explain where each key should be used, and note that private keys should not be shared.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzz998np5h52u3l7aaj70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzz998np5h52u3l7aaj70.png" alt="Authentication of API Keys" width="800" height="1185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  API status and error codes:
&lt;/h2&gt;

&lt;p&gt;Most APIs should have a general page listing responses and error codes across the entire API. A standalone page listing the status codes (rather than including these status codes with each endpoint) allows you to expand on each code with more detail without crowding the other documentation. It also reduces redundancy and the sense of information overload.&lt;br&gt;
Status and error codes can be particularly helpful when it comes to troubleshooting. As such, you can think of these error codes as complementary to a section on troubleshooting. for example, whether the request was successful (200), resulted in a server error (500), or had authorization issues (403). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkw79dhiss9izbvahzxhr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkw79dhiss9izbvahzxhr.png" alt="Paystack Response Status Code Page" width="800" height="1125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  API rate limiting and thresholds:
&lt;/h2&gt;

&lt;p&gt;Rate limits determine how frequently you can call a particular endpoint. Companies with APIs boost their revenue when they charge for access to their API, which is why they offer low and high usage often making the low usage options free so that developers can explore and experiment with the API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Far22g6t016lhd4tyke4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Far22g6t016lhd4tyke4d.png" alt="Rate Limiting" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  API quick reference:
&lt;/h2&gt;

&lt;p&gt;The quick reference guide helps users get a glimpse of the system as a whole, often by providing a list of the API’s endpoints. Whether for end-user documentation or developer documentation, the quick reference guide provides a 1-2-page guide that provides a summary of the core tasks and features in the system. End-to-end Users can also get a gist of what is going on in the system, all of which are tasks and a quick summary of the API functionalities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc1og67p3szs0dh4sa85f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc1og67p3szs0dh4sa85f.png" alt="Quick Reference Format" width="800" height="620"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  API Best Practices
&lt;/h2&gt;

&lt;p&gt;API best practices can refer to any general advice your product team wants to communicate to developers about working with the API. They can necessarily be tips about fault tolerance - how it can be managed, and what type of specific requests should be used to produce the best results from the API.&lt;/p&gt;

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

&lt;p&gt;To write better, and an effective API documentation about a product and service, the conceptual topics outlined above must be required in your documentation. Yes! They are what provide a critical context for understanding and using the API beyond just the reference documentation - users can grasp the functionality and the purpose of the API, enabling them to effectively apply it in their products. If you have any questions regarding this article, feel free to ask in the comment section. Toodles and good luck!&lt;/p&gt;

</description>
      <category>api</category>
      <category>writing</category>
    </item>
    <item>
      <title>The Current State and Future of Progressive Web Apps (PWA)</title>
      <dc:creator>LordCodex</dc:creator>
      <pubDate>Tue, 31 Jan 2023 15:37:41 +0000</pubDate>
      <link>https://forem.com/devmirx/the-current-state-and-future-of-progressive-web-apps-pwa-ool</link>
      <guid>https://forem.com/devmirx/the-current-state-and-future-of-progressive-web-apps-pwa-ool</guid>
      <description>&lt;p&gt;&lt;strong&gt;What are Progressive Web Apps?&lt;/strong&gt;&lt;br&gt;
The term Progressive Web App was coined by Alex Russell and Frances Berriman. In Alex’s words&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Progressive Web Apps are just websites that took all the right vitamins&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the simplest terms, Progressive Web Apps (PWAs) are web applications that use modern web technologies to provide a user experience similar to native mobile apps.&lt;/p&gt;

&lt;p&gt;They are designed to work offline, be fast, and provide native-like navigation, among other features. &lt;/p&gt;

&lt;p&gt;PWAs can be installed on a user's device and can run independently of a web browser, making them accessible like a native app.&lt;/p&gt;

&lt;p&gt;They offer an alternative to native app development and are compatible with multiple platforms including desktop and mobile.&lt;/p&gt;

&lt;p&gt;PWAs are seen as a promising solution to bridge the gap between web and native apps, offering a more cost-effective and efficient option for businesses and developers.&lt;/p&gt;

&lt;p&gt;They combine the look and feel of an app with the ease of programming of a website. These cutting-edge apps make it easy for your users to access your content, and happy users, engaged users, and engaged users increase your revenue.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why do we need a Progressive Web App?
&lt;/h2&gt;

&lt;p&gt;Before we understand why we need a progressive web app as an alternative to native apps, let’s talk about some of the challenges we are facing today with native and web apps.&lt;/p&gt;
&lt;h2&gt;
  
  
  Internet speed
&lt;/h2&gt;

&lt;p&gt;you may not realize this depending on where you live, but 60% of the world’s population is still using 2G internet. Even in the US, some people have to use dialup to access the internet.&lt;/p&gt;
&lt;h2&gt;
  
  
  Slow website load
&lt;/h2&gt;

&lt;p&gt;Do you know how long a user waits to click the “Close X” button if a website is too slow? Three seconds! 53% of users abandon a website if it is too slow.&lt;/p&gt;
&lt;h2&gt;
  
  
  High friction
&lt;/h2&gt;

&lt;p&gt;People don’t want to install native apps. An average user installs 0 applications in a month.&lt;/p&gt;
&lt;h2&gt;
  
  
  User engagement
&lt;/h2&gt;

&lt;p&gt;Users spend most of their time in native apps, but mobile web reach is almost three times that of native apps. Hence, most of the users are not actively engaged. However, users are spending 80% of their time on only their top three native apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Well, you may ask why they are an alternative to native apps&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VZiR34bB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/v2/resize:fit:750/format:webp/1%2Ao2eA_ZR6hnUVTH2EvIAYqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VZiR34bB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/v2/resize:fit:750/format:webp/1%2Ao2eA_ZR6hnUVTH2EvIAYqg.png" alt="Image description" width="750" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast and secure&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7mvblqcq96yvhsh0dfg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7mvblqcq96yvhsh0dfg.jpg" alt="Image description" width="692" height="254"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrated user experience&lt;/strong&gt;: PWAs feel and behave like native apps. They sit on a user’s home screen, send push notifications like native apps, and have access to a device’s functionalities like native apps. The experience feels seamless and integrated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reliable experience&lt;/strong&gt;&lt;br&gt;
With the help of service workers, we can reliably paint a picture on a user’s screen even when network has failed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Engaging&lt;/strong&gt;: Because we can send notifications to a user, we can really drive the engagement up by keeping the user notified and engaged with the app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Future Proof&lt;/strong&gt;&lt;br&gt;
They adapt easily as technology improves&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cnRWChPQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.designnews.com/sites/designnews.com/files/styles/article_featured_retina/public/AdobeStock_296619471_770-400.jpeg%3Fitok%3DEaatvAvz" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cnRWChPQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://www.designnews.com/sites/designnews.com/files/styles/article_featured_retina/public/AdobeStock_296619471_770-400.jpeg%3Fitok%3DEaatvAvz" alt="Image description" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Access Anywhere&lt;/strong&gt;&lt;br&gt;
They can be operated on any operating system&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--im0fErua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://image.freepik.com/free-photo/pretty-woman-putting-notebook-her-locker_109710-5121.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--im0fErua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://image.freepik.com/free-photo/pretty-woman-putting-notebook-her-locker_109710-5121.jpg" alt="Image description" width="626" height="418"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to build a Progressive Web App&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Google has published a checklist of items for Progressive Web apps. I will go over four minimum requirements for an application to be a PWA:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Web App Manifest&lt;/strong&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="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"short_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;"PWA 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;"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;"Longer Name of PWA"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start_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;"/index.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"background_color"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"#FFFFFF"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"theme_color"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"#FFFFFF"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"display"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"standalone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"icons"&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="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-48x48.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"48x48"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-72x72.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"72x72"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-96x96.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"96x96"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-144x144.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"144x144"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-192x192.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"192x192"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-256x256.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"256x256"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-384x384.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"384x384"&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;"image/png"&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;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/icons/icon-512x512.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"512x512"&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;"image/png"&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;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 is just a json file that gives meta information about the web app. It has information like the icon of the app (which a user sees after installing it in their app drawer), background color of the app, name of the app, short name, and so on. We can write this manifest file ourselves or we can use tools to generate one for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Service Workers
&lt;/h2&gt;

&lt;p&gt;Service Workers are event-driven workers that run in the background of an application and act as a proxy between the network and application. They are able to intercept network requests and cache information for us in the background. This can be used to load data for offline use. They are a javascript script that listens to events like fetch and install, and they perform tasks.&lt;/p&gt;

&lt;p&gt;Here is a sample serviceworker.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Javascript 
// Register the service worker
if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function(err) {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });
  });
}

// The service worker JavaScript file
var cacheName = 'my-site-cache-v1';
var filesToCache = [
  '/',
  '/index.html',
  '/css/style.css',
  '/js/app.js'
];

self.addEventListener('install', function(e) {
  console.log('[ServiceWorker] Install');
  e.waitUntil(
    caches.open(cacheName).then(function(cache) {
      console.log('[ServiceWorker] Caching app shell');
      return cache.addAll(filesToCache);
    })
  );
});

self.addEventListener('activate', function(e) {
  console.log('[ServiceWorker] Activate');
  e.waitUntil(
    caches.keys().then(function(keyList) {
      return Promise.all(keyList.map(function(key) {
        if (key !== cacheName) {
          console.log('[ServiceWorker] Removing old cache', key);
          return caches.delete(key);
        }
      }));
    })
  );
});

self.addEventListener('fetch', function(e) {
  console.log('[ServiceWorker] Fetch', e.request.url);
  e.respondWith(
    caches.match(e.request).then(function(response) {
      return response || fetch(e.request);
    })
  );
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Icon
&lt;/h2&gt;

&lt;p&gt;This is used to provide an app icon when a user installs the PWA in their application drawer. A jpeg image will just be fine. The manifest tool I highlighted above helps in generating icons for multiple formats, and I found it very useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Served over HTTPS
&lt;/h2&gt;

&lt;p&gt;In order to be a PWA, the web application must be served over a secure network. With services like Cloudfare and LetsEncrypt, it is really easy to get an SSL certificate. Being a secure site is not only a best practice, but it also establishes your web application as a trusted site for users demonstrating trust and reliability and avoiding middleman attacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now Let's talk about the current state and future of Progressive web apps (PWAs)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--016u2Ma---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AaZlPUej18zqyRTO1egF2sg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--016u2Ma---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1400/1%2AaZlPUej18zqyRTO1egF2sg.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current state of PWAs is one of growth and widespread adoption. Major companies such as Twitter, Uber, and Alibaba have already implemented PWAs and are seeing positive results. PWAs provide a high-quality user experience, offline functionality, and increased engagement, which has led to increased conversions and engagement for many companies.&lt;/p&gt;

&lt;p&gt;The future of PWAs is promising as well. As web technologies continue to improve, PWAs will become increasingly capable and feature-rich. For example, advancements in areas such as web assembly, web VR/AR, and web GPU will further enhance the capabilities of PWAs. In addition, browser vendors and standards organizations are investing heavily in PWAs, which will result in improved support and easier development.&lt;/p&gt;

&lt;p&gt;In Conclusion, the future of PWAs looks bright and they will likely play an increasingly important role in the web development landscape in the coming years&lt;/p&gt;

&lt;p&gt;Thanks for reading. Let’s connect!&lt;/p&gt;

&lt;p&gt;➡️ I help you grow into Web Development and share my journey as a Software Developer. Join me on Twitter for more. 🚀🎒&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
