<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adel</title>
    <description>The latest articles on DEV Community by Adel (@adelhamad).</description>
    <link>https://dev.to/adelhamad</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%2F729687%2Fea306993-c675-44a1-8d38-bb5198a43a5f.png</url>
      <title>DEV Community: Adel</title>
      <link>https://dev.to/adelhamad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adelhamad"/>
    <language>en</language>
    <item>
      <title>CSRF Protection in Next.js</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Mon, 07 Mar 2022 22:53:29 +0000</pubDate>
      <link>https://dev.to/adelhamad/csrf-protection-in-nextjs-1g1m</link>
      <guid>https://dev.to/adelhamad/csrf-protection-in-nextjs-1g1m</guid>
      <description>&lt;p&gt;Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to submit a request to a Web application against which they are currently authenticated.&lt;/p&gt;

&lt;p&gt;It ensures the authenticity of your requests.&lt;/p&gt;

&lt;p&gt;We will use a popular npm package to handle CSRF called &lt;a href="https://github.com/expressjs/csurf" rel="noopener noreferrer"&gt;csurf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Because csurf is express middleware, and there is no easy way to include express middlewares in next.js applications we have two options.&lt;/p&gt;

&lt;p&gt;1- Create custom express server and use the middleware, check &lt;a href="https://github.com/vercel/next.js/tree/canary/examples/custom-server-express" rel="noopener noreferrer"&gt;this link&lt;/a&gt;&lt;br&gt;
2- Connect express middleware, we will follow this method, more details in &lt;a href="https://nextjs.org/docs/api-routes/api-middlewares#connectexpress-middleware-support" rel="noopener noreferrer"&gt;next.js docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we will create new file &lt;code&gt;/src/csrf.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;csurf&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;csurf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Helper method to wait for a middleware to execute before continuing&lt;/span&gt;
&lt;span class="c1"&gt;// And to throw an error when an error happens in a middleware&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;csrf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;csurf&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&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="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;csrf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have two steps to implement this,&lt;/p&gt;

&lt;p&gt;1- Make sure the API is protected by CSRF token.&lt;/p&gt;

&lt;p&gt;Lets take the default API route that comes with initial next.js project "hello.js", to include the middleware we need to do the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;csrf&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../../src/csrf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;csrf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way we are protecting this route with CSRF token&lt;/p&gt;

&lt;p&gt;2- Expose this token to the react page so it can be sent with the requests.&lt;/p&gt;

&lt;p&gt;To get the token&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;csrf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;csrfToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;csrfToken&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now on the next API call to hello.js we need to include the token in the header, here I used axios but you can use fetch as well&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000/api/hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;CSRF-Token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;csrfToken&lt;/span&gt;&lt;span class="p"&gt;}})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it, Now you are protected against CSRF attacks&lt;/p&gt;

&lt;p&gt;Note that you can add more options to your cookie like make it HttpOnly and change the key name, check the library docs for more details.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nextjs</category>
      <category>webdev</category>
      <category>security</category>
    </item>
    <item>
      <title>Browser Storage APIs</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Sun, 30 Jan 2022 22:04:46 +0000</pubDate>
      <link>https://dev.to/adelhamad/browser-storage-apis-3gcl</link>
      <guid>https://dev.to/adelhamad/browser-storage-apis-3gcl</guid>
      <description>&lt;h2&gt;
  
  
  1- Cookies
&lt;/h2&gt;

&lt;p&gt;Cookies are small pieces of text data stored in the browser mainly used for authentication, tracking and personalization.&lt;/p&gt;

&lt;p&gt;Cookies can be read and set by browser and server with certain rules, hold 4069 bytes of data.&lt;/p&gt;

&lt;p&gt;For more details I have an article  “&lt;a href="https://dev.to/adelhamad/cookies-simple-and-comprehensive-guide-3ni1"&gt;Cookies: simple and comprehensive guide&lt;/a&gt;”&lt;/p&gt;

&lt;p&gt;To set a cookie using javascript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2- Web Storage
&lt;/h2&gt;

