<?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: Miguel A. Gavilán Merino</title>
    <description>The latest articles on DEV Community by Miguel A. Gavilán Merino (@miangame).</description>
    <link>https://dev.to/miangame</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%2F199484%2F308ba86b-d807-4cdc-add4-fb136349b445.jpg</url>
      <title>DEV Community: Miguel A. Gavilán Merino</title>
      <link>https://dev.to/miangame</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/miangame"/>
    <language>en</language>
    <item>
      <title>Use SWR for a better data fetching</title>
      <dc:creator>Miguel A. Gavilán Merino</dc:creator>
      <pubDate>Sun, 21 Feb 2021 09:33:40 +0000</pubDate>
      <link>https://dev.to/miangame/use-swr-for-a-better-data-fetching-5c7l</link>
      <guid>https://dev.to/miangame/use-swr-for-a-better-data-fetching-5c7l</guid>
      <description>&lt;p&gt;Hi everyone!&lt;/p&gt;

&lt;p&gt;In this post I'm going to talk about how we can use &lt;a href="https://swr.vercel.app/" rel="noopener noreferrer"&gt;SWR&lt;/a&gt; powered by &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt; to do better and easier data fetching, as well as some of the possibilities it has.&lt;/p&gt;

&lt;p&gt;As it says in the documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The name “SWR” is derived from stale-while-revalidate, a HTTP cache invalidation strategy popularized by HTTP RFC 5861. SWR is a strategy to first return the data from cache (stale), then send the fetch request (revalidate), and finally come with the up-to-date data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore with SWR the components will have a data stream that is constantly and automatically updated, and the UI will always be fast and reactive.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use SWR?
&lt;/h2&gt;

&lt;p&gt;First of all we must install it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add swr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or with npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;swr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will be able to use it in the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSWR&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../hooks/useSWR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&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;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSWR&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getUsers&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;The &lt;code&gt;data&lt;/code&gt; variable is the users fetched.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;error&lt;/code&gt; variable tells us if there has been an error during the data fetch.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'users'&lt;/code&gt; is a key that is passed to SWR. I will explain later what it is used for.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UserService.getUsers&lt;/code&gt; is the fetcher.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example of a fetcher could be:&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;fetcher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;res&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Auto Revalidation
&lt;/h2&gt;

&lt;p&gt;The power of SWR is that the data can be auto validating in different ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  Revalidate on focus
&lt;/h3&gt;

&lt;p&gt;When you re-focus a page or switch between tabs, SWR automatically revalidates data.&lt;br&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%2Fxrkrs36hhysofmbottla.gif" 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%2Fxrkrs36hhysofmbottla.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Revalidate on interval
&lt;/h3&gt;

&lt;p&gt;We can add a time interval to SWR so that the revalidation of data occurs every so often.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSWR&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../hooks/useSWR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&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;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSWR&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;refreshInterval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Revalidate with key
&lt;/h3&gt;

