<?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: Babs Craig</title>
    <description>The latest articles on DEV Community by Babs Craig (@thebabscraig).</description>
    <link>https://dev.to/thebabscraig</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%2F132474%2F245b085a-db6a-490c-950c-31889bbf08ad.jpeg</url>
      <title>DEV Community: Babs Craig</title>
      <link>https://dev.to/thebabscraig</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thebabscraig"/>
    <language>en</language>
    <item>
      <title>The JavaScript Execution Context, Call-stack &amp; Event Loop</title>
      <dc:creator>Babs Craig</dc:creator>
      <pubDate>Mon, 22 Jul 2019 11:14:08 +0000</pubDate>
      <link>https://dev.to/thebabscraig/the-javascript-execution-context-call-stack-event-loop-1if1</link>
      <guid>https://dev.to/thebabscraig/the-javascript-execution-context-call-stack-event-loop-1if1</guid>
      <description>&lt;h4&gt;
  
  
  This is going to be a long one - grab a ☕️ and dig in...
&lt;/h4&gt;

&lt;p&gt;Have you ever looked at a piece of JS code and known what the result of executing that piece of code would be, and yet deep in your mind, you knew you had no idea &lt;em&gt;how&lt;/em&gt; the result came about. Or perhaps you've looked at some asynchronous code like an on click handler or an AJAX call and wondered &lt;em&gt;how the heck&lt;/em&gt; the callback function knew when to fire?&lt;/p&gt;

&lt;p&gt;JavaScript is everywhere. In the browser, on the desktop, in mobile apps, in everyday things around us. &lt;a href="https://en.wikipedia.org/wiki/Jeff_Atwood"&gt;Atwood's&lt;/a&gt; Law seems to fulfill itself more and more each day - "Any application that can be written in JavaScript, will eventually be written in JavaScript."&lt;/p&gt;

&lt;p&gt;It's not news that JavaScript's reach extends far and wide and with it, the number of developers who use it on a daily basis, and yet, a deep knowledge of JavaScript is often hard to come by. This is because JS is one of those languages where you can know just enough to get by and never bother to go really deep.&lt;/p&gt;

&lt;p&gt;This article is about deepening our knowledge of JS by understanding how our JS code gets executed. These laws are governed by the interaction of the Execution Context, Call-stack and Event Loop. The interplay of these three concepts is what allows our code to be executed. A good understanding of these foundational concepts is crucial in order to understand more advanced stuff such as scopes and closures. Let's step right in.&lt;/p&gt;

&lt;p&gt;Whenever you write JavaScript and run it, you are relying on an engine to execute the code for you. This engine can vary depending on the environment you are in and even between different implementations of the same environment. For instance the Chrome browser and Firefox browser use different engines (V8 for the former and SpiderMonkey for the latter).&lt;/p&gt;

&lt;p&gt;The engine is what takes your code and executes it. It follows a series of steps - the first of which is to create a global execution context. This global execution context is usually an anonymous function that serves as a space to run all the code you have written.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Execution Context
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&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;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at a fairly straightforward piece of code. In this sample, we assign a number value to the &lt;code&gt;a&lt;/code&gt; variable, we declare a function &lt;code&gt;foo&lt;/code&gt; and then we call &lt;code&gt;foo&lt;/code&gt; passing in &lt;code&gt;a&lt;/code&gt; as a parameter and then store the return value of that function in &lt;code&gt;b&lt;/code&gt;. If I asked you what the result of this piece of code is, I'm sure you'd have no problems following along and getting the correct answer. If however, I asked &lt;em&gt;how&lt;/em&gt; JavaScript arrived at the answer, you might not be able to give a straight answer. Let's examine the answer to that question together.&lt;/p&gt;

&lt;p&gt;The first thing the engine would do, in the code above would be to create an execution context. There are precise steps which the engine follows and there are two phases to this. The creational phase and the execution phase.&lt;/p&gt;

&lt;p&gt;The first time the code runs, a &lt;code&gt;Global Execution Context&lt;/code&gt; is created. During this &lt;strong&gt;creational phase&lt;/strong&gt; the engine will do a couple of things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a global object. This object is for instance, called &lt;code&gt;window&lt;/code&gt; in the browser or &lt;code&gt;global&lt;/code&gt; in Node.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;this&lt;/code&gt; object binding which points to the object created above.&lt;/li&gt;
&lt;li&gt;Set up a memory heap for storing variables and function references&lt;/li&gt;
&lt;li&gt;Store function declarations in the memory heap above and store every variable within the context with &lt;code&gt;undefined&lt;/code&gt; being assigned as the value.&lt;/li&gt;
&lt;/ul&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%2Faz5xnbpss6szwopkw28x.jpg" 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%2Faz5xnbpss6szwopkw28x.jpg" alt="Global Execution Context Creation" width="600" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our example, during the creational phase, the engine will store the variables &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; and the function declaration &lt;code&gt;foo&lt;/code&gt;. It will also assign &lt;code&gt;undefined&lt;/code&gt; to both variables initially.&lt;/p&gt;

&lt;p&gt;After this phase is done, the engine moves to the &lt;strong&gt;execution phase&lt;/strong&gt;. During the execution phase, the code is run line by line. It is in this phase that variables are assigned their values and functions are invoked.&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%2Fhvfm04jcwb83l3oruovg.jpg" 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%2Fhvfm04jcwb83l3oruovg.jpg" width="600" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there are no function calls in your code, the story ends here. However, for every function you call, the engine creates a new &lt;code&gt;Function Execution Context&lt;/code&gt;. This context is identical to the one above, but instead of creating a global object, this time around an &lt;strong&gt;arguments&lt;/strong&gt; object is created containing a reference to all the parameters passed into the function.&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%2Fm4ranwgawsrto15y72dl.jpg" 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%2Fm4ranwgawsrto15y72dl.jpg" width="600" height="1004"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To return to our example above, during the execution phase, the engine would first get to the variable declaration, &lt;code&gt;a&lt;/code&gt;, and assign the value &lt;code&gt;42&lt;/code&gt; to it. Then it would move on the line where we assign a value to &lt;code&gt;b&lt;/code&gt;. Seeing that that line makes a function call, it would create a new &lt;code&gt;Function Execution Context&lt;/code&gt; and repeat the steps it followed above (with an arguments object being created this time around).&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%2Fyrapjmyk9zdxmo7tk7az.jpg" 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%2Fyrapjmyk9zdxmo7tk7az.jpg" width="600" height="1004"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how does it keep track of all these execution contexts? Especially in a scenario where there are multiple nested function calls or conditionals? How does it know which one is active or which one has been completely executed?&lt;/p&gt;