&lt;p&gt;Similar to cookies, pieces of text data stored in the browser. It can hold up to 5MB and can be read and set only by the browser.&lt;/p&gt;

&lt;p&gt;It offers simpler APIs than cookies and all Web Storage calls are synchronous.&lt;/p&gt;

&lt;p&gt;The two mechanisms within Web Storage are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Local Storage&lt;/p&gt;

&lt;p&gt;No expiration date.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;SessionStorage&lt;/p&gt;

&lt;p&gt;data is stored until the tab is closed.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myCat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myCat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myCat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3- IndexedDB
&lt;/h2&gt;

&lt;p&gt;A Transactional NoSQL database system built in the browser. It can be used synchronously and asynchronously.&lt;/p&gt;

&lt;p&gt;If you have big amount of data or files that needs to be stored in the browser, IndexedDB is a good option.&lt;/p&gt;

&lt;p&gt;While it includes more features than traditional Web Storage API, using IndexedDB is also more complex, for that there are open source libraries to simplify it like &lt;a href="https://github.com/localForage/localForage" rel="noopener noreferrer"&gt;localForage&lt;/a&gt; which wraps IndexedDB API and makes it as simple as using Web Storage API&lt;/p&gt;

&lt;h2&gt;
  
  
  4- Cache
&lt;/h2&gt;

&lt;p&gt;Used mainly to optimize the performance of the website by storing its files to not be downloaded again when you open the same website again.&lt;/p&gt;

&lt;p&gt;You can cache any response whether it’s HTML, Javascript, CSS, image, font or even API response.&lt;/p&gt;

&lt;p&gt;The storage allowed for cache storage is different per browser, for example Chrome allow to take 80% of available disk space.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is Beacon request</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Thu, 27 Jan 2022 22:27:03 +0000</pubDate>
      <link>https://dev.to/adelhamad/what-is-beacon-request-1237</link>
      <guid>https://dev.to/adelhamad/what-is-beacon-request-1237</guid>
      <description>&lt;p&gt;navigator.sendBeacon() method asynchronously sends an HTTP POST request in reliable way, it's mostly used to send analytics.&lt;/p&gt;

&lt;p&gt;it returns true if the user agent successfully queued the data for transfer. Otherwise, it returns false.&lt;/p&gt;

&lt;p&gt;Unlike traditional HTTP requests, sendBeacon will always reach to the server even if you unload or exit the page in the middle of the request.&lt;/p&gt;

&lt;p&gt;Example&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;visibilitychange&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;visibilityState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hidden&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="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendBeacon&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/log&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;analyticsData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>api</category>
      <category>programming</category>
    </item>
    <item>
      <title>Cookies: simple and comprehensive guide</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Sun, 23 Jan 2022 00:17:41 +0000</pubDate>
      <link>https://dev.to/adelhamad/cookies-simple-and-comprehensive-guide-3ni1</link>
      <guid>https://dev.to/adelhamad/cookies-simple-and-comprehensive-guide-3ni1</guid>
      <description>&lt;p&gt;Cookies are small pieces of text data stored in the browser mainly used for authentication, tracking and personalization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limits
&lt;/h3&gt;

&lt;p&gt;Typically, the following are allowed:&lt;br&gt;
300 cookies in total&lt;br&gt;
4096 bytes per cookie&lt;br&gt;
20 cookies per domain&lt;br&gt;
81920 bytes per domain&lt;br&gt;
** Given 20 cookies of max size 4096 = 81920 bytes.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create a cookie?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Javascript:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;HTTP response:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Set-Cookie: &amp;lt;cookie-name&amp;gt;=&amp;lt;cookie-value&amp;gt;; Domain=&amp;lt;domain-value&amp;gt;; Secure; HttpOnly
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to read a cookie?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Javascript:
Note that javascript will not have access to httponly cookies.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// returns cookie1=value; cookie2=value;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;HTTP request: your browser will send the cookies to the associated site in eligible requests in the http headers, so it's easy read them from service side.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to delete a cookie?
&lt;/h2&gt;

