<?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: John Samuel</title>
    <description>The latest articles on DEV Community by John Samuel (@codervtwo).</description>
    <link>https://dev.to/codervtwo</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%2F364408%2F0aae702e-9e1e-477f-9898-70226ef6cabb.png</url>
      <title>DEV Community: John Samuel</title>
      <link>https://dev.to/codervtwo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codervtwo"/>
    <language>en</language>
    <item>
      <title>Creating a JAMstack blog using Contentful and Nuxt with fetch()</title>
      <dc:creator>John Samuel</dc:creator>
      <pubDate>Tue, 05 May 2020 20:37:34 +0000</pubDate>
      <link>https://dev.to/codervtwo/creating-a-jamstack-blog-using-contentful-and-nuxt-with-fetch-2kf9</link>
      <guid>https://dev.to/codervtwo/creating-a-jamstack-blog-using-contentful-and-nuxt-with-fetch-2kf9</guid>
      <description>&lt;p&gt;Sarah Drasner (&lt;a class="comment-mentioned-user" href="https://dev.to/sarah_edo"&gt;@sarah_edo&lt;/a&gt;
) recently posted &lt;a href="https://www.netlify.com/blog/2020/04/20/create-a-blog-with-contentful-and-nuxt/"&gt;a great tutorial&lt;/a&gt; on how to build a blog with Nuxt and Contentful hosted on Netlify.  Sarah is a great teacher and I have learned a lot from her tutorials.  I used this tutorial to build a quick blog and it was super easy.&lt;/p&gt;

&lt;p&gt;In the tutorial, she uses Vuex to store the blog posts for the summary page and the individual dynamic blog pages.   However, I noticed that in the 2.12 release of Nuxt, they updated how the fetch() method works (there is a great &lt;a href="https://nuxtjs.org/blog/understanding-how-fetch-works-in-nuxt-2-12"&gt;blog post&lt;/a&gt; on their site that explains all the details).  Using this updated method makes building a blog even easier --- and you  don't have to involve Vuex at all.&lt;/p&gt;

&lt;p&gt;In Sarah's tutorial, she created an action in her Vuex store like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const actions = {
 async getPosts({ commit }) {
   try {
     if (!client) return;
     const response = await client.getEntries({
       content_type: "blogPost"
     });
     if (response.items.length &amp;gt; 0) commit("updatePosts", response.items);
   } catch (err) {
     console.error(err);
   }
 }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But with the new fetch method, you can move this logic into a Vue component as follows...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
import cms from "~/plugins/contentful"

export default {
  async fetch() {
    try {
      if (!cms) return
      const response = await cms.getEntries({
        content_type: "blog"
      })
      if (response.items.length &amp;gt; 0) this.posts = response.items
    } catch (err) {
      console.error(err)
    }
  },
  ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Similarly, you can use fetch in the dynamic post component as follows...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight vue"&gt;&lt;code&gt;import cms from "~/plugins/contentful"
export default {
  async fetch() {
    try {
      if (!cms) return
      const response = await cms.getEntries({
        content_type: "blog",
        "fields.slug[in]": this.$route.params.slug
      })
      if (response.items.length &amp;gt; 0) this.post = response.items[0].fields
    } catch (err) {
      console.error(err)
    }
  },
  ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice, since I don't have access to the post id, I grab the posts that match the slug in the params and use the first (and only) item in the returned array.&lt;/p&gt;

&lt;p&gt;One of the nice features of this fetch method is that it exposes a &lt;code&gt;$fetchState&lt;/code&gt; object with three useful keys...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$fetchState = {
  pending: true | false,
  error: null | {},
  timestamp: Integer
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For example, you can add some conditional logic in your Vue template to show while the posts are being fetched from Contentful...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"$fetchState.pending"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Fetching posts...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm really enjoying building things with this combination of Nuxt, Contentful and Netlify (I also use TailwindCSS for styling).  And this new &lt;code&gt;fetch()&lt;/code&gt; method makes Nuxt even easier to use with third-party data sources.&lt;/p&gt;

&lt;p&gt;Hope this helps!&lt;/p&gt;

</description>
      <category>nuxt</category>
      <category>contentful</category>
      <category>netlify</category>
    </item>
  </channel>
</rss>