&lt;p&gt;This introduces us nicely to our next concept - the Call-stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Call-stack
&lt;/h3&gt;

&lt;p&gt;The call-stack is a data structure used to keep track of and manage function execution in a piece of JS code. It's job is to store of all the execution contexts created during code execution and to record which execution context we're actually in as well as those that are still remaining on the stack. When you call a function, the engine pushes that function to the top of the stack, then creates an execution context. From our exploration of the execution context above, we know that this context will either be the global one or a function execution context.&lt;/p&gt;

&lt;p&gt;As each function runs, the call-stack pops it off and moves on to the next function until it is empty and all functions have been run.This sequence is known as &lt;strong&gt;LIFO&lt;/strong&gt; - &lt;strong&gt;Last In First Out&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a function is called, a stack frame is created. This is a location in memory where parameters and variables are stored (remember the memory heap we talked about above?). This memory gets cleared when the function returns (implicitly or explicitly) and the whole context then gets popped off the call-stack.&lt;/p&gt;

&lt;p&gt;Execution contexts are popped off the stack one by one as they complete execution with each one creating a stack frame and when we throw an error, we get what is known as a &lt;strong&gt;stack trace&lt;/strong&gt;, which is what it sounds like - tracing all the execution contexts from the point of the error through to all the contexts we have passed through.&lt;/p&gt;

&lt;p&gt;It is also possible to blow the call-stack by having more frames than the stack is designed to hold. This could happen when calling a function recursively without some sort of exit condition or as I'm sure we have all done at some point in time - when an infinite for-loop is run.&lt;/p&gt;

&lt;p&gt;Take a look at this piece of code:&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;function&lt;/span&gt; &lt;span class="nf"&gt;thirdFunc&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Greetings from thirdFunc()&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;secondFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;thirdFunc&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Greetings from secondFunc()&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;firstFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;secondFunc&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Greetings from firstFunc()&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="nf"&gt;firstFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Greetings from thirdFunc()&lt;/span&gt;
&lt;span class="c1"&gt;// Greetings from secondFunc()&lt;/span&gt;
&lt;span class="c1"&gt;// Greetings from firstFunc()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, &lt;em&gt;how&lt;/em&gt; do we get the result we did?&lt;/p&gt;

&lt;p&gt;When we run this piece of code, the first thing the engine does is make a call to the call-stack and place a &lt;code&gt;main()&lt;/code&gt; or &lt;code&gt;global()&lt;/code&gt; function on the call-stack. This is the main thread of execution of your JS code. The execution context we described in the previous section will enter the creation phase first and then the execution phase will be invoked. When the engine gets to the call to &lt;code&gt;firstFunc()&lt;/code&gt; during this phase, the call-stack will be referenced again and the function execution context for &lt;code&gt;firstFunc()&lt;/code&gt; will be pushed onto the call-stack on top of &lt;code&gt;main()&lt;/code&gt; (Step 2 below).&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%2Fjerm4660skzqotxa6ig1.jpg" 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%2Fjerm4660skzqotxa6ig1.jpg" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the engine will start to execute &lt;code&gt;firstFunc()&lt;/code&gt; since it is at the top of the call-stack. It will in turn create a local execution context and local memory allocation to store the variables, parameters and function declarations in this new context. (The concept of scope is tied to this).&lt;/p&gt;

&lt;p&gt;The very first line of &lt;code&gt;firstFunc()&lt;/code&gt; calls &lt;code&gt;secondFunc()&lt;/code&gt;. At this point, the engine will again reference the call-stack and place &lt;code&gt;secondFunc()&lt;/code&gt; at the top of the stack repeating the process again. In &lt;code&gt;secondFunc()&lt;/code&gt; the first line again references another function called &lt;code&gt;thirdFunc()&lt;/code&gt; and the process is repeated one more time.&lt;/p&gt;

&lt;p&gt;Now in &lt;code&gt;thirdFunc()&lt;/code&gt;, we do not make any function call, instead we simply console.log the string &lt;em&gt;"Greetings from thirdFunc()"&lt;/em&gt;. This gets executed and then since there are no more instructions in the function, it returns implicitly. At this point, the call-stack pops &lt;code&gt;thirdFunc()&lt;/code&gt; off (Step 4 above) and now &lt;code&gt;secondFunc()&lt;/code&gt; is at the to the top of the stack. The engine will continue where we left off and console.log the string &lt;em&gt;"Greetings from secondFunc()"&lt;/em&gt;. Again, as there are no more instructions in this function, the function will return and call-stack will pop off &lt;code&gt;secondFunc()&lt;/code&gt; bringing us back to the execution context of &lt;code&gt;firstFunc()&lt;/code&gt; where we continue and log out the string &lt;em&gt;"Greetings from firstFunc()"&lt;/em&gt;. After executing that code, &lt;code&gt;firstFunc()&lt;/code&gt; is popped off and control returned to the main execution context which has no further instructions to execute and will be popped in turn. Once our stack is empty, the program will stops running.&lt;/p&gt;

&lt;p&gt;The nature of the call-stack reflects the fact that JavaScript is essentially single threaded and only one execution context can be run at a time. This means that while a function is being executed, the engine cannot run another context at the same time. It also means that every time a function is pushed onto the call-stack, it then becomes the active executing context and takes control flow away from whatever function called it, until it &lt;em&gt;returns&lt;/em&gt; either explicitly (with a &lt;code&gt;return&lt;/code&gt; statement) or implicitly (when all instructions have been executed).&lt;/p&gt;

&lt;p&gt;Now if this was where the story ended, then JavaScript would not be much use in anything but the most trivial of applications and certainly not in a web application with a multitude of concurrent events firing at once - user inputs, resource requests, API calls. Each event would block the other until it had finished running. This would mean that when a function was called - perhaps one making a request to a server for an image - nothing else could happen on the page till that image was loaded. If you clicked a link before the image got loaded, the event would not be handled until after the image got loaded.&lt;/p&gt;

&lt;p&gt;So how then do we achieve asynchronous JavaScript with it's illusion of multiple things happening all at once? Enter the event loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Event Loop
&lt;/h3&gt;

&lt;p&gt;As we've seen above, the JavaScript engine can really only do one thing at a time. It starts at the top of our code and works it's way down creating new execution contexts as required and pushing and popping them onto and off of the call-stack.&lt;/p&gt;

&lt;p&gt;If you have a blocking function that takes a long time to execute, then the browser cannot do anything during the time that the function is at the top of the call-stack. No new execution contexts or code execution can take place. This means that even user input like scrolls and button click events would not work.&lt;/p&gt;