&lt;p&gt;just set the same cookie with passing "expires" as a past date.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attributes:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Secure&lt;/strong&gt;:
The cookie will be sent only over https.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HttpOnly&lt;/strong&gt;:
Can't be accessed from client side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain&lt;/strong&gt;:
the cookie will be sent if the domain matches or if it is a subdomain, then the path attribute will be checked next.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path&lt;/strong&gt;:
if the path attribute was set to the web server root /, then the application cookies will be sent to every application within the same domain
If set to specific path like "/blog", then it will be sent only to the requests that matches the path like "/blog/hello".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expires&lt;/strong&gt;:
To specify when the cookie will die, this time is relative to the client not the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Max-Age&lt;/strong&gt;:
After how many seconds the cookie will expire, not supported by all browsers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SameSite&lt;/strong&gt;:
To control sending cookies along with cross-site requests and take three values:

&lt;ul&gt;
&lt;li&gt;Strict: Sent only to first-party.&lt;/li&gt;
&lt;li&gt;Lax: Default in most browsers, same as Strict except that cookies are sent when the user navigates to the cookie's origin site.&lt;/li&gt;
&lt;li&gt;None: Sent cross-site.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prefixes:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;__Host-&lt;/strong&gt; : the cookie will be rejected if not Secure with no Domain and Path = "/".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;__Secure-&lt;/strong&gt; : will be rejected if not Secure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Types:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;First-party Cookies:&lt;/strong&gt;&lt;br&gt;
Set by the website visited by the user through HTTP headers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Third-party Cookies:&lt;/strong&gt;&lt;br&gt;
Set by other domains, examples: ads, iframes, fonts, images from other domains.&lt;br&gt;
Starting in chrome v80 cookies are restricted to first party, default value for samesite attribute if not set is Lax, &lt;br&gt;
If you need 3rd party you need to explicitly mark it as samesite=none and sercure=true.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Persistent Cookies:&lt;/strong&gt;&lt;br&gt;
Deleted at a date specified by the Expires attribute, or after a period of time specified by the Max-Age attribute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non-persistent Cookies:&lt;/strong&gt;&lt;br&gt;
If expires attribute is empty, it will be deleted when you close your browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Zombie Cookies:&lt;/strong&gt;&lt;br&gt;
Extremely persistent cookies in a browser. Its goal is to identify a client even after they've removed standard cookies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flash Cookies:&lt;/strong&gt;&lt;br&gt;
No longer exists, local shared object used by Adobe Flash.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Legal:
&lt;/h3&gt;

&lt;p&gt;All regulations require:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Notify user that you are using cookies.&lt;/li&gt;
&lt;li&gt;Allowing users to opt out of receiving some or all cookies.&lt;/li&gt;
&lt;li&gt;Allowing users to use the bulk of your service without receiving cookies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Further reads:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/samyk/evercookie" rel="noopener noreferrer"&gt;https://github.com/samyk/evercookie&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>cookies</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Simple way to do device fingerprinting</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Sat, 08 Jan 2022 20:21:25 +0000</pubDate>
      <link>https://dev.to/adelhamad/simple-way-for-device-fingerprinting-4hm2</link>
      <guid>https://dev.to/adelhamad/simple-way-for-device-fingerprinting-4hm2</guid>
      <description>&lt;p&gt;Device fingerprint is a unique identifier for a specific device the most common uses for it are fraud detection and user validation.&lt;/p&gt;

&lt;p&gt;There is no clear way to get this unique identifier instead, we will try to generate a string out of some information collected from the user.&lt;/p&gt;

&lt;p&gt;Fingerprinting can be on the client-side (browser) or on the backend, we will do the backend way because it doesn't limit us by the user device or the features enabled on it.&lt;/p&gt;

&lt;p&gt;You can use a lot of parameters to generate this identifier, in my case I'm trying to detect if the user is trying to log in from a different device or browser to the same account, for this simple case I will use a combination of IP address, user agent and params(credentials + CSRF token)&lt;/p&gt;

&lt;p&gt;You can add more parameters to this combination like cookies or other headers.&lt;/p&gt;

