<?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: Chathu Vishwaijith</title>
    <description>The latest articles on Forem by Chathu Vishwaijith (@iamchathu).</description>
    <link>https://forem.com/iamchathu</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%2F458529%2F04b14f34-a203-452e-b073-1b84b0d80d14.jpeg</url>
      <title>Forem: Chathu Vishwaijith</title>
      <link>https://forem.com/iamchathu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/iamchathu"/>
    <language>en</language>
    <item>
      <title>Deploy a Next.js project to Netlify using GitLab CI</title>
      <dc:creator>Chathu Vishwaijith</dc:creator>
      <pubDate>Wed, 01 Jun 2022 05:42:58 +0000</pubDate>
      <link>https://forem.com/iamchathu/deploy-a-nextjs-project-to-netlify-using-gitlab-ci-5dac</link>
      <guid>https://forem.com/iamchathu/deploy-a-nextjs-project-to-netlify-using-gitlab-ci-5dac</guid>
      <description>&lt;p&gt;&lt;a href="https://nextjs.org"&gt;Next.js&lt;/a&gt; from &lt;a href="https://vercel.com"&gt;Vercel&lt;/a&gt; and it has great support on hosting the project without hassle. &lt;a href="https://netlify.com"&gt;Netlify&lt;/a&gt; is also a JamStack hosting platform and have git based build and deployment support without much hassle too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://about.gitlab.com/topics/ci-cd/"&gt;GitLab CI&lt;/a&gt; is the go to CI platfrom since we have moved our projects to GitLab. From building docker images to deploying to Google Cloud Run is handled by GitLab CI. Setup a &lt;code&gt;yaml&lt;/code&gt; based config file and GitLab take care of the rest.&lt;/p&gt;

&lt;p&gt;All above mentioned services has a generous free tier.&lt;/p&gt;

&lt;p&gt;Netlify support CLI based manual deployments. After doing some research I managed to get up and running with GitLab CI.&lt;/p&gt;

&lt;p&gt;Since Next.js support exporting static web sites and SSR based deployments I will explain the both.&lt;/p&gt;

&lt;p&gt;If you don't have account in Netlify please create one and create personal access token from &lt;a href="https://app.netlify.com/user/applications"&gt;User settings &amp;gt; Applications&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You also need to create a Netlify site to deploy. The way I do it is, install the &lt;code&gt;Netlify CLI&lt;/code&gt; in your device.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;After that login to your account from CLI by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;netlify login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that you can create the project from CLI. (Since to create a project from UI you need to connect a git repository.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;netlify sites:create &lt;span class="nt"&gt;--disable-linking&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an interactive command you have to select your team and give a name your site. Recent update of CLI has linking the site to current directory which we don't need since we do it from the CI.&lt;/p&gt;

&lt;p&gt;After that your will get the site id in the result. Copy it.&lt;/p&gt;

&lt;p&gt;Then these environment variables needs to be saved in &lt;code&gt;Gitlab project &amp;gt; Settings &amp;gt; CI/CD &amp;gt; Variables&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NETLIFY_SITE_ID: &amp;lt;site id of the created site&amp;gt;
NETLIFY_AUTH_TOKEN: &amp;lt;personal access token&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above variables are mandatory to deploy the project. You can set them in the &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; too but &lt;strong&gt;not recommended&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying static Next.js based site
&lt;/h2&gt;

&lt;p&gt;Next.js has support to generate complete static websites. But there are some limitations, Like internationalization, &lt;code&gt;next/image&lt;/code&gt; component are not supported.&lt;/p&gt;

&lt;p&gt;First add these scripts to you &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"export"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"next export"&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;Now create &lt;code&gt;netlify.toml&lt;/code&gt; file in the root of the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[build]&lt;/span&gt;
&lt;span class="err"&gt;command:&lt;/span&gt; &lt;span class="err"&gt;"yarn&lt;/span&gt; &lt;span class="err"&gt;build&lt;/span&gt; &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="err"&gt;yarn&lt;/span&gt; &lt;span class="err"&gt;export"&lt;/span&gt;
&lt;span class="err"&gt;publish:&lt;/span&gt; &lt;span class="err"&gt;"out"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next create or add following task to your &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;stages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;deploy&lt;/span&gt;

&lt;span class="na"&gt;deploy-to-netlify&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deploy&lt;/span&gt;
  &lt;span class="na"&gt;only&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;refs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;master&lt;/span&gt;    
  &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node:lts&lt;/span&gt;
  &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;NETLIFY_USE_YARN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;NEXT_TELEMETRY_DISABLED&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
  &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.yarn&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;/.next/cache&lt;/span&gt;
  &lt;span class="na"&gt;before_script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;yarn global add netlify-cli&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;yarn install --frozen-lockfile --non-interactive --prefer-offline --cache-folder .yarn&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;netlify deploy --prod --build&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This task will run in master branch when commits are pushed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy dynamic Next.js project.
&lt;/h2&gt;

&lt;p&gt;Netlify has &lt;code&gt;Essential Next.js Build Plugin&lt;/code&gt; which we can use. It supports image optimazation now out of the box. I'm going to use the v4 which is currently in beta which also support Next.js 12.&lt;/p&gt;

&lt;p&gt;First we need to install the &lt;code&gt;Essential Next.js Build Plugin&lt;/code&gt; as a dev dependancy.&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 @netlify/plugin-nextjs &lt;span class="nt"&gt;-D&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that you need to update &lt;code&gt;netlify.toml&lt;/code&gt; as the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[build]&lt;/span&gt;
&lt;span class="py"&gt;command&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yarn build"&lt;/span&gt;
&lt;span class="py"&gt;publish&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;".next"&lt;/span&gt;

&lt;span class="nn"&gt;[[plugins]]&lt;/span&gt;
&lt;span class="py"&gt;package&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"@netlify/plugin-nextjs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All other files are remain the same.&lt;/p&gt;

</description>
      <category>netlify</category>
      <category>nextjs</category>
      <category>gitlab</category>
      <category>ci</category>
    </item>
    <item>
      <title>Byte sized tip: Dart and GraphQL (Flutter and GraphQL)</title>
      <dc:creator>Chathu Vishwaijith</dc:creator>
      <pubDate>Tue, 31 May 2022 18:38:06 +0000</pubDate>
      <link>https://forem.com/iamchathu/byte-size-tip-dart-and-graphql-flutter-and-graphql-35d8</link>
      <guid>https://forem.com/iamchathu/byte-size-tip-dart-and-graphql-flutter-and-graphql-35d8</guid>
      <description>&lt;p&gt;I have seen in some tutorials and documentations that write GraphQL query or mutation like bellow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;_getCustomersQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'''
  query Customers(&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;: String) {
   customers(name: &lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;){
     id
     name
     address
     telNo
    }
  }
'''&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;a href="https://fartlang.org/guides/language/language-tour.html#strings"&gt;Dart language tour&lt;/a&gt; we can see there is a way to use "raw" strings.&lt;/p&gt;

&lt;p&gt;So above query can be written without string escaping like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;_getCustomersQuery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sx"&gt;r''&lt;/span&gt;&lt;span class="s"&gt;'
  query Customers(&lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;: String) {
   customers(name: &lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;){
     id
     name
     address
     telNo
    }
  }
'''&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks nice and you can just copy and paste from GraphQL playground or any other tool that you use.&lt;/p&gt;

&lt;p&gt;Do you like to get more tips on using GraphQL with Flutter?&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>dart</category>
      <category>flutter</category>
    </item>
  </channel>
</rss>