&lt;p&gt;Instead, when we have a function that might take a long time to complete, oftentimes we provide a callback function. This function encapsulates the code we would like to run at a later time when the blocking action (e.g a network call) has been resolved. This allows us to return control to the JS engine and defer the rest of the execution until after the call-stack has been cleared. This is the concept of asynchrony in JavaScript.&lt;/p&gt;

&lt;p&gt;Let's tweak our code from before into something requiring this new concept:&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;function&lt;/span&gt; &lt;span class="nf"&gt;thirdFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Greetings from thirdFunc()&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="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&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;secondFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;thirdFunc&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Greetings from secondFunc()&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;firstFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;secondFunc&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Greetings from firstFunc()&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="nf"&gt;firstFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Greetings from secondFunc()&lt;/span&gt;
&lt;span class="c1"&gt;// Greetings from firstFunc()&lt;/span&gt;
&lt;span class="c1"&gt;// approx. 5 seconds later...&lt;/span&gt;
&lt;span class="c1"&gt;// Greetings from thirdFunc()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the execution begins as in the previous example. When the engine reaches the third function however, instead of immediately logging the message to the console, it invokes &lt;code&gt;setTimeout()&lt;/code&gt; which is an API provided to us by the browser environment. This function accepts a "callback" function which will be stored in a structure we have not discussed yet called the callback queue. &lt;code&gt;thirdFunc()&lt;/code&gt; will then complete it's execution, returning control to &lt;code&gt;secondFunc()&lt;/code&gt; and &lt;code&gt;firstFunc()&lt;/code&gt; in turn. Finally after &lt;em&gt;at least&lt;/em&gt; 5 seconds (more on this below), the message from &lt;code&gt;thirdFunc()&lt;/code&gt; is logged to the console.&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%2Fxnlmkcgxkrcz2ue9p5pm.jpg" 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%2Fxnlmkcgxkrcz2ue9p5pm.jpg" width="800" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In JavaScript, the mechanism by which we achieve asynchronous execution of code is via environment APIs (Node and the browser both provide certain APIs that expose low level features to us), the callback queue and the event loop,.&lt;/p&gt;

&lt;p&gt;Concurrency (or the illusion of it) is achieved via these extra mechanisms.&lt;/p&gt;

&lt;p&gt;Just as we said the call-stack was used to keep track of the currently executing function context, the callback queue keeps track of any execution contexts that need to be run at a later time. Such as a callback passed to a setTimeout function or a node async task. While our code is being invoked, the event loop periodically checks if the call-stack is empty. Once the call-stack has run all the execution contexts in our code, the event loop takes the first function that entered the callback queue and places it on the call-stack to be executed. Then repeats the process again continually checking both the call-stack and the callback queue and passing functions from the callback queue onto the call-stack once the call-stack is empty.&lt;/p&gt;

&lt;p&gt;Remember when we said the setTimeout callback would run "at least" 5 seconds from the point of invoking setTimeout? This is because setTimeout does not just insert it's code into the call-stack when the timeout completes, it must pass it to the callback queue and then wait for the event loop to place it onto the call-stack &lt;em&gt;when the call-stack is empty.&lt;/em&gt; So long as there are still items in the call-stack, the setTimeout callback will not be run. Let's take a look at this in detail.&lt;/p&gt;

&lt;p&gt;Our code runs as above until we get to the &lt;code&gt;thirdFunction&lt;/code&gt; at this point, setTimeout is invoked, taken off the call-stack and begins a countdown. Our code continues on to &lt;code&gt;secondFunc&lt;/code&gt; and &lt;code&gt;firstFunc&lt;/code&gt; and console.logs their messages in turn. In the meantime, setTimeout completed it's countdown almost immediately - in 0 seconds - but there was no way for it to get it's callback directly onto the call-stack. Instead when it completed it's countdown, it passed the callback to the callback queue. The event loop kept checking the call-stack but during that time &lt;code&gt;secondFunc&lt;/code&gt; and in turn &lt;code&gt;firstFunc&lt;/code&gt; occupied space on the call-stack. It was not until these two functions completed execution and the call-stack was emptied, that the event loop takes the callback function we passed to &lt;code&gt;setTimeout&lt;/code&gt; and places it on the call-stack to be executed.&lt;/p&gt;

&lt;p&gt;This is why sometimes you find the pattern of calling setTimeout with &lt;code&gt;0&lt;/code&gt; as a way to defer execution of the code in the callback passed to it. We simply want to ensure that all other synchronous code runs before the code in the &lt;code&gt;setTimeout&lt;/code&gt; callback.&lt;/p&gt;

&lt;p&gt;It's important to also note that a "callback" is a function that's called by another function, but the callbacks we've discussed above, such as the one that is passed to &lt;code&gt;setTimeout&lt;/code&gt; are "asynchronous callbacks". The distinction being that async callbacks are passed to the callback queue to await being placed (by the event loop) onto the call-stack for execution at a later time.&lt;/p&gt;

&lt;p&gt;And with this, we've covered the major concepts when it comes to JavaScript code execution and how the JavaScript engine handles asynchronous code. We've seen that the JS engine is single threaded and can only execute code synchronously. We've also seen the mechanism for achieving asynchronous code without blocking the thread of execution. We also have a better understanding of the order in which functions are executed and the rules surrounding this process.&lt;/p&gt;

&lt;p&gt;These concepts can be a lot to understand but it's worth taking the time to really grasp them as they form the basis for an in-depth knowledge of JavaScript. Not just the &lt;code&gt;var a = 2&lt;/code&gt; syntax but a wholistic view of &lt;em&gt;what exactly happens&lt;/em&gt; when JavaScript takes that syntax and runs it. These concepts also act as a building block for a greater understanding of other concepts such as scopes and closures. A subject like this requires further resources so feel free to dig in below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ"&gt;What the heck is the event loop anyway? — Philip Roberts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/playlist?list=PLWrQZnG8l0E4kd1T_nyuVoxQUaYEWFgcD"&gt;Understanding JavaScript Execution — Codesmith&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tylermcginnis.com/ultimate-guide-to-execution-contexts-hoisting-scopes-and-closures-in-javascript/"&gt;The Ultimate Guide to Execution Contexts, Hoisting, Scopes, and Closures in JavaScript — Tyler McGinnis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>From Chrome To Firefox Quantum in 4 Easy Steps </title>
      <dc:creator>Babs Craig</dc:creator>
      <pubDate>Wed, 06 Mar 2019 17:16:20 +0000</pubDate>
      <link>https://dev.to/thebabscraig/from-chrome-to-firefox-quantum-in-4-easy-steps--5572</link>
      <guid>https://dev.to/thebabscraig/from-chrome-to-firefox-quantum-in-4-easy-steps--5572</guid>
      <description>&lt;h2&gt;
  
  
  Bookmarks, extensions, history and more - How to pull the plug on Chrome and jump right into Firefox