&lt;p&gt;I used here md5 as a hashing algorithm to generate the fingerprint but you can use a different one.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;md5&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;crypto-js/md5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestIp&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;request-ip&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;requestIp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getClientIp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useragent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user-agent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fingerprint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;md5&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ip&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;useragent&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>security</category>
    </item>
    <item>
      <title>5 ways to use microforntends</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Sat, 08 Jan 2022 13:34:10 +0000</pubDate>
      <link>https://dev.to/adelhamad/5-ways-to-use-microforntends-2b9l</link>
      <guid>https://dev.to/adelhamad/5-ways-to-use-microforntends-2b9l</guid>
      <description>&lt;h2&gt;
  
  
  What is microfrontend:
&lt;/h2&gt;

&lt;p&gt;In short, it's a way to a way to apply the microservice principles to frontend applications, this will give multiple benefits like Autonomous teams, Easier maintenance, independent deployments, and flexible tech choices.&lt;/p&gt;

&lt;p&gt;The best use for microfrontend architecture is when you have a large-scale application and a large team working on it and you want to divide to conquer, otherwise adopting microfrontend architecture will be overkill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. iFrames&lt;/strong&gt;&lt;br&gt;
The old good iframe tag, you can use anywhere on your page, you need to make sure this header is enabled on the page you want to include as an iframe&lt;br&gt;
&lt;em&gt;X-Frame-Options: ALLOW-FROM &lt;a href="https://example.com" rel="noopener noreferrer"&gt;https://example.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can pass data from the parent page to the iframe in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Url params&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage" rel="noopener noreferrer"&gt;Postmessage&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Javascript bundles&lt;/strong&gt;&lt;br&gt;
Include any javascript file written in any framework to your page at runtime, and make sure that this file will render itself in a specific div you will create in a certain place in your original app&lt;br&gt;
You can include CSS files in the same way, or include &lt;a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts" rel="noopener noreferrer"&gt;manifest.json&lt;/a&gt; file which reference all your JS and CSS files (if you have multiple javascript and CSS files) and it will load all needed files automatically &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Zones&lt;/strong&gt;&lt;br&gt;
Feature in Next.js lets you reference another project as a page in the main app using redirects, if you are not using Next.js you can achieve that using Nginx as well.&lt;br&gt;
Since the main app and all the zones are on the same domain, they will share the cookies so you can do authentication on the main app and the other apps can read the auth cookie&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Module federations&lt;/strong&gt;&lt;br&gt;
A new feature in webpack 5, allows you to export one of your components and use it in another app and import it like any other component in the app, any update on the federated component will reflect all consumers at runtime&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Other dedicated frameworks&lt;/strong&gt;&lt;br&gt;
There are number of frameworks designed to build microfrontends like&lt;br&gt;
&lt;a href="https://bit.dev/" rel="noopener noreferrer"&gt;https://bit.dev/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://single-spa.js.org/" rel="noopener noreferrer"&gt;https://single-spa.js.org/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Demo
&lt;/h3&gt;