&lt;p&gt;Earlier I mentioned that we have to pass a key to SWR. &lt;br&gt;
If this key changes, the data will be automatically revalidated. &lt;br&gt;
This can be combined with the use of states to have dynamic data fetching.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSWR&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../hooks/useSWR&lt;/span&gt;&lt;span class="dl"&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;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUserId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="kd"&gt;const&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;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useSWR&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&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="s2"&gt;`user-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;UserService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, these are just some of the things SWR provides that can make our data fetching faster and better.&lt;/p&gt;

&lt;p&gt;Thanks for reading me!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/3ohs7JG6cq7EWesFcQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3ohs7JG6cq7EWesFcQ/giphy.gif" alt="Thank you"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>swr</category>
      <category>fetch</category>
    </item>
    <item>
      <title>Static site generation (SSG) with Nextjs</title>
      <dc:creator>Miguel A. Gavilán Merino</dc:creator>
      <pubDate>Thu, 17 Sep 2020 18:09:14 +0000</pubDate>
      <link>https://dev.to/miangame/static-files-with-nextjs-2nh0</link>
      <guid>https://dev.to/miangame/static-files-with-nextjs-2nh0</guid>
      <description>&lt;p&gt;Hi! In this post I want to talk about static files in next.js, when and how to use them to improve our application performance.&lt;/p&gt;

&lt;p&gt;I have been working with Nextjs for a while.&lt;br&gt;
It is a very powerful framework, since it solves many problems that we find when code an application with reactjs, such as the use of webpack, routing, etc.&lt;br&gt;
But of course all this can be modified as we want.&lt;/p&gt;

&lt;p&gt;Also with nextjs we have two types of rendering: Server Side Rendering (SSR) and Static Generation (SSG).&lt;br&gt;
Next we are going to talk about this last type, which is the competition of the well-known Gatsby to generate static pages.&lt;/p&gt;
&lt;h1&gt;
  
  
  How can I create static pages?
&lt;/h1&gt;

&lt;p&gt;To make our pages static, we just have to add the &lt;code&gt;getStaticProps&lt;/code&gt; function to our page.&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;Contact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;title&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="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticProps&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="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;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Contact&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Contact&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As with &lt;code&gt;getInitialProps&lt;/code&gt; or &lt;code&gt;getServerSideProps&lt;/code&gt;, what we return are the parameters that later arrive at our application as &lt;code&gt;props&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Only with this, the contact page will be rendered statically &lt;strong&gt;AT BUILD TIME&lt;/strong&gt;.&lt;br&gt;
We can check it by running &lt;code&gt;yarn build&lt;/code&gt;, And this is the output we get:&lt;br&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%2Fi%2Frm0h3jdk5rc4qt0o5s4r.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%2Fi%2Frm0h3jdk5rc4qt0o5s4r.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
We can see in the legend that the contact page has been exported to a static file and also its size, and we can see it in the file explorer.&lt;br&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%2Fi%2Fkjczy0hogk13vssg352y.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%2Fi%2Fkjczy0hogk13vssg352y.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But this is not all&lt;/strong&gt;, for example it may be that at some point changes have been added to a page that has already been generated statically and we want it to be regenerated with this new content in production.&lt;br&gt;
For this, from version 9.5 of nextjs we have "Incremental Static Regeneration" that will allow us to configure every few seconds we want this page to be rendered again.&lt;br&gt;
This can be done very easily just by adding &lt;code&gt;revalidate&lt;/code&gt; to what &lt;code&gt;getStaticProps&lt;/code&gt; returns.&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;getStaticProps&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="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;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Contact&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="c1"&gt;// Next.js will attempt to re-generate the page:&lt;/span&gt;
      &lt;span class="c1"&gt;// - When a request comes in&lt;/span&gt;
      &lt;span class="c1"&gt;// - At most once every second&lt;/span&gt;
      &lt;span class="na"&gt;revalidate&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="c1"&gt;// In seconds&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;All this is great for those pages that are not very common that can change as an information page, a contact page, etc. but ...&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens to pages that are dynamic?
&lt;/h2&gt;

&lt;p&gt;For this we have another function with which we can indicate which routes we want it to export statically.&lt;br&gt;
We need a dynamic route, for example if we want to export our blog posts, we should have something like this in our files.&lt;br&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%2Fi%2F6e1lnfg35eq18v2ml4au.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%2Fi%2F6e1lnfg35eq18v2ml4au.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
In this new file, we also need the &lt;code&gt;getStaticProps&lt;/code&gt;, but we will also add a new &lt;code&gt;getStaticPaths&lt;/code&gt; 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PostPage&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRouter&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="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;POST&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticPaths&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="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
      &lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}],&lt;/span&gt;
    &lt;span class="na"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getStaticProps&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="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="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;PostPage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What are we returning?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;paths: Routes that we can export. For example we can generate them with a &lt;code&gt;map&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;fallback: With this prop we can tell nextjs that if there is no certain route within &lt;code&gt;paths&lt;/code&gt;, force its rendering.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the previous example, if we execute &lt;code&gt;yarn install&lt;/code&gt;, we get the following console output:&lt;br&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%2Fi%2F0u6wph2b5qz17w8xzclz.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%2Fi%2F0u6wph2b5qz17w8xzclz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
In the previous image, we can see how the routes that we have established have been exported, and we can also find them in the file system.&lt;br&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%2Fi%2Fbgfq89lbgkk0h9whynnf.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%2Fi%2Fbgfq89lbgkk0h9whynnf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with this I finish a small introduction to the generation of static pages with one of my favorite frameworks at the moment ❤️.&lt;/p&gt;

&lt;p&gt;Thanks for reading me!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/osjgQPWRx3cac/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/osjgQPWRx3cac/giphy.gif" alt="gif"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>frontend</category>
      <category>static</category>
    </item>
    <item>
      <title>Use MirageJS to fake API calls with NextJS</title>
      <dc:creator>Miguel A. Gavilán Merino</dc:creator>
      <pubDate>Fri, 10 Jul 2020 11:29:59 +0000</pubDate>
      <link>https://dev.to/miangame/use-miragejs-to-fake-api-calls-with-nextjs-7dl</link>
      <guid>https://dev.to/miangame/use-miragejs-to-fake-api-calls-with-nextjs-7dl</guid>
      <description>&lt;p&gt;Welcome!&lt;/p&gt;

&lt;p&gt;In this post, I am going to write an introduction to &lt;a href="https://miragejs.com/"&gt;MirageJS&lt;/a&gt;, a tool that I discovered recently and I think that is very useful when you develop a Frontend application that will use an API to get its data without having the backend fully developed.&lt;/p&gt;

&lt;p&gt;Assuming we already have a working frontend project in which we want to use Mirage, we have to follow the next steps:&lt;/p&gt;

&lt;h2&gt;
  
  
   1. Install Mirage in our project:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Using npm&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; miragejs

&lt;span class="c"&gt;# Using Yarn&lt;/span&gt;
yarn add &lt;span class="nt"&gt;--dev&lt;/span&gt; miragejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Create a &lt;code&gt;mirage&lt;/code&gt; folder
&lt;/h2&gt;

&lt;p&gt;Now we need to create a folder called &lt;code&gt;mirage&lt;/code&gt;. I located it at &lt;code&gt;lib&lt;/code&gt; folder with the next structure:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--05KiwhEs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1ju4v9g204jo6dnfxfm0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--05KiwhEs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1ju4v9g204jo6dnfxfm0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Inside &lt;code&gt;index.js&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Inside &lt;code&gt;index.js&lt;/code&gt; we have to create a mirage server which will serve to generate our fake api:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;miragejs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Some imports from routes folder. Ex:&lt;/span&gt;
&lt;span class="c1"&gt;// import { mock1Routes } from './routes/mock1Routes'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createMirageServer&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;routes&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;urlPrefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;API_BASE_URL&lt;/span&gt;

      &lt;span class="c1"&gt;// Examples:&lt;/span&gt;
      &lt;span class="c1"&gt;// mock1Routes(this)&lt;/span&gt;
      &lt;span class="c1"&gt;// mock2Routes(this)&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;server&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;createMirageServer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;urlPrefix&lt;/code&gt; we have to set the API base URL of our development API.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Create the mock routes
&lt;/h2&gt;

&lt;p&gt;Now, inside the folder called &lt;code&gt;routes&lt;/code&gt; we have to create those calls to the api that we want to mock.&lt;br&gt;
For example we create a &lt;code&gt;routes/mock1Routes.ts&lt;/code&gt; file with the next content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;miragejs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mock1Routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/whatever/&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="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;request&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;successCondition&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="nx"&gt;Response&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="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;else&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="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&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="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;mock1Routes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can customize responses according to what we need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;br&gt;
In this example we are mocking a &lt;code&gt;GET&lt;/code&gt; method with &lt;code&gt;server.get&lt;/code&gt;, but we can mock any method: &lt;code&gt;server.post&lt;/code&gt;, &lt;code&gt;server.patch&lt;/code&gt;...&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Configure our Next.js app to use Mirage
&lt;/h2&gt;

&lt;p&gt;To finish, we have to configure our Nextjs app to use this.&lt;br&gt;
I added a &lt;code&gt;.env&lt;/code&gt; variable to switch between use or not use mirage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE_MIRAGE_SERVER=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we need to configure the &lt;code&gt;next.config.js&lt;/code&gt; file in order to use our env variables:&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&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="nx"&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;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;config&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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;API_BASE_URL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;API_BASE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;USE_MIRAGE_SERVER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;USE_MIRAGE_SERVER&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;And now in our &lt;code&gt;_app.tsx&lt;/code&gt; file we have to use this env variable and create the mirage server:&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;USE_MIRAGE_SERVER&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ATTENTION - Using mirage server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;createMirageServer&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;And with this would be everything. We already have a functional API mock for our frontend development. 🎉🎉🎉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;THANKS FOR READ ME&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://i.giphy.com/media/3o6Zt6KHxJTbXCnSvu/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o6Zt6KHxJTbXCnSvu/giphy.gif" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nextjs</category>
      <category>miragejs</category>
      <category>api</category>
    </item>
    <item>
      <title>How to squash migrations in Django</title>
      <dc:creator>Miguel A. Gavilán Merino</dc:creator>
      <pubDate>Sun, 23 Feb 2020 16:15:44 +0000</pubDate>
      <link>https://dev.to/miangame/how-to-squash-migrations-in-django-127e</link>
      <guid>https://dev.to/miangame/how-to-squash-migrations-in-django-127e</guid>
      <description>&lt;p&gt;Today, when I was programming in a project with DRF, I had to create a new app with its respective model.&lt;br&gt;
While I was creating my model, I had to make several changes (add fields, rename fields ...) so several migration files were also generated.&lt;br&gt;
This can generate problems in the future because if for each change in our model a migration file is create, we can have a lot of migration files.&lt;/p&gt;

&lt;p&gt;Django comes with a lot of different management commands that can be executed through the &lt;code&gt;manage.py&lt;/code&gt; file that brings a lot of utilities.&lt;/p&gt;

&lt;p&gt;The squashmigrations is one of these commands that can help us achieve just what we're looking for. It allows us to squash multiple migration files into a single one.&lt;br&gt;
 &lt;br&gt;
For example, if we have the following migration files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./foo
    ./migrations
        0001_initial.py
        0002_project.py
        0003_article_project.py
        0004_auto_20200223_0123.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can squash all of them executing the next command:&lt;br&gt;
&lt;code&gt;python manage.py squashmigrations &amp;lt;our_app&amp;gt; 0004&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That command generates a new file named &lt;code&gt;0001_squashed_0004_auto_&amp;lt;timestamp&amp;gt;.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And if you open this file, you can see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This file is marked as initial=True , meaning this is the new initial migration for your application.&lt;/li&gt;
&lt;li&gt;A new attribute named &lt;code&gt;replaces=[]&lt;/code&gt; and added to the Migration class with a list of strings that represent the file names of all the migrations that were squashed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/lD76yTC5zxZPG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/lD76yTC5zxZPG/giphy.gif" alt="Alt img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks for reading me!
&lt;/h3&gt;

</description>
      <category>django</category>
      <category>migrations</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to automate a deploy in a VPS with GitHub actions via SSH</title>
      <dc:creator>Miguel A. Gavilán Merino</dc:creator>
      <pubDate>Sun, 02 Feb 2020 21:19:20 +0000</pubDate>
      <link>https://dev.to/miangame/how-to-automate-a-deploy-in-a-vps-with-github-actions-via-ssh-101e</link>
      <guid>https://dev.to/miangame/how-to-automate-a-deploy-in-a-vps-with-github-actions-via-ssh-101e</guid>
      <description>&lt;p&gt;Hi everyone.&lt;br&gt;
Some time ago I hired a VPS server to host my portfolio and do tests, but I had a problem. Every time I wanted to push a change to the server, I had to do it manually. The steps I followed were the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connect to VPS via SSH&lt;/li&gt;
&lt;li&gt;Move to project directory&lt;/li&gt;
&lt;li&gt;Make a &lt;code&gt;git pull&lt;/code&gt; to have the new changes on my web&lt;/li&gt;
&lt;li&gt;Typical things of the framework used (Clear cache, etc...)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But with the appearance of Github actions, these steps changed.&lt;br&gt;
Now we can write a script to tell GhActions 'do this deploy for me'.&lt;/p&gt;

&lt;p&gt;This is very easy. Just we should create a &lt;code&gt;yml&lt;/code&gt; file on &lt;code&gt;project/.github/workflows/&lt;/code&gt;. For example &lt;code&gt;/my_project/.github/workflows/ci.yml&lt;/code&gt;&lt;br&gt;
And in this file, we can write something like this:&lt;/p&gt;

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

name: CI

on: [push]

jobs:
  deploy:
    if: github.ref == 'refs/heads/master'
    runs-on: [ubuntu-latest]
    steps:
      - uses: actions/checkout@v1
      - name: Push to server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_IP }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          script: cd ${{ secrets.PROJECT_PATH }} &amp;amp;&amp;amp; git pull


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;on&lt;/code&gt; line says that this job is launched on &lt;code&gt;push&lt;/code&gt; event.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;jobs&lt;/code&gt; block defines what jobs will be executed.

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;if&lt;/code&gt; line says that this job will only run in a push to the master branch.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;runs-on&lt;/code&gt; line says that this job will run on latest ubuntu distro.&lt;/li&gt;
&lt;li&gt;And the &lt;code&gt;steps&lt;/code&gt; block says what are the steps that we are going to execute.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;uses: actions/checkout@v1&lt;/code&gt;. This is a GitHub Action for executing remote ssh commands. &lt;/li&gt;
&lt;li&gt;Finally, in the &lt;code&gt;with&lt;/code&gt; block we configure the parameters for the SSH connection.

&lt;ul&gt;
&lt;li&gt;In the &lt;code&gt;Script&lt;/code&gt; label, we can write the commands that we want to run on our server, for example &lt;code&gt;cd our-project-path/ &amp;amp;&amp;amp; git pull&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note that I use the GitHub secrets to keep important information hidden.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once we have this file in our repository, the next time we push the master branch, it will automatically be deployed to our server.&lt;br&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%2Fi%2Feb42dap5o6t9h35j8m0h.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%2Fi%2Feb42dap5o6t9h35j8m0h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;*It is always important to maintain good test coverage in our application to avoid unwanted errors*&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>deploy</category>
      <category>github</category>
      <category>vps</category>
    </item>
    <item>
      <title>Vscode linters for react.js and python</title>
      <dc:creator>Miguel A. Gavilán Merino</dc:creator>
      <pubDate>Sat, 18 Jan 2020 12:38:36 +0000</pubDate>
      <link>https://dev.to/miangame/vscode-linters-for-react-js-and-python-1fna</link>
      <guid>https://dev.to/miangame/vscode-linters-for-react-js-and-python-1fna</guid>
      <description>&lt;p&gt;Most of the team at &lt;a href="https://theneonproject.org/"&gt;The Neon Project&lt;/a&gt; (included me) uses Visual Studio Code as a favourite editor to code. In this post, I tell you a bit about this editor and share our configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  About Visual Studio Code
&lt;/h2&gt;

&lt;p&gt;Launched in 2015, Visual Studio Code quickly became the favorite editor of many programmers. Although it is made by Microsoft, the code is open and available in Github. Microsoft has done a great job creating a powerful and flexible cross-platform editor. VSCode has a good ecosystem of add-ons (extensions). Extension management is built-in, and there are already several thousand available! Some extensions, such as the ones for Atom, are installed by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our configuration and plugins
&lt;/h2&gt;

&lt;p&gt;If you are new to VSCode, you will have to spend some time choosing the accessories that best suit your workflow, and I want to share our experiences doing these configurations in this post.&lt;/p&gt;

&lt;p&gt;At The Neon Project, we approach most projects with two technologies that we believe have a lot of projection and are very scalable for the future. They are Django (Python) for the Backend part and ReactJS as a JavaScript framework for the Frontend. In order to develop fluently and with the least effort in VSCode, we use the following plugins:&lt;/p&gt;

&lt;h3&gt;
  
  
  ESLint
&lt;/h3&gt;

&lt;p&gt;In the &lt;a href="https://eslint.org/"&gt;official documentation&lt;/a&gt;, ESLint is defined as a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goals of making code more consistent and avoiding bugs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ESLint uses Espree for JavaScript parsing.&lt;/li&gt;
&lt;li&gt;ESLint uses an AST to evaluate patterns in code.&lt;/li&gt;
&lt;li&gt;ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To install it, it's as easy as going to the VSCode Extensions, searching for 'ESLint' and installing it.&lt;/p&gt;

&lt;p&gt;In general, we leave the default settings activated, in particular, I like to fix bugs when the file is saved. To do this, we access the configuration of the 'ESLint' plugin and activate the following option:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n7j3dJ_K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ga5mkenl7y5a0fq40d4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n7j3dJ_K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ga5mkenl7y5a0fq40d4t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prettier
&lt;/h3&gt;

&lt;p&gt;Prettier is a code formatter that enforces a consistent style by parsing your code and reprinting it with its own rules. These rules include considering the maximum line length into account, wrapping code when necessary, etc. &lt;br&gt;
We can use this plugin together with the previous one to have a better code quality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pep8
&lt;/h3&gt;

&lt;p&gt;Pep8 is a Style Guide for Python Code. Like ESLint, we can use Pep8 as Python Linter to improve our code. &lt;br&gt;
To install it, just type the following command: &lt;code&gt;pip install pep8&lt;/code&gt;. &lt;br&gt;
With this we can already use the Python lint in VSCode.&lt;/p&gt;

&lt;h3&gt;
  
  
  Autopep8
&lt;/h3&gt;

&lt;p&gt;Autopep8 is similar to Prettier but for Python. It is a code formatter that serves to have a cleaner and readable code. To install it, we must write the following command in the terminal: &lt;code&gt;sudo -H pip install autopep8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once installed we can use the formatter of our editor, in this case VSCode.&lt;/p&gt;

&lt;p&gt;For both Prettier and Autopep8 we have enabled the code to auto-format when saving files.&lt;/p&gt;

&lt;p&gt;NOTE: In order to see the errors reported by the linters, just look in the 'Problems' window of the editor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rwaM0hxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r7k4a7bq7inqq8w5ax81.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rwaM0hxr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r7k4a7bq7inqq8w5ax81.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can start coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3oKIPnAiaMCws8nOsE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3oKIPnAiaMCws8nOsE/giphy.gif" alt="Alt text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>configuration</category>
      <category>python</category>
      <category>react</category>
    </item>
  </channel>
</rss>