&lt;/h2&gt;

&lt;p&gt;I recently got the 15 inch, 2017 MacBook Pro, a radical upgrade from the 3 different MacBook Airs I’ve used for the last 6 years. It’s been a wonderful new world of retina color, huge bump in screen real estate and  faster operating speeds. But this is not a review of the MacBook Pro, it’s a story of how the morning after, while running my usual developer workflow (Chrome, Visual Studio Code and iTerm), my MacBook Pro started to run hot, so hot the fans started blowing loudly. I immediately checked Activity Monitor and lo and behold, Google Chrome was the culprit. I’d heard before how much RAM Chrome loves to hog and it seemed that it was gunning for all 16GB of memory that I had.&lt;/p&gt;

&lt;p&gt;This reminded me that I’d tried several times to get weaned off Chrome for a variety of reasons. The first is that I’ve found Firefox Developer Edition to have a better user experience when it comes to developer tools. I just like the look and feel of debugging a web app in Firefox. I’m also a tad bit nostalgic about the browser I used as a kid when the internet was a new and exciting place to me. I’d also read that the new Quantum browser used up to 30% less memory than Chrome and perhaps most importantly, I like the way Firefox treats user privacy and shies away from aggressive collection of data and tracking.&lt;/p&gt;

&lt;p&gt;That said, I’ve always found myself drawn back to Chrome because I have all my browser history, bookmarks, extensions and passwords in Chrome from years of using it as my daily browser. Every time I tried it would end up going go like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4oph3bk2wy033zesdo4u.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4oph3bk2wy033zesdo4u.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time around though, I promised myself that I would make the jump and give Firefox a fair shot at winning me over. Here’s how I finally got everything over from Chrome into Firefox Developer Edition. If you’re like me and looking to try out Firefox for a bit to see if it sticks, then follow the steps below and you’ll be up and running in no time. With the exception of the last step, which I haven’t had the opportunity to try on another laptop, most of these instructions will work on any machine regardless of OS.&lt;/p&gt;

&lt;p&gt;Let’s get cracking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step One: Download Firefox
&lt;/h3&gt;

&lt;p&gt;The easy part - download your new browser. You can get Firefox Quantum Developer Edition &lt;a href="https://www.mozilla.org/en-US/firefox/developer/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Follow the instructions and &lt;em&gt;fire&lt;/em&gt; it up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Two: Bookmarks &amp;amp; History
&lt;/h3&gt;

&lt;p&gt;Let’s import our bookmarks and history. This will ensure you feel more at home in Firefox. Autocomplete in the url field, all your sites just a click away and bookmarks from those sites we promised we would visit one day 😉 . To do this, click on the stacked books icon in the toolbar, select &lt;code&gt;Bookmarks&lt;/code&gt; scroll all the way down to &lt;code&gt;Show all Bookmarks&lt;/code&gt; (or &lt;code&gt;cmd+shift+B&lt;/code&gt;) and click on the star icon. Select &lt;code&gt;Import Data From Another Browser&lt;/code&gt; and choose Chrome.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxs47ire3dbclh77vrmph.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxs47ire3dbclh77vrmph.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure you quit Chrome. You might also want to check your Activity Monitor and make sure no Chrome processes or helpers are running in the background. Now you can select the items you want to import.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fo78vrj699yt03hd541d9.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fo78vrj699yt03hd541d9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After selecting the data you want, click continue and let Firefox do it’s work. Once the process is compete, you should have your cookies, history and bookmarks in Firefox.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Three: Extensions
&lt;/h3&gt;

&lt;p&gt;The next thing is our extensions. You can open up the extensions manager in Chrome and then in your Firefox browser, search for any extensions you want to use. I found a Firefox counterpart for every Chrome extension that I intended to keep. I also used this process as a time to do some house-keeping and pull a Marie Kondo on my extensions list. You might be surprised at the number of extensions that no longer serve a useful purpose or “spark joy” 😂 . It’s also a great idea to keep extensions light, especially those that run in the background. They add to your browser’s overall load and affect how much memory is consumed. You can find Firefox "Add-ons" as they're called, &lt;a href="https://addons.mozilla.org/en-US/firefox/extensions/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step Four: Passwords and Login
&lt;/h3&gt;

&lt;p&gt;This step is a bit more involved but we’ve come so far and we can do it. This one was really big for me. I have dozens of variations of my password and I have come to rely on a carefully worked out system, where the browser takes care of some of them and the rest I carry in my head or delegate to social logins. This proved to be my undoing the last time I tried to move over from Chrome as I just got tired of having to reset so many account passwords.&lt;/p&gt;

&lt;p&gt;First, get your passwords exported from google in a &lt;code&gt;.csv&lt;/code&gt; file. This can be done by going into Chrome and going into your settings. Click on &lt;code&gt;passwords&lt;/code&gt; in the Autofill section and then click the three dot menu button and select &lt;code&gt;Export passwords&lt;/code&gt;. Make sure to keep this file local. Don’t put it in any shared folders. It’s extremely sensitive information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fts8xrgaj1u7x1c5u3473.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fts8xrgaj1u7x1c5u3473.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll be asked to enter your computer password. Do this and choose the target local folder (see above), leave the file type as &lt;code&gt;.csv&lt;/code&gt; and input “password” as the name of the file.&lt;/p&gt;

&lt;p&gt;Next, you’ll need to install a command line tool called &lt;code&gt;ffpass&lt;/code&gt;. You can find the GitHub repo &lt;a href="https://github.com/louisabraham/ffpass" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To install this tool, you’ll need Python 3. If you don’t have version three,  you can use homebrew by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install python3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will also install &lt;code&gt;pip3&lt;/code&gt; which is the python installer. If your MacBook is setup like mine, you’ll need to change the command line aliases for both &lt;code&gt;python&lt;/code&gt; and &lt;code&gt;pip&lt;/code&gt; commands to point to the newly installed &lt;code&gt;python3&lt;/code&gt; and &lt;code&gt;pip3&lt;/code&gt; and not the python and pip versions 2.* which ships with MacBooks by default.&lt;/p&gt;

&lt;p&gt;To do so, open your &lt;code&gt;.bash_profile&lt;/code&gt; and set up some new aliases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ open ~/.bash_profile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the text file that appears add these two lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alias python='python3'
alias pip='pip3'

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

&lt;/div&gt;