&lt;p&gt;I created a small demo used Zones and Module federations&lt;br&gt;
I this demo there is 2 Zones, Home and Products, and I exported the top menu bar as federated module from Home app and used it in both Home and Products&lt;br&gt;
Also using next-auth library I created a simple auth and since and it's shared between two zones&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Demos&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://mfe-shop-home.vercel.app/" rel="noopener noreferrer"&gt;https://mfe-shop-home.vercel.app/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://mfe-shop-products.vercel.app/products" rel="noopener noreferrer"&gt;https://mfe-shop-products.vercel.app/products&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://github.com/adelhamad/micro-frontend-shop" rel="noopener noreferrer"&gt;https://github.com/adelhamad/micro-frontend-shop&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Further reads&lt;br&gt;
&lt;a href="https://www.facebook.com/notes/10158791368532200/" rel="noopener noreferrer"&gt;https://www.facebook.com/notes/10158791368532200/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/luistak/cross-micro-frontends-communication-30m3"&gt;https://dev.to/luistak/cross-micro-frontends-communication-30m3&lt;/a&gt;&lt;br&gt;
&lt;a href="https://micro-frontends.org" rel="noopener noreferrer"&gt;https://micro-frontends.org&lt;/a&gt;&lt;br&gt;
&lt;a href="https://martinfowler.com/articles/micro-frontends.html" rel="noopener noreferrer"&gt;https://martinfowler.com/articles/micro-frontends.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://indepth.dev/posts/1477/taking-micro-frontends-to-the-next-level#third-party-micro-frontends" rel="noopener noreferrer"&gt;https://indepth.dev/posts/1477/taking-micro-frontends-to-the-next-level#third-party-micro-frontends&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>microfrontends</category>
      <category>microservices</category>
      <category>webdev</category>
    </item>
    <item>
      <title>BFF - Backend for Frontend Design Pattern with Next.js</title>
      <dc:creator>Adel</dc:creator>
      <pubDate>Tue, 26 Oct 2021 16:24:50 +0000</pubDate>
      <link>https://dev.to/adelhamad/bff-backend-for-frontend-design-pattern-with-nextjs-3od0</link>
      <guid>https://dev.to/adelhamad/bff-backend-for-frontend-design-pattern-with-nextjs-3od0</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;These days microservice architecture becoming more and more popular, and if you worked on a project that adopt this architecture then as a frontend developer you probably faced one of the following scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You support multiple platforms ( web, mobile app, smartwatch…. ) and each one has a specific need of data.&lt;/li&gt;
&lt;li&gt;Calling APIs from multiple services to build one user interface.&lt;/li&gt;
&lt;li&gt;Manipulate, mix and match the responses of multiple API calls to reach the desired shape of data.&lt;/li&gt;
&lt;li&gt;Receive unnecessary data from API that you don't need at all.&lt;/li&gt;
&lt;li&gt;Receiving the same piece of info from different services with the different data types, for example one service could send the date as epoch and other one might send it as a Linux timestamp.&lt;/li&gt;
&lt;li&gt;Finding yourself writing complex computations or maybe business logic in the frontend.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As your codebase grows and becomes more complex, it becomes hard to keep organized, and by the time you might find your codebase out of control and of course complexity where the bugs hide.&lt;/p&gt;

&lt;p&gt;Typically the frontend code should be very simple, straight forward and easy to read, and we should avoid doing complex computations in the UI layer especially while rendering, otherwise you will be using a lot more of browser resources which will lead to bad performance .&lt;/p&gt;

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

&lt;h2&gt;
  
  
  General Purpose API
&lt;/h2&gt;

&lt;p&gt;Generic APIs contain unnecessary data that is sometimes of no use for consumer applications This might be critical sometimes especially when sometimes we need to deliver as small response as possible to some frontends like smartwatches.&lt;/p&gt;

&lt;p&gt;Each one of these frontends may have specific needs about the data delivered to it from the backend. And since all of them calling the same API, the backend developer will try to spit every piece of data available to satisfy all frontends needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is BFF Design Pattern
&lt;/h2&gt;

&lt;p&gt;This pattern was first described by &lt;a href="https://samnewman.io/patterns/architectural/bff" rel="noopener noreferrer"&gt;Sam Newman&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By Implementing BFF we try to keep the frontend decoupled from the backend. The BFF should be tightly coupled with frontend, Because in the first place it existed to serve the frontend needs and Ideally it should be built by the frontend developer.&lt;/p&gt;

&lt;p&gt;In most cases we should have one BFF for each frontend, then we can customize the BFF and fine-tune it according to that frontend needs.&lt;/p&gt;

&lt;p&gt;In some cases we might share one BFF with multiple frontends if the requirements are very similar, for example one BFF for iOS and Android this way is adopted by SoundCloud for example, by doing this you will avoid a lot of duplicate code across BFFs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One BFF per frontend&lt;/em&gt; &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F508bs3aih21gniiw826v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F508bs3aih21gniiw826v.png" alt="One BFF per frontend" width="800" height="634"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sharing BFF for some frontends&lt;/em&gt; &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgxclvfkgkidhrx06cbq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgxclvfkgkidhrx06cbq.png" alt="Sharing BFF for some frontends" width="800" height="647"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not an API gateway:&lt;/strong&gt; you might think that the BFF is very similar to API gateway but it's not because the main reason for API gateway is to be a reverse proxy between the consumer and all other microservices not to customize the response according to this particular frontend needs. Also API gateway is the single entry point for anybody need to reach any backend service wheather the BFF is specific to one frontend.&lt;/p&gt;

