<?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: ziad</title>
    <description>The latest articles on Forem by ziad (@mage1711).</description>
    <link>https://forem.com/mage1711</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%2F464022%2F128c9fda-12e3-4eed-a85b-ad0ddc9eb42e.jpg</url>
      <title>Forem: ziad</title>
      <link>https://forem.com/mage1711</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mage1711"/>
    <language>en</language>
    <item>
      <title>Increasing Next.js app performance with GetServerSideProps and Next/Image</title>
      <dc:creator>ziad</dc:creator>
      <pubDate>Mon, 26 Jul 2021 13:46:17 +0000</pubDate>
      <link>https://forem.com/mage1711/increasing-next-js-performance-with-getserversideprops-and-next-image-3de0</link>
      <guid>https://forem.com/mage1711/increasing-next-js-performance-with-getserversideprops-and-next-image-3de0</guid>
      <description>&lt;p&gt;I was developing an app using react and next.js and I had to decide on the method I will be fetching my data in so I had two options to render my data&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using Axios and fetching the data on the client&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the prebuilt function GetServerSideProps and rendering on the server&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, I decided on doing both and testing the performance using &lt;a href="https://developers.google.com/speed/pagespeed/insights/" rel="noopener noreferrer"&gt;pageSpeed insights&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;First, The data I will be fetching is detailed in the interface below written in Typescript&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export interface Post {

identifier: string

title: string

body?: string

slug: string

subName: string

username: string

createdAt: string

updatedAt: string

sub?: Sub

mediaLink?: string

bodyPreview?: string

url: string

voteScore?: number

commentCount?: number

userVote?: number

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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Fetching data on the client
&lt;/h2&gt;

&lt;p&gt;First, we will fetch data dynamically using Axios as demonstrated in the code snippet below&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [posts, setPosts] = useState&amp;lt;Post[]&amp;gt;([])

useEffect(() =&amp;gt; {Axios.get('/posts').then((res)=&amp;gt;setPosts(res.data)).catch((err) =&amp;gt; console.log(err))}, [])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then I will render elements using the post component&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{posts?.map((post)=&amp;gt;(&amp;lt;PostPreview post={post} key={post.identifier}/&amp;gt;))}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The client-side post component will be using the default HTML &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; tag&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src={mediaLink}/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The post component&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Fetching data on the server
&lt;/h2&gt;

&lt;p&gt;First, I will wrap the Axios function used on the client-side with Next.js built-in function &lt;strong&gt;&lt;em&gt;GetServerSideProps&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (context) =&amp;gt; {try {

const res = await Axios.get('/post')
return { props: { posts: res.data } }
} catch (err) {

return { props: { error: 'Something went wrong' }}
}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;and in the post component, I will be using Next/Image component instead of &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Image src={mediaLink} width={16} height={16} layout="responsive"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;but what do they do exactly&lt;/p&gt;

&lt;p&gt;GetServerSideProps Fetches data on each request and renders it on the server then sends it to the client&lt;/p&gt;

&lt;p&gt;Why Image instead of &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;because Images using Next/Image are always rendered in such a way as to avoid &lt;a href="https://web.dev/cls/" rel="noopener noreferrer"&gt;Cumulative Layout Shift&lt;/a&gt;, a &lt;a href="https://web.dev/vitals/" rel="noopener noreferrer"&gt;Core Web Vital&lt;/a&gt; that Google uses in web search ranking, and Automatic Image Optimization according to Next.js&lt;/p&gt;

&lt;p&gt;So no that I have two pages one that loads data on the server and one on the client I used P&lt;a href="https://developers.google.com/speed/pagespeed/insights/" rel="noopener noreferrer"&gt;ageSpeed insights&lt;/a&gt; to test both routes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Results
&lt;/h2&gt;

&lt;p&gt;I analyzed both &lt;a href="https://notus-client.vercel.app/" rel="noopener noreferrer"&gt;https://notus-client.vercel.app/&lt;/a&gt; and &lt;a href="https://notus-client.vercel.app/serverside" rel="noopener noreferrer"&gt;https://notus-client.vercel.app/serverside&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and saw an increase of about 9–14 points on mobile which is the only platform we will focus on because it is the one that benefits most from server-side rendering&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%2Fjyop2x2xzhu1exv6n9j1.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%2Fjyop2x2xzhu1exv6n9j1.png" alt="Client-side results" width="707" height="502"&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%2Fwxsn4azza3nci8kgavdi.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%2Fwxsn4azza3nci8kgavdi.png" alt="Client-side results" width="800" height="164"&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%2Ffcnqqs6nih0r6x2n9wqg.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%2Ffcnqqs6nih0r6x2n9wqg.png" alt="Server-side results" width="800" height="430"&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%2Fnmseddz5y3e77n81vqcb.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%2Fnmseddz5y3e77n81vqcb.png" alt="Server-side results" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the results show the server-side approach increased the score by about 15% to 20%. which proves that this approach will be better for the app moving forward.&lt;/p&gt;

&lt;p&gt;you can run the test if you want on&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://notus-client.vercel.app/" rel="noopener noreferrer"&gt;https://notus-client.vercel.app/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://notus-client.vercel.app/serverside" rel="noopener noreferrer"&gt;https://notus-client.vercel.app/serverside&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;source code on GitHub: &lt;a href="https://github.com/mage1711/Notus-client" rel="noopener noreferrer"&gt;https://github.com/mage1711/Notus-client&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