&lt;p&gt;Now we can install &lt;code&gt;ffpass&lt;/code&gt; by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install ffpass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It shouldn’t take long to install, but once it does, we can now import our &lt;code&gt;password.csv&lt;/code&gt; file’s data  into Firefox . Make sure Firefox is closed and then enter the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ffpass import --from /path/to/passwords.csv -d /Users/YOUR_HOME_FOLDER_NAME_HERE/Library/Application\ Support/Firefox/Profiles/91ybe9eg.dev-edition-default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we’re doing here is importing from the path where our &lt;code&gt;passwords.csv&lt;/code&gt; file is and writing to the location where Firefox can view and consume the data. Be sure to &lt;em&gt;permanently delete&lt;/em&gt; the password file once this command runs as this is sensitive information and shouldn’t sit around on your computer or be shared with anyone.&lt;/p&gt;

&lt;p&gt;And that’s it! Open up Firefox again and it should feel more like the browser you’re used to. You’d still be logged out of most your accounts but the form autofill data should have you easily setting  things up again with just a click.&lt;br&gt;
I’m hoping to use Firefox for a few weeks and see how I feel and more importantly, how my computer feels about it. So far I haven’t really missed Chrome and even with my web development, I’m finding the Firefox environment to be a welcome change. Let’s see how it goes.&lt;/p&gt;

&lt;p&gt;How about you? Are you planning on giving Firefox Quantum a try or have you done so already? Let’s compare notes in the comments below!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>workflow</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Content Management with Gatsby, Netlify and Contentful</title>
      <dc:creator>Babs Craig</dc:creator>
      <pubDate>Fri, 22 Feb 2019 02:55:23 +0000</pubDate>
      <link>https://dev.to/thebabscraig/content-management-with-gatsby-netlify-and-contentful-3kbg</link>
      <guid>https://dev.to/thebabscraig/content-management-with-gatsby-netlify-and-contentful-3kbg</guid>
      <description>&lt;h3&gt;
  
  
  Gatsby, Netlify and Contentful - The Triple Tag Team For Content Management Success
&lt;/h3&gt;

&lt;p&gt;I've been using Gatsby for the better part of 6 months now. And it's quickly become my go-to for building static sites. The advantages are huge. You get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A lot of configuration and boilerplate done out of the box.&lt;/li&gt;
&lt;li&gt;Speed, SEO and performance optimizations.&lt;/li&gt;
&lt;li&gt;A great community, great docs, a growing plugin ecosystem.&lt;/li&gt;
&lt;li&gt;And my personal favourite - getting to write all the React &amp;amp; GraphQL code I want.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's been a great developer experience, to say the least. However, when building static sites, one of the major concerns in choosing a tool is how content gets updated on the site. Lots of older platforms have solved this one way or another, Wordpress being the most popular of them, but using the triple threat of Gatsby, Netlify, and Contentful, we can build a pretty good alternative to the traditional CMS tools out there while retaining our SEO compatibility.&lt;/p&gt;

&lt;p&gt;This article will show you how you can set up a system for managing content on any page of your Gatsby site. In our case, we'll be using Gatsby's powerful &lt;code&gt;gatsby-node&lt;/code&gt; API to pull in content from Contentful and to dynamically generate pages. You can also use Contentful data on any existing page via the provided &lt;code&gt;graphql&lt;/code&gt; queries.&lt;/p&gt;

&lt;p&gt;Let's begin.&lt;/p&gt;

&lt;p&gt;You'll need the &lt;code&gt;gatsby-cli&lt;/code&gt;  tool to get started. Run &lt;code&gt;npm i -g gatsby&lt;/code&gt; in your terminal and once that has run, create a new project with&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gatsby new gatsby-contentul-blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create a new Gatsby project in a folder called &lt;code&gt;gatsby-contentful-blog&lt;/code&gt;. &lt;code&gt;cd&lt;/code&gt; into the new project and run &lt;code&gt;gatsby develop&lt;/code&gt;. Now you have the default Gatsby starter homepage:&lt;/p&gt;

&lt;p&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%2F72gs4camtc085us34ahb.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%2Fuploads%2Farticles%2F72gs4camtc085us34ahb.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open up the project in your favourite text editor and navigate to the &lt;code&gt;pages&lt;/code&gt; folder. Let's tweak some of the content in the &lt;code&gt;index.js&lt;/code&gt;: (You can just copy and paste this in)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import React from "react";
    import { Link } from "gatsby";

    import Layout from "../components/layout";
    import Image from "../components/image";
    import SEO from "../components/seo";
    import "./index.css";

    const IndexPage = () =&amp;gt; (
      &amp;lt;Layout&amp;gt;
        &amp;lt;SEO title="Home" keywords={[`gatsby`, `application`, `react`]} /&amp;gt;
        &amp;lt;div className="home"&amp;gt;
          &amp;lt;h1&amp;gt;Hello There&amp;lt;/h1&amp;gt;
          &amp;lt;p&amp;gt;Welcome my awesome blog&amp;lt;/p&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;div
              style={{
                maxWidth: `300px`,
                margin: "0 auto 1.45rem"
              }}
            &amp;gt;
              &amp;lt;Image /&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;Link to="/blogposts/"&amp;gt;View all posts&amp;lt;/Link&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/Layout&amp;gt;
    );

    export default IndexPage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, find page-2.js and change the filename to blogposts.js. Gatsby uses the name of any file in the pages folder as a route name and will make the exported React component available on said route. This means we now have a /blogposts route. We'll come back to this file later but in the meantime, let's also change a few values in the gatsby-config.js file. This file sits in the project root.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;siteMetadata: {
        title: `My Awesome Blog`,
        description: `An awesome blog displaying my awesome posts.`,
        author: `YOUR_NAME`,
    },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! We now have our basic site set up. So we'll go to the &lt;a href="https://www.contentful.com/" rel="noopener noreferrer"&gt;Contentful&lt;/a&gt; website and create a new account. It's pretty painless and you should be set up in no time. By default they provide an example space but let's create a fresh one for the project.&lt;/p&gt;

&lt;p&gt;Open up the sidebar and click on &lt;strong&gt;Create Space&lt;/strong&gt;. Choose the free option and give your space any name. I'll call mine &lt;strong&gt;gatsby-blog&lt;/strong&gt;. Select the empty space option, click &lt;strong&gt;Proceed to confirmation,&lt;/strong&gt; and confirm your options.&lt;/p&gt;

&lt;p&gt;After confirming, on the dashboard, click either the "Create Content Type" button or the "Content Model" button in the header and fill in the form that appears. Let's call the content type &lt;strong&gt;Blog Post&lt;/strong&gt; and leave the API Identifier as is. Enter any description you'd like.&lt;/p&gt;