&lt;p&gt;BFF will hide a lot of complexities from the frontend which will make the app more resilient to new change.&lt;br&gt;
Also  you have the freedom the use any protocol you are most comfortable with like GraphQL, even if the other services use REST or SOAP.&lt;/p&gt;

&lt;p&gt;Using BFF will also abstract the frontend related unit tests .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that the BFF pattern is not useful when you only support one frontend.&lt;/p&gt;
&lt;h2&gt;
  
  
  With Multiple Backend services
&lt;/h2&gt;

&lt;p&gt;Lets say you need to build a user profile page for a social platform, and this platform is built with microservices architecture, then it will look something like this.&lt;/p&gt;

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

&lt;p&gt;As you see here the web UI calls APIs from multiple services to build the profile page.&lt;br&gt;
First needs to get the data about the user, and do another two or more calls to get the rest of the results based on the fetched username or user id. Note that the response could contain a lot of data that is not needed to build this UI, the latter calls can be called on parallel to be executed in less time, then you need to merge the responses and gather only the data you need to build this user profile page. It looks painful right? Imagine you have similar scenarios with much more complex UIs and much more services to consume data from, this is not very practical.&lt;/p&gt;

&lt;p&gt;Instead it will be more efficient to call just one API and get only the data needed to build this page, and this is what needs to happen in the BFF layer.&lt;/p&gt;

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

&lt;p&gt;In this way we abstracted all this complexity from the frontend, and the frontend role here is just to present the returned data.&lt;/p&gt;

&lt;p&gt;I will do an example for the same problem later in this article.&lt;/p&gt;
&lt;h2&gt;
  
  
  API Versioning and A/B testing
&lt;/h2&gt;

&lt;p&gt;Sometimes you maybe supporting different versions of the API for the same service, it's much easier to abstract this from the frontend and do it inside the BFF. This way the frontend will not be aware of the version it will just render the UI no matter what.&lt;/p&gt;

&lt;p&gt;It can be useful as well when you want to run A/B testing campaign, for example you can return the version needed for specific users with the user object then let the BFF handle different API versions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Nice Additions, Taking it further
&lt;/h2&gt;

&lt;p&gt;Now after you added the BFF layer, there is a lot of cool things you can do specifically to that frontend.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Because you are sending only what the frontend needs, you are hiding a lot of unnecessary or sensitive data that the attacker might use against you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: You can connect to redis for example directly and cache the API responses, then serve the results from the cache if available instead of calling the microservice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: multiple services can handle errors in different ways, in the BFF you can define a unified way to give the frontend a consistent response in case of any error happens.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access Control&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logging&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Web Sockets&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;etc …&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although I think it's better to keep it as simple as you can and stick to the main reason of building this BFF which is solving the problems of that specific frontend not solving general problems.&lt;/p&gt;

&lt;p&gt;As the codebase grows, you might find yourself implementing general-purpose tiny services inside the BFF (sound cloud faced this issue) so try to keep the scope of the BFF as it's defined from the beginning.&lt;/p&gt;
&lt;h2&gt;
  
  
  With Next.js
&lt;/h2&gt;

&lt;p&gt;By using Next.js you will get a few benefits out of the box&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fewer deployments: you don’t need to deploy your BFF separately because it will be integrated with Next.js by default.&lt;/li&gt;
&lt;li&gt;Using the backend layer in Next.js, BFF will be tightly coupled to your frontend which is what we exactly need.&lt;/li&gt;
&lt;li&gt;Sharing code like type definitions and utility functions between BFF and the frontend will be very easy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the sake of demonstrating how BFF work we will use Next.js API to simulate the microservices behavior, so we will have one file for each of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Messaging service will include 