&lt;p&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%2Fmw96e5tftkvb21l8wc3i.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%2Fuploads%2Farticles%2Fmw96e5tftkvb21l8wc3i.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating the content type, we'll start to add some fields to it A field is a building block for our content. If you have a blog post for instance, a few fields could be the &lt;em&gt;title&lt;/em&gt;, the &lt;em&gt;body&lt;/em&gt;, the &lt;em&gt;tags&lt;/em&gt; and an &lt;em&gt;image&lt;/em&gt;. This will generate a form that you'll fill later on when we start to create actual blog posts. Follow the next steps to create a &lt;em&gt;title&lt;/em&gt; field.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on the &lt;strong&gt;Add Field&lt;/strong&gt; button to the right of the dashboard.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Text&lt;/strong&gt; as the type of field you want.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&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%2Fpn3b748mzx9v0opolgkv.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%2Fuploads%2Farticles%2Fpn3b748mzx9v0opolgkv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add another field. Select &lt;strong&gt;Media&lt;/strong&gt; as the type instead of &lt;strong&gt;Text&lt;/strong&gt; and call it &lt;strong&gt;image&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Add a &lt;em&gt;tags&lt;/em&gt; field by selecting Text as the type. Give it &lt;strong&gt;tags&lt;/strong&gt; as a name, and then select the &lt;strong&gt;list&lt;/strong&gt; option on the screen below since we will be storing a list of &lt;em&gt;tags&lt;/em&gt; in this field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&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%2Fl6z8z21szrjoswjjmx7h.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%2Fuploads%2Farticles%2Fl6z8z21szrjoswjjmx7h.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, create a slug field. Start by selecting Text as the type and call it slug. This time, instead of clicking Create as above, click on Create and Configure. On the next screen go to the Appearance tab and select slug as the way the field should be displayed. Also, in the Validations tab select unique field to be sure that no two blog posts have the same slugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&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%2F750eppimag6ri7an3y7d.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%2Fuploads%2Farticles%2F750eppimag6ri7an3y7d.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your content model should now look like this:&lt;/p&gt;

&lt;p&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%2Frc53x9b8zqx5vxear40m.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%2Fuploads%2Farticles%2Frc53x9b8zqx5vxear40m.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A content model is like a schema that our actual content will follow. You can create all types of models such as case studies, blog posts, product data, page content and so on.&lt;/p&gt;

&lt;p&gt;Save your changes and click on the &lt;strong&gt;Content&lt;/strong&gt; button at the top of the page and select &lt;strong&gt;Add Blog Post&lt;/strong&gt;. I'm going to add three posts with placeholder data, feel free to add as many as you'd like. For images, you can grab some free, open license ones from &lt;a href="https://unsplash.com" rel="noopener noreferrer"&gt;unsplash.com&lt;/a&gt;. Notice how the &lt;code&gt;slug&lt;/code&gt; field gets auto-generated as you enter the title? This will come in handy later.&lt;/p&gt;

&lt;p&gt;Awesome! That was a lot but we're halfway there...&lt;/p&gt;

&lt;p&gt;At this point we have our first couple of blog posts and it's time to bring them into our Gatsby site. For this, we'll depend on Gatsby's awesome GraphQL API for pulling in the data. Let's work on that next.&lt;/p&gt;

&lt;p&gt;Go to your settings in Contentful and click on the &lt;strong&gt;API Keys&lt;/strong&gt; option in the dropdown menu. Create a new API Key and keep the details close by.&lt;/p&gt;

&lt;p&gt;Back in your terminal, install the Gatsby plugin we need to start pulling in our Contentful data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add gatsby-source-contentful
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll be using Contentful's &lt;em&gt;Content Delivery API&lt;/em&gt; since we want to retrieve published data only, so be sure to grab the &lt;em&gt;Content Delivery API&lt;/em&gt; key and not the &lt;em&gt;Content Preview API key&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;gatsby-config.js&lt;/code&gt; file, add the configuration object to the &lt;code&gt;plugins&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins: [
        ...
    {
      resolve: `gatsby-source-contentful`,
      options: {
        spaceId: `YOUR_SPACE_ID`,
        accessToken: `YOUR_CONTENT_DELIVERY_API_KEY`
      }
    }
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should restart your development server again at this point for the new configs to kick in. When the server restarts, &lt;code&gt;gatsby-source-contentful&lt;/code&gt;'s GraphQL queries will be available to use.&lt;/p&gt;

&lt;p&gt;We can easily test if everything is working by using the GraphiQL playground that Gatsby provides for us. Open &lt;a href="http://localhost:8000/___graphql" rel="noopener noreferrer"&gt;http://localhost:8000/___graphql&lt;/a&gt; in your browser and run the query below by pasting it into the left window on the page. The query name is &lt;code&gt;allContentfulBlogPost&lt;/code&gt; because our content model is called &lt;strong&gt;Blog Pos&lt;/strong&gt;t. If we had called it &lt;strong&gt;Product&lt;/strong&gt; or &lt;strong&gt;Case Study&lt;/strong&gt;, then the query made available to us would have been &lt;code&gt;allContentfulProduct&lt;/code&gt; or &lt;code&gt;allContentfulCaseStudy&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;{
  allContentfulBlogPost {
    edges {
      node {
        id
    slug
        title
        tags
        image {
          file {
            url
          }         
        }
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;gatsby-source-contentful&lt;/code&gt; plugin handles all the behind the scenes fetching from the Contentful API using the keys we provided in the &lt;code&gt;gatsby-config&lt;/code&gt; file. It then makes a semantically named GraphQL query available to us.&lt;/p&gt;

&lt;p&gt;If it all works, you should see the content you added in the results window to the right of the GraphiQL window in JSON format.&lt;/p&gt;

&lt;p&gt;Now that we have connected our Gatsby blog with our Contentful data, we can start building the pages for the blog. Gatsby provides us with a file called &lt;code&gt;gatsby-node.js&lt;/code&gt;. This file can be used to dynamically add pages to your site. When Gatsby runs, it will look at the code here and create any pages you tell it to. In the &lt;code&gt;gatsby-node.js&lt;/code&gt; file, let's place the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require(`path`);
const slash = require(`slash`);

exports.createPages = ({ graphql, actions }) =&amp;gt; {
  const { createPage } = actions;
  // we use the provided allContentfulBlogPost query to fetch the data from Contentful
  return graphql(
    `
      {
        allContentfulBlogPost {
          edges {
            node {
              id
              slug
            }
          }
        }
      }
    `
  ).then(result =&amp;gt; {
      if (result.errors) {
        console.log("Error retrieving contentful data", result.errors);
      }

      // Resolve the paths to our template
      const blogPostTemplate = path.resolve("./src/templates/blogpost.js");

      // Then for each result we create a page.
      result.data.allContentfulBlogPost.edges.forEach(edge =&amp;gt; {
        createPage({
          path: `/blogpost/${edge.node.slug}/`,
          component: slash(blogPostTemplate),
          context: {
                        slug: edge.node.slug,
            id: edge.node.id
          }
        });
      });
    })
    .catch(error =&amp;gt; {
      console.log("Error retrieving contentful data", error);
    });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This module exports a function called &lt;code&gt;createPages&lt;/code&gt;. This function has two parameters, graphql and an actions object. We extract the &lt;code&gt;createPage&lt;/code&gt; action then call the same Graphql query we ran in the GraphiQL playground earlier. We take this result and for each result (each blog post) we call the &lt;code&gt;createPage&lt;/code&gt; function. This function accepts a config object which Gatsby reads when rendering the page. We set the path equal to the concatenated string &lt;code&gt;"/blogpost"&lt;/code&gt; plus the &lt;code&gt;slug&lt;/code&gt;. Notice that we also reference a template file at &lt;code&gt;./src/templates/blogpost.js&lt;/code&gt;, not to worry, we'll create that file soon.&lt;/p&gt;

&lt;p&gt;At this point, kill your server and start it up again. If you enter a dud route like &lt;a href="http://localhost:8000/dskl;sfd/" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:8000/some-non-existent-route/&lt;/code&gt;&lt;/a&gt; you'll see Gatsby's development 404 page. This page has a list of all the routes and as you can see the newly created pages have been set up.&lt;/p&gt;

&lt;p&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%2Fnlgv1r3k9e8s50zp9il5.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%2Fuploads%2Farticles%2Fnlgv1r3k9e8s50zp9il5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See why we chose to have a unique slug field? Each post has to have a unique route and using slugs looks so much nicer than using nonsensical ID strings in the URL. Also since Gatsby generates a static site which can have a sitemap, it's much better for SEO to have your route names match the kind of content you want to rank for.&lt;/p&gt;

&lt;p&gt;Now we can concentrate on building out the actual pages.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;templates&lt;/code&gt; folder inside your &lt;code&gt;src&lt;/code&gt; folder and add a file called &lt;code&gt;blogpost.js&lt;/code&gt;. This will be our template component which will be used every time Gatsby calls the &lt;code&gt;createPage&lt;/code&gt; function in the &lt;code&gt;gatsby-node.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Be sure to restart your server at this point if you get any errors. We're doing a lot of config stuff and Gatsby may need a restart in order to run everything properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Link, graphql } from "gatsby";
import Layout from "../components/layout";
import SEO from "../components/seo";

const BlogPost = ({ data }) =&amp;gt; {
  const { title, body, image, tags } = data.contentfulBlogPost;
  return (
    &amp;lt;Layout&amp;gt;
      &amp;lt;SEO title={title} /&amp;gt;
      &amp;lt;div className="blogpost"&amp;gt;
        &amp;lt;h1&amp;gt;{title}&amp;lt;/h1&amp;gt;
        &amp;lt;img alt={title} src={image.file.url} /&amp;gt;
        &amp;lt;div className="tags"&amp;gt;
          {tags.map(tag =&amp;gt; (
            &amp;lt;span className="tag" key={tag}&amp;gt;
              {tag}
            &amp;lt;/span&amp;gt;
          ))}
        &amp;lt;/div&amp;gt;
        &amp;lt;p className="body-text"&amp;gt;{body.body}&amp;lt;/p&amp;gt;
        &amp;lt;Link to="/blogposts"&amp;gt;View more posts&amp;lt;/Link&amp;gt;
        &amp;lt;Link to="/"&amp;gt;Back to Home&amp;lt;/Link&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/Layout&amp;gt;
  );
};

export default BlogPost;

export const pageQuery = graphql`
  query($slug: String!) {
    contentfulBlogPost(slug: { eq: $slug }) {
      title
      slug
      body {
        body
      }
      image {
        file {
          url
        }
      }
      tags
    }
  }
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the bottom of the page, we export a Graphql query. Gatsby will run this query at runtime and will pass a &lt;strong&gt;data&lt;/strong&gt; prop to &lt;strong&gt;BlogPost&lt;/strong&gt; containing the Contentful data. Note that in this case we are querying a single post and passing the slug along as a filter parameter. We're basically asking for the post which matches the passed in slug  (&lt;code&gt;contentfulBlogPost(slug: { eq: $slug })&lt;/code&gt;). This slug is made available to us because we passed it in as a page context in our &lt;code&gt;gatsby-config.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The rest is straightforward React. We create a component and using the &lt;strong&gt;data&lt;/strong&gt; prop, we populate the page content. We have no styling yet but we'll get to that in a bit.&lt;/p&gt;

&lt;p&gt;What we need now is a page to list all the available blog post pages. We cannot rely on going to the 404 page every time we need to read a blog post!&lt;/p&gt;

&lt;p&gt;Let's head back to the &lt;code&gt;blogposts.js&lt;/code&gt; file in the &lt;code&gt;pages&lt;/code&gt; folder that we created at the beginning of this project and tweak it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { Link, graphql } from "gatsby";

import Layout from "../components/layout";
import SEO from "../components/seo";

const BlogPosts = ({ data }) =&amp;gt; {
  const blogPosts = data.allContentfulBlogPost.edges;
  return (
    &amp;lt;Layout&amp;gt;
      &amp;lt;SEO title="Blog posts" /&amp;gt;
            &amp;lt;h1&amp;gt;{"Here's a list of all blogposts!"}&amp;lt;/h1&amp;gt;
      &amp;lt;div className="blogposts"&amp;gt;
        {blogPosts.map(({ node: post }) =&amp;gt; (
          &amp;lt;div key={post.id}&amp;gt;
            &amp;lt;Link to={`/blogpost/${post.slug}`}&amp;gt;{post.title}&amp;lt;/Link&amp;gt;
          &amp;lt;/div&amp;gt;
        ))}
        &amp;lt;span className="mgBtm__24" /&amp;gt;
        &amp;lt;Link to="/"&amp;gt;Go back to the homepage&amp;lt;/Link&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/Layout&amp;gt;
  );
};

export default BlogPosts;

export const query = graphql`
  query BlogPostsPageQuery {
    allContentfulBlogPost(limit: 1000) {
      edges {
        node {
          id
          title
          slug
          body {
            body
          }
          image {
            file {
              url
            }
          }
          tags
        }
      }
    }
  }
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pattern should be familiar now. &lt;/p&gt;

&lt;p&gt;At the bottom of the page, we export a GraphQL query. The query is the same as the one we used in &lt;code&gt;gatsby-nod.js&lt;/code&gt; to generate the blogpost pages. It pulls all the Contentful data of the &lt;strong&gt;BlogPost&lt;/strong&gt; type. Gatsby makes the result of the query available to us in the data object just as with the individual blogpost page. For this page though, we only need the &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;slug&lt;/code&gt; and &lt;code&gt;tags&lt;/code&gt; fields. &lt;/p&gt;

&lt;p&gt;At this point, let's add some very basic styling. For the sake of this example, we'll just add a few lines to the end of the &lt;code&gt;layout.css&lt;/code&gt; file, but in a real-world project you'd probably not want to do this. Go with whatever method you are comfortable with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Add these lines to the end of the layout.css file */
@import url("https://fonts.googleapis.com/css?family=Open+Sans:300,400,600");
html {
  font-family: "Open Sans";
}

header {
  /* We use !important here to override
  the inline styles just for this example.
  in production code, avoid using it where
  possible*/
  background-color: cadetblue !important;
}

header div {
  text-align: center;
}

header div h1 {
  font-weight: 600;
}

.home {
  text-align: center;
}

.home img {
  margin: auto;
}

.blogpost {
  font-size: 18px;
  width: 35em;
}

h1 {
  font-weight: 400;
  margin-top: 48px;
  font-family: "Open Sans";
}

img {
  margin-bottom: 8px;
}

.tags {
  margin-bottom: 24px;
}

.tags span.tag {
  font-weight: bold;
  margin-right: 8px;
  background: cadetblue;
  padding: 2px 12px;
  border-radius: 4px;
  color: white;
  font-size: 12px;
}

.blogpost p.body-text {
  margin-bottom: 32px;
}

p {
  line-height: 1.8;
  color: #929191;
  font-weight: 300;
}

.blogpost a {
  display: block;
  margin-bottom: 8px;
}

.blogposts a {
  display: block;
  margin-bottom: 8px;
}

footer {
  margin-top: 120px;
}

.mgBtm__24 {
  display: inline-block;
  margin-bottom: 24px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have our blog, the next step is to deploy it and make it available for all the world to see. With Netlify this is super easy. Netlify integrates really well with GitHub. In your terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $ git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to your GitHub and create a new repo called &lt;code&gt;gatsby-contentful-blog-starter&lt;/code&gt;, then follow the commands for pushing to an existing repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    $ git add .
    $ git commit -m 'initial commit'
    $ git remote add origin git@github.com:YOUR_GITUHB_USERNAME/gatsby-contentful-blog-starter.git
    $ git push -u origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With your code pushed to GitHub, head over to &lt;a href="https://www.netlify.com/" rel="noopener noreferrer"&gt;Netlify&lt;/a&gt; and create an account. In your dashboard click on'&lt;strong&gt;New site from Git&lt;/strong&gt;, select &lt;strong&gt;GitHub&lt;/strong&gt; as a provider and go through the authorization process selecting whatever options you feel comfortable with. &lt;/p&gt;

&lt;p&gt;Next, select a repo from the list provided. if you can't find the repo we just created, select &lt;strong&gt;Configure the Netlify app on GitHub&lt;/strong&gt;. This will open a popup allowing you to choose the repo you want to authorize for use with Netlify. Follow the prompts till you find the repo. After selecting our blog project, you'll be redirected to the Netlify deploy screen and you should now be able to select the &lt;code&gt;gatsby-contentful-blog-starter&lt;/code&gt; repo. As you can see, Netlify knows how to build Gatsby sites so you can just click the &lt;strong&gt;Deploy Site&lt;/strong&gt; button at the end of the page.&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%2F999w21cmcv5asbecktq0.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%2Fuploads%2Farticles%2F999w21cmcv5asbecktq0.png"&gt;&lt;/a&gt;&lt;br&gt;
Netlify runs Gatsby sites really easy with minimal configuration. Your new site should be ready in a few minutes.&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%2Fro8qujsajdugxg520a96.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%2Fuploads%2Farticles%2Fro8qujsajdugxg520a96.png"&gt;&lt;/a&gt;&lt;br&gt;
Remember how we had to kill the server and restart it to fetch new data? Well we don't want to have to trigger a redeploy manually every time someone adds or changes content in Contenful. The solution to that is to use Contentful's hooks to trigger an automatic Netlify redeploy of our site (Yup, this triple tag-team thought of everything).&lt;/p&gt;

&lt;p&gt;This means that new pages will get added to your blog automatically for each new blogpost you add. Also, if you're using the Gatsby sitemap plugin, the new pages will be included in the sitemap when it get regenerated on deployment, making it much easier to rank for keywords and help your SEO efforts with minimal fuss.&lt;/p&gt;

&lt;p&gt;On Netify click on &lt;strong&gt;Site Settings&lt;/strong&gt; and then in the left menu, select &lt;strong&gt;Build &amp;amp; Deploy&lt;/strong&gt;. Look for the &lt;strong&gt;Add build hook&lt;/strong&gt; button and click on it, giving the build hook a name (I am using "&lt;strong&gt;contentful&lt;/strong&gt;") and then click on &lt;strong&gt;Save&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now copy the &lt;strong&gt;buildhook&lt;/strong&gt; url and go back to your Contentful dashboard. Hit the settings dropwdown and select &lt;strong&gt;Webhooks&lt;/strong&gt;. The Webhooks screen already has a template for Netlify in the bottom right. Click on this.&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%2Fbyt5tbdpl6nyotsxrbqj.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%2Fuploads%2Farticles%2Fbyt5tbdpl6nyotsxrbqj.png"&gt;&lt;/a&gt;&lt;br&gt;
In the form that appears, add the Netlify build hook url and click &lt;strong&gt;Create Webhook&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now go back to the &lt;strong&gt;Content&lt;/strong&gt; page and add a new blog post. As soon as you hit publish, Contentful will make an API call to the build hook you provided. This will in turn, cause Netlify to redeploy your site. Gatsby will pull in the Contentful data all over again, which now includes the new post you added, and create a new page based on the new blog post.&lt;/p&gt;

&lt;p&gt;And that's it! It's been quite a journey, but now we have a working blog using three awesome tools that work so well together. From here you can add more content types and pages, expand the site or start a new project from scratch. Happy hacking!&lt;/p&gt;

&lt;p&gt;P.S: I know this was quite a long one, and I'm glad to answer any questions you may have if you get stuck. If you do, leave me a line in the comments below or hit me up on Twitter at &lt;a href="https://twitter.com/thebabscraig" rel="noopener noreferrer"&gt;@thebabscraig&lt;/a&gt;, I'm always happy to learn together. I'm also looking to connect with other developers on Instagram, so also hit me up at &lt;a href="https://www.instagram.com/thebabscraig" rel="noopener noreferrer"&gt;@thebabscraig&lt;/a&gt; there too!&lt;/p&gt;

</description>
      <category>react</category>
      <category>gatsby</category>
      <category>frontenddevelopment</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