&lt;ul&gt;
&lt;li&gt;One endpoint to get all messages based on "read" filter, and it can take two values (true, false).&lt;/li&gt;
&lt;li&gt;One endpoint to get the latest message received (to get the last seen).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Notification service will include one endpoint to get all notifications based on "seen" filter and it can take two values (1,0).&lt;/li&gt;
&lt;li&gt;Friends service will include one endpoint to get all pending friend requests.&lt;/li&gt;
&lt;li&gt;BFF itself will consume APIs from all of those services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, we will see how the data will look like from each service.&lt;/p&gt;
&lt;h5&gt;
  
  
  Message object
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&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;"uid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"263f4178-39c6-4b41-ad5b-962a94682ceb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est. Phasellus sit amet erat. Nulla tempus."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"created_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1634320826"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"read"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&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;h5&gt;
  
  
  Notification object
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&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;"uid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ee7cd9df-2409-46af-9016-83a1b951f2fa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Vestibulum quam sapien, varius ut, blandit non, interdum in, ante."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"created_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1617738727000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"seen"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  Person object
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&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;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"first_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;"Marillin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"last_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;"Pollicott"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"birthdate"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"4/20/2021"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mpollicott0@wikispaces.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"gender"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Male"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"ip_address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"105.134.26.93"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2132 Leroy Park"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"created_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"9/13/2021"&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;h5&gt;
  
  
  Desired profile object
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"birthdate"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2020-11-17T00:00:00.000Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"242 Vermont Parkway"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"joined"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-08-27T00:00:00.000Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"last_seen"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-10-15T18:00:26.000Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"new_notifications"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;61&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"new_messages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;56&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"new_friend_requests"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;15&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;Notice the differences in data types for each service, like date, in message object it's a Linux timestamp in seconds and in notification service it's Linux timestamp in milliseconds while it's just simple date string in friends service and what we want actually is a simplified extended ISO format with the timezone set to zero UTC offset so it can be formatted in the frontend however we want. You can see also message service the Boolean represented as (true, false) and in notification service it's (1,0) you can spot other differences too if you look in details.&lt;/p&gt;

&lt;p&gt;Also, notice the person object we have first and last name as different attributes, but in the frontend we show the combination of both.&lt;/p&gt;

&lt;p&gt;So the main task of the BFF is to get data from different services, gather them and format them in the easiest form so the frontend will do the least effort to render these data. For that we defined a new interface (Profile).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Profile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
   &lt;span class="nx"&gt;birthdate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;
   &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
   &lt;span class="nx"&gt;joined&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;
   &lt;span class="nx"&gt;last_seen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;
   &lt;span class="nx"&gt;new_notifications&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
   &lt;span class="nx"&gt;new_messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
   &lt;span class="nx"&gt;new_friend_requests&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this interface we described the data we want and in which type to guarantee that the response returned to the frontend will be always correct.&lt;/p&gt;

&lt;p&gt;You can check the code on this &lt;a href="https://github.com/adelhamad/bff-demo" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;br&gt;
The demo on this &lt;a href="https://backend-for-frontend.vercel.app/" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One more cool thing with Next.js&lt;/strong&gt;&lt;br&gt;
If you are planning to integrate with some sort of caching mechanism like redis, next.js will make it much more easy and performant.&lt;/p&gt;

&lt;p&gt;With server-side rendering in next.js you can just get the data from redis and just send the page ready to the frontend without the need to call an API from the frontend, the data will just be there in the fastest way possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;BFF focuses on Creating a new backend per frontend that only serves the needs of that frontend.&lt;/li&gt;
&lt;li&gt;BFF will call APIs from multiple services and form the minimal response required.&lt;/li&gt;
&lt;li&gt;Frontend will get only what is needed to render the UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Further Reads
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://samnewman.io/patterns/architectural/bff" rel="noopener noreferrer"&gt;https://samnewman.io/patterns/architectural/bff&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developers.soundcloud.com/blog/service-architecture-1" rel="noopener noreferrer"&gt;https://developers.soundcloud.com/blog/service-architecture-1&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/backends-for-frontends" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/azure/architecture/patterns/backends-for-frontends&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>microservices</category>
      <category>designpattern</category>
    </item>
  </channel>
</rss>
