<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Charles Ouellet on Medium]]></title>
        <description><![CDATA[Stories by Charles Ouellet on Medium]]></description>
        <link>https://medium.com/@charlesouellet?source=rss-1a55c912c9e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zJyLyNCDTwDPSQpfsJo3sA.jpeg</url>
            <title>Stories by Charles Ouellet on Medium</title>
            <link>https://medium.com/@charlesouellet?source=rss-1a55c912c9e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 10:15:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@charlesouellet/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Vue Render Functions (w/ Examples)]]></title>
            <link>https://medium.com/@charlesouellet/introduction-to-vue-render-functions-w-examples-1255782ff099?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1255782ff099</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Mon, 15 Jul 2019 13:09:35 GMT</pubDate>
            <atom:updated>2021-03-16T18:16:08.659Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qpFbJ2FCTPkz1Nks.jpeg" /></figure><p>Lately, I’ve been reading Dr. Harari’s book “Sapiens.” If you haven’t read it yet, you should.</p><p>It’s killer.</p><p>Plus, it makes you appreciate our specie’s progress through the creative use of technology. And just like our ancestors’ living 10,000 years ago, we’re constantly using new tools to advance and evolve (we’ve just come very far from sticks and stones). But the only way to make sure you’re using a tool as efficiently as possible is to first understand exactly how it works.</p><p>So, today, for us digital sapiens, I want to dive into Vue render functions so that you can have a better understanding of <a href="https://snipcart.com/blog/vue-js-seo-prerender-example">Vue.js</a> as a whole.</p><p>More specifically, I’m going to:</p><p>And as you’ve probably come to expect, we’ll provide plenty of examples along the way :)</p><p>Let’s dive in!</p><h3>What is a Vue render function?</h3><p>You may or may not have heard the term “render functions” before depending on which frameworks you’re used to tooling around with. Render functions are something specifically related to Vue.js. Though some other frameworks, like <a href="https://snipcart.com/blog/react-ecommerce-gatsby-tutorial">React</a>, use the same wording, they’re not technically using “Vue render functions”.</p><p>With that short disclaimer out of the way, let’s get down to business. What are they?</p><p>Each Vue component implements a render function. Most of the time, the function will be created by the Vue compiler. When you specify a template on your component, the content of this template will be processed by the Vue compiler that will return a render function. The render function essentially returns a virtual DOM node which will be rendered by Vue in your browser DOM.</p><p>Ok, so that’s good and all, but now you may be wondering, “what in the hell is a ‘virtual DOM?’”</p><p>A virtual Document Object Model (or “DOM”) allows Vue to render your component in its memory before updating your browser. This makes everything faster because, as you can already tell, there are fewer interactions with the browser.</p><p>When Vue updates your browser DOM, it compares the updated virtual DOM to the previous one and updates the real DOM <em>with only the parts that have been modified.</em> This means that fewer elements change, thereby improving performance. A render function returns a virtual DOM node, commonly named VNode in the Vue ecosystem, which is an interface that allows Vue to write these objects in your browser DOM. They contain all the information necessary to work with Vue. The next version of Vue will include a whole new virtual DOM implementation that will be even faster than it is at the moment. React, Riot, Inferno and many others also use the concept of Virtual DOM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9TA-2oUk9ROE9Zya.jpg" /><figcaption>This image was found and modified from Michael Gallagher’s <a href="https://codeburst.io/low-verbosity-i18n-with-custom-vue-directives-c303a35c40c7">article on Low Verbosity i18n</a></figcaption></figure><blockquote>React has a created a great <a href="https://reactjs.org/docs/faq-internals.html">explanation of virtual DOMs</a> in their documentation if you’re still not 100% clear on the topic.</blockquote><p>You can implement a Vue render function in any Vue component. Also, given Vue reactivity, the render function will be called again whenever a reactive property of the component gets updated. Here’s a quick example on how you could render a h1 tag directly from a component render function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8252a963f709d262c55d87235bcd3c5/href">https://medium.com/media/b8252a963f709d262c55d87235bcd3c5/href</a></iframe><p><a href="https://codepen.io/couellet/pen/pBmPmV"><em>View on Codepen</em></a></p><p>There are some built-in components that leverage the power of the render functions such as transition and keep-alive. These components manipulate the VNodes directly in the render function. Something that wouldn&#39;t be possible without this feature which Vue provides.</p><blockquote>You can also check out Vue’s official (and awesome) documentation <a href="https://vuejs.org/v2/guide/render-function.html">here</a>.</blockquote><h3>How do Vue compilers fit in with render functions?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*09p5Hfh4_jCZYRVr.jpg" /></figure><p>Most of the time, the Vue render function will be compiled by the Vue compiler during the build of your project (with <a href="https://snipcart.com/blog/how-to-use-webpack-for-killer-refactoring">Webpack</a>, for instance). So, the compiler doesn’t end up in your production code, saving you a couple of bytes. This is why when you use Single File Components, you don’t really need to deal with render functions unless you really need/want to.</p><p>However, if you’d like to use the compiler in your code, you can use the full build of Vue which comes with the compiler. This can be quite handy. In fact, we’re using it internally here at Snipcart in the new version of the cart (the v3.0) coming out very soon. In short, we’re using the Vue compiler to compile custom templates that developers can inject into the cart, giving them more control over the checkout experience than they had before.</p><p>We wrote a component that implements a custom render function which fetches the template created by the customer and replaces our default template. And who knows? We might write something about this after the release! ;)</p><p>Ok, so stepping aside from our shameless plug, here’s a quick example of how you can use the compiler to compile a template string into a render function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cbd39f4eb7cbb7c265f5412674e9d46/href">https://medium.com/media/4cbd39f4eb7cbb7c265f5412674e9d46/href</a></iframe><p><a href="https://codepen.io/couellet/pen/ZZNXzy"><em>View on Codepen</em></a></p><p>As you can see, the compiler returns an object which contains the render function ready-to-use.</p><h3>The importance of event binding in Vue render functions</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/0*oc7FPCC1UcB5wBgS.jpg" /></figure><p>That brings us to event binding. The createElement function can receive a parameter called the data object. This object can have multiple properties that are the equivalent to the directives like v-bind:on which you use in your standard templates. Here&#39;s an example of a simple counter component with a button that increases the click count.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/089186d29e5801071475f5bb7d280748/href">https://medium.com/media/089186d29e5801071475f5bb7d280748/href</a></iframe><p><a href="https://codepen.io/couellet/pen/VNOMov?editors=1010"><em>View on Codepen</em></a></p><p>But the data object isn’t limited to event binding! You can also apply classes to the element as you’d do with the v-bind:class directive.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21c14804c5586ed250230b3993f1eda7/href">https://medium.com/media/21c14804c5586ed250230b3993f1eda7/href</a></iframe><blockquote>You’ll find more information about the data object in this section of <a href="https://vuejs.org/v2/guide/render-function.html#The-Data-Object-In-Depth">Vue’s documentation</a>.</blockquote><p><a href="https://codepen.io/couellet/pen/YMbrvz"><em>View on Codepen</em></a></p><h3>Real-life use case of template overriding</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*y0xj8-asKdY9pq8G.gif" /></figure><p>I think it’s very interesting to understand how Vue works under the hood. Like I said earlier, the only way to know if you’re using a tool the most efficiently is to know exactly how it works.</p><p>That’s not to say that you should start converting all your templates to render functions (you shouldn’t) but, sometimes they can come in handy so you should at least know how to use them.</p><p>With the example above, I’ll show you how we used a custom render function in a component which allows some of our components to be overridable. This is something that we had to implement in the next version of our cart. Just as a quick heads up, I tried to streamline what we did as much as possible for this example.</p><p>First, let’s create our initial markup.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e96f4803fcb4290500b3dd550bc8576b/href">https://medium.com/media/e96f4803fcb4290500b3dd550bc8576b/href</a></iframe><p>Inside our div where the Vue app will mount, we&#39;ll define a custom template. Basically, we want the template to override the default version of the heading component we&#39;ll create.</p><p>The first thing we’ll do is scan through the custom templates and precompile them with the Vue compiler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91076752b732442733696ad63f9f909d/href">https://medium.com/media/91076752b732442733696ad63f9f909d/href</a></iframe><p>Then, let’s create the heading component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4496a8ee9244d1f3a0248fe44fd492dc/href">https://medium.com/media/4496a8ee9244d1f3a0248fe44fd492dc/href</a></iframe><p>Now, it’s just a simple component that has one props named title. The default template will render a h1tag with the title in it. We&#39;ll wrap this component with the overridable component that we&#39;ll then create.</p><p>This is where we’ll use a custom render function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09c9bc6ed25402e2765f0f203357e948/href">https://medium.com/media/09c9bc6ed25402e2765f0f203357e948/href</a></iframe><p>Then, let’s mount our Vue app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a0a8e0964f09f97047bf930f3abbdb0/href">https://medium.com/media/0a0a8e0964f09f97047bf930f3abbdb0/href</a></iframe><p>We can see in this example, that the default template is used, so it’s a standard h1 tag.</p><p><em>See the Pen </em><a href="https://codepen.io/couellet/pen/OGYEep/"><em>OGYEep</em></a><em> by Charles Ouellet (</em><a href="https://codepen.io/couellet"><em>@couellet</em></a><em>) on </em><a href="https://codepen.io/"><em>CodePen</em></a><em>.</em></p><p>If we add the custom template inside the div#app then we&#39;ll see that the heading component will now render the custom template we specified.</p><p><em>See the Pen </em><a href="https://codepen.io/couellet/pen/MRdBBd/"><em>MRdBBd</em></a><em> by Charles Ouellet (</em><a href="https://codepen.io/couellet"><em>@couellet</em></a><em>) on </em><a href="https://codepen.io/"><em>CodePen</em></a><em>.</em></p><h3>Conclusion</h3><p>Overall, it’s been fun playing with render functions and has come in handy with our v3.0. Vue render functions are a fundamental piece of Vue itself, so I really think it’s valuable to take some time and thoroughly understand the concept (especially if your regularly using the framework). And as <a href="https://snipcart.com/blog/vuejs-debugging">Vue.js</a> evolves and becomes more efficient, the knowledge you’ll build by knowing what’s under the hood will help you evolve right along with it.</p><p>In other words, understanding Vue render functions is one small-but important-step to your digital evolution. :)</p><p><em>Any questions? Feel free to hit comments to give us your thoughts, feedback, and questions. If you enjoyed this post, take a second to 👏 or share on Twitter!</em></p><p><em>Originally published on the </em><a href="https://snipcart.com/blog/vue-render-functions"><em>Snipcart blog</em></a><em> and in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1255782ff099" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why headless CMS and GraphQL are a natural fit]]></title>
            <link>https://medium.com/free-code-camp/why-headless-cms-and-graphql-are-a-natural-fit-4f4e22e4815c?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f4e22e4815c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[headless-cms]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Thu, 14 Mar 2019 17:04:58 GMT</pubDate>
            <atom:updated>2019-03-14T17:04:58.958Z</atom:updated>
            <content:encoded><![CDATA[<h4>Bonus: Takeshape Demo</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*HffX47JLsY3x1EG82epLSQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/M4lve6jR26E?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Gabrielle Henderson</a> on <a href="https://unsplash.com/search/photos/gratitude?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The first time we wrote about GraphQL, back in 2017, <a href="https://snipcart.com/blog/graphql-nodejs-express-tutorial">we described it</a> as a shiny new tool developed by Facebook.</p><p>GraphCMS, which we presented in that article, was one of the first softwares to use it as a core feature.</p><p>It was just another drop in a sea of development tools. No biggie, right?</p><p>Not quite.</p><p><strong>I think we can all agree that GraphQL is now integral to the modern web ecosystem.</strong></p><p><em>Proof #1</em>: the number of static site generators and headless CMSs making it a prime built-in feature has been skyrocketing in the last few months.</p><p>One of them being <a href="https://www.takeshape.io/">TakeShape.io</a>, which I’ll explore further in a technical demo at the end of this post.</p><p>First, I want to take a look at:</p><ul><li>The state of GraphQL in 2019</li><li>The benefits it brings to headless CMSs</li><li>The tools that feature GraphQL</li><li>An introduction to TakeShape.io</li></ul><p>Let’s begin by wrapping our heads around the concepts behind GraphQL.</p><h3>1. Getting up-to-date with GraphQL</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WBMasZ7lhCWbq_Gm.png" /></figure><p>You may be reading without even knowing what GraphQL really is. For clarity’s sake, <strong>it’s a query language for APIs that defines how to fetch data from one or many databases</strong>.</p><blockquote>For a proper technical GraphQL introduction I suggest <a href="https://snipcart.com/blog/graphql-nodejs-express-tutorial">our first post</a> on the matter.</blockquote><p>A lot has changed since its first use case with FB mobile apps back in 2012, mostly regarding its adoption. GraphQL was open-sourced in 2015, giving it an initial burst of life for a larger number of devs. 4 years later, its rising presence in our industry is undeniable.</p><p><em>Proof #2 that GraphQL is an integral part of web development</em>: its adoption by tech giants. Apart from Facebook, other major players like GitHub, Shopify, Pinterest, Paypal &amp; Twitter all use GraphQL in some way or another.</p><p>Impressively enough, it fits directly in <a href="https://nordicapis.com/is-graphql-moving-toward-ubiquity/">the adoption timeline</a> that Lee Byron, GraphQL’s creator, had in mind a few years ago. The next step according to him? Omnipresence in web platforms. We have every reason to believe him.</p><p>Prepare to hear about GraphQL a lot in 2019. Many publications rank it as one of the most important <a href="https://hackernoon.com/which-top-javascript-trends-to-look-in-2019-528072bf0082">JS trends to keep an eye on</a> in the next few months. Npm itself says that <a href="https://blog.graphqleditor.com/javascript-predictions-for-2019-by-npm/">you’ll need to learn it</a> in 2019, &amp; GraphQL experts are already drooling thinking about <a href="https://medium.com/graphqlconf/top-5-graphql-predictions-for-2019-6f281f16fac1">the bright future</a> of the tech.</p><p>But… wait a second here.</p><p>Hype is one thing, but one of my colleagues said something about it that I found pretty interesting:</p><p>It’s true that if intensive public talk can sometimes oversell something (that Birdbox movie, anyone?) it can also overshadow its initial merits.</p><p>For us, these pillars for GraphQL are:</p><ol><li>The GraphQL specification has been developed as a way to define single highly flexible API endpoints giving developers access to complete relational models.</li><li>It is incredibly powerful because it gives consumers total control over how they want the requested data to be structured, as well as how deep they want to go into entity relations.</li></ol><blockquote>In the same vein, a GitHub engineer explains why he can’t wait for GraphQL to be “uncool” in <a href="https://www.heavybit.com/library/podcasts/jamstack-radio/ep-5-graphql-at-github/">this episode of JAMstack Radio</a>.</blockquote><p><strong>What about that old GraphQL vs. REST talk?</strong></p><p>I won’t lose myself too long here because it’s also a topic we discussed in our first GraphQL piece and the debate hasn’t really changed.</p><p>Of course, the rising GraphQL adoption is forcing us to keep this discussion going, but REST APIs are still alive &amp; well and not under any threat of extinction, for now. You can’t put them aside and say that one is the other’s predecessor because they’re simply not of the same nature. They can live together just fine in the same environment.</p><p>Still, GraphQL comes as an upgrade for certain functions that weren’t optimal with REST APIs.</p><h3>2. How headless CMSs are leveraging GraphQL</h3><p>If you’ve paid attention in the last few months, you might have observed a lot of tools adding GraphQL as a built-in feature, or new ones breaking into the market with GraphQL as a selling point.</p><p>This list includes:</p><ul><li>Gatsby (SSG) — The popular React-powered static site generator <a href="https://snipcart.com/blog/react-graphql-grav-cms-headless-tutorial">leverages GraphQL</a> to fetch data from multiple sources.</li><li>Gridsome (SSG) — The Vue.js equivalent to Gatsby, also <a href="https://snipcart.com/blog/vuejs-graphql-airtable-example">featuring GraphQL</a>.</li><li>GraphCMS — The name says it all. It was probably the first headless CMS to put GraphQL forward.</li><li>Strapi — This neat Node.js headless CMS lets you use REST or GraphQL APIs.</li><li>Unite CMS — Still in beta, this new headless CMS is built on Symfony 4 &amp; GraphQL.</li><li>CannerCMS — An open-source API-driven headless CMS.</li><li>TakeShape — More on this new headless CMS down below.</li></ul><p><strong>In this post, I want to focus solely on the headless CMSs in this list. More precisely, how GraphQL is making them better and worthy of your attention.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*y8PTk5eyC6dv4wH-.gif" /></figure><p>There are 4 main advantages that I think it’s important to highlight:</p><p><strong>→ Better API to retrieve data</strong>: The main premise of GraphQL is to offer the capacity for devs to chose exactly what they need from the database and receive that data in a predictable way. Not only that, but you can fetch many resources in a single request. Say goodbye to over bloated server/database requests.</p><p><strong>→ Possibility to insert data from external applications</strong>: Isn’t it what APIs have always been used for? Sure, but once again GraphQL makes this process more structured and organized. One great thing that headless CMSs enable is modular development. So, when scaling an app you’ll need to plug some external functionalities and GraphQL assures that you’ll only get what you really need from each of them.</p><p><strong>→ Great performances</strong>: Smaller, more efficient requests to databases and external apps will no doubt be beneficial for your system’s performance.</p><p><strong>→ Easy delivery to multiple channels</strong>: GraphQL is embracing modern trends. With the rise of mobile, wearable technologies &amp; IoT it’s becoming important to develop APIs that can easily serve multiple channels.</p><p>It’s time to move from theory to practice, with TakeShape.io as our guinea pig.</p><h3>3. What is TakeShape.io?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p_6wb-5LOGvBlnUG.png" /></figure><p><strong>TakeShape is a new </strong><a href="https://snipcart.com/blog/jamstack"><strong>JAMstack-oriented</strong></a><strong> tool that combines the functionalities of a headless CMS, GraphQL API and static site generator.</strong></p><p>It’s only a few months old since it was officially launched in September 2018. It caught our attention thanks to their <a href="https://www.takeshape.io/articles/why-the-best-websites-are-static/">JAMstack-related content</a> and we’ve been eager to try it ever since.</p><p>It was built with the premise that “content management shouldn’t be hard”. TakeShape was crafted with developers in mind, giving them a frictionless framework to create projects on. But content crafters won’t be left in the dark, thanks to an intuitive interface and the necessary features to manage content efficiently.</p><p>It fully embraces the latest modern development trends, making the best of the built-in GraphQL API and even offering its own static site generator. But, don’t worry, as with every good headless CMS, you can also attach to it the frontend tech of your choice.</p><p>The founders also seem to be all-around good guys from the small discussion we’ve had with them prior to building this demo, so I’m really happy to finally dive into it!</p><h3>4. Technical tutorial: Building an app with headless CMS &amp; GraphQL</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8p2YS428tD7vLIxn.png" /></figure><h4><strong>Context</strong></h4><p>The idea behind this demo comes from a therapeutic practice that emphasizes the importance to list things you’re grateful for, every day. I’ll build a small app that will allow users to send entries to what I’ll call our <strong>Gratitude Journal</strong>. I’ll then use GraphQL to fetch the data submitted in the form and publish to a website that will showcase these entries.</p><h4><strong>Prerequisites:</strong></h4><ul><li>A <a href="https://app.takeshape.io/signup">TakeShape account</a> (for free)</li><li>A <a href="https://app.netlify.com/signup?_ga=2.139846771.1148780123.1549400286-251304755.1546976271">Netlify account</a> (also for free)</li></ul><p>Basically, I’m going to use TakeShape to build the website and store the data. I’ll then use Netlify functions to add entries to the GraphQL database with TakeShape API. Netlify functions will act as a kind of proxy to make sure the API keys aren’t exposed to the wild and that the entry I’ll insert into my database is in the right state.</p><p>Let’s do this!</p><h3>4.1 Setting up TakeShape</h3><p>First things first, you need to setup your TakeShape project.</p><p>Once your account is created and you’re logged in, you’ll be ready to create your first <em>content type</em>. A content type in TakeShape is the representation of any object or document in your application.</p><p>For example, if you were building a classic e-commerce project, you could have a content type for your products, another content type for the product categories and so on. You can then very easily define relationships between your entries.</p><blockquote>I suggest watching the introduction video <a href="https://www.takeshape.io/docs/quickstart/">here</a>. It lasts about 20 minutes and you should get everything you need to know to get started with TakeShape.</blockquote><p>You’ll end up with a GraphQL database that can be queried directly into the API explorer in the TakeShape app or via their API endpoint. This is a very easy way to get started with GraphQL. For this case, create two content types.</p><p><strong>The first one</strong> will be for the messages people can submit on the site. Let’s call this content type: Entry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/0*Ep78o_eIqDq5uF54.png" /></figure><p>You can define your fields by drag-and-dropping the different controls available in the right side panel in TakeShape app. In this case, use a single line to represent the message. <em>Please note that I used Multiple as Annotation value so we can create more than one entry</em>.</p><p>Create the <strong>second content type</strong>, which will be Home. This will be useful since you want your marketing team to have control over the wording on the homepage. This is where they’ll edit the content. This is going to be a <em>Single</em> entry since it can be added only once (there will be only one homepage).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/0*NCG_HvmVtIIcZjb0.png" /></figure><p>Easy, wasn’t it? Your CMS is ready now, you only need to build the website and the function that will add new entries to your GraphQL database.</p><p>Time to dive into the code!</p><h3>4.2 Creating the project</h3><p>The code for TakeShape templates and the upcoming Netlify function will reside in the same Git repository.</p><p>You’ll need to use TakeShape CLI tools to initialize the project. So you can install it via npm.</p><pre>npm install takeshape-cli -g</pre><p>Create a new folder, and in this folder start a new npm project:</p><pre>npm init</pre><p>Follow the steps to create your project as usual.</p><p>Then, you’ll need to initialize the TakeShape project:</p><pre>tsg init</pre><p>Follow the steps by entering your TakeShape credentials and, you’ll be prompted to answer a couple of questions. Their CLI is well made so this shouldn’t be complicated.</p><p>You should now have a couple of files in your folder. As suggested by TakeShape CLI, write a .gitignore file in the root of the folder and add some files to ignore in there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df9a5c0c4323f89cb3cbfc2e7d7f6557/href">https://medium.com/media/df9a5c0c4323f89cb3cbfc2e7d7f6557/href</a></iframe><p>Then, you can initialize a new Git repository:</p><pre>git init</pre><h3>4.3 Adding function for the GraphQL database</h3><p>It’s now time to generate the Netlify function that will be responsible for submitting new entries to TakeShape.</p><p>As mentioned earlier, one of the cool things about a GraphQL-powered CMS is that inserting new data is as simple as querying. This isn’t something you’ll often see in demos like this. Most of the time you’ll just gather the data with some queries and display it. In this case, you’ll use GraphQL mutations to create new entries.</p><p>Let’s start by adding a few dependencies again. You’ll need to install netlify-lambda npm package which will be used during the build process. This is the CLI tool created by Netlify to compile and test functions locally:</p><pre>npm install netlify-lambda --save-dev</pre><p>Also, install node-fetch to interact with TakeShape API:</p><pre>npm install node-fetch --save</pre><p>Create a new file at the root of your project named <em>netlify.toml</em>. That’s where you’ll specify where the functions will be located in the project. The Netlify build process will use this file to deploy everything correctly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/054ae1b0f80949968c5a01132ab2a0d7/href">https://medium.com/media/054ae1b0f80949968c5a01132ab2a0d7/href</a></iframe><p>The functions will need to be compiled into the build/functions folder. Otherwise, Netlify won’t see them and they won’t be available. You’ll need to use netlify-lambda CLI tool to compile your function. The build function is going to be helpful here, this function awaits one parameter that is the path to the <strong>uncompiled</strong> functions; your source code.</p><p>Open your package.json file and add a new script named build.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/856781e88ecdf31d32e2f243665dc6d7/href">https://medium.com/media/856781e88ecdf31d32e2f243665dc6d7/href</a></iframe><p>This is what you’re going to use in Netlify as the build command.</p><p>You’ll need two standalone Netlify sites for this demo. One that will be responsible to host your functions (this is going to be an API, in a way) and the other will be the output from TakeShape, the public website. I’ll come back to this later.</p><p>For now, create your function. Create a new folder: src/functions at the root of your project. Then create a file named add-entry.js in it.</p><p>You’ll need to run a mutation in this function using TakeShape API. It’ll look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97bec59ddb339b19f6cbce1ed0d63351/href">https://medium.com/media/97bec59ddb339b19f6cbce1ed0d63351/href</a></iframe><p>Basically, you’ll write a new entry in TakeShape’s GraphQL datasource that isn’t enabled by default. Your site administrators will therefore be able to review the messages before publishing them on the public website.</p><p>Here’s the code for the function itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c99293495b268a0132b10bb2e5f05cf6/href">https://medium.com/media/c99293495b268a0132b10bb2e5f05cf6/href</a></iframe><p>Later on, you’ll need to configure some environment variables on Netlify’s side to store TakeShape’s API key and Project ID so we don’t commit them on GitHub. This API key would allow anyone to insert or delete data from your GraphQL database. You really don’t want this shared externally.</p><p>Now, commit these files. Using Github, create a <a href="https://github.com/snipcart/gratitude-journal">new repository</a> and push the project.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a095b3b51f0c3e929ba88b64ef3b78d/href">https://medium.com/media/8a095b3b51f0c3e929ba88b64ef3b78d/href</a></iframe><p>Then, open your Netlify account and create a new site from this repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/0*bKyZDwPeIGgGJQ8H.png" /></figure><p>In your site settings, go to <em>Build &amp; Deploy</em> and scroll down to the <em>Build environment variables</em> section.</p><p>You’ll need to add two environment variables: TAKESHAPE_API_KEY and TAKESHAPE_PROJECT_ID.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ivNoCCr0CxDa3nwf.png" /></figure><p>Now, in your TakeShape dashboard, you’ll need to craft a new API key with <em>Read/Write</em> permissions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/0*MSCnSTMFj09HuUFD.png" /></figure><p><strong>Make sure to note the API key value since you won’t be able to view it anymore</strong>. Use this value to specify the TAKESHAPE_API_KEY environment variable in Netlify’s dashboard.</p><p>To get your project ID, look at the URL in TakeShape’s dashboard (it’s the guid in it).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/0*An8HlLGfNG02AXel.png" /></figure><p>With this, your function should now be working as expected and you should be able to submit new entries to TakeShape CMS. You might need to redeploy your site in Netlify, though, to make sure your function is aware of new environment variables.</p><p>To test it, you can use an HTTP client such as Postman or Insomnia.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/0*kC7SWYuJaPEBPN47.png" /></figure><h3>4.4 Building the site</h3><p>Now that your API is ready, you can build your website. Awesome.</p><p>When I started playing around TakeShape in preparation for this demo, I wasn’t even aware that they had their own <a href="https://www.takeshape.io/docs/project-structure/">static site generation</a> feature. That’s really cool in my opinion. It’s solid and enables a quick start for small projects, so that’s what I’m using here.</p><p>Make sure that the project you were working on is still open in your favorite code editor. Next thing to do is building the TakeShape config file. At the root of your project, create a new file named tsg.yml. This is where you’ll create your routes and map the templates/queries to them.</p><p>Here’s what’s the config looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/993806beaf672daa6fb81c3db5e13daa/href">https://medium.com/media/993806beaf672daa6fb81c3db5e13daa/href</a></iframe><p>What does it mean? You’ll have a single route, the homepage, that will render the homepage.html template and use the data coming from the homepage.graphql query.</p><p>Each page specified in the routes array consist of three elements:</p><ul><li>The path, which is the URL of the page</li><li>The template, which will be the view itself, the HTML markup</li><li>And the context, which will be the GraphQL query that will run and populate the view model.</li></ul><p>Once you start working with it a bit, you quickly realize it makes a lot of sense.</p><p>Also specified in there is the staticPath. This path is the folder where all your static assets, such as Javascript or CSS files, should reside.</p><p>Start by creating our query file. Generate a file named homepage.graphql in src/templates/data folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ceadbd2f4669cce7021d3897ceac8285/href">https://medium.com/media/ceadbd2f4669cce7021d3897ceac8285/href</a></iframe><p>Your query will return the list of entries and the homepage details. You’ll have access to all this information in your template file.</p><p>Let’s create a file named homepage.html in src/templates/pages folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da0748d35fcadf3c0a21da1c0262de9/href">https://medium.com/media/4da0748d35fcadf3c0a21da1c0262de9/href</a></iframe><p>Noticed the app.js file just before the closing body tag? This is where you’ll add the code that will interact with your Netlify function. Build this file in a new folder named static at the root of your project:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e30a6a0d9c09bd2612590217d22cce9/href">https://medium.com/media/3e30a6a0d9c09bd2612590217d22cce9/href</a></iframe><p>There’s also a file named site.css that is included in the header of the site. Create this file in the static folder and just like app.js (please refer to <a href="https://github.com/snipcart/gratitude-journal/blob/master/static/site.css">this file on Github</a>), this asset will be deployed and can be included it directly into your markup.</p><p>Essentially, you’re going to catch the form submit event, and make your request to the API. That’s pretty straightforward.</p><p>Now, you can test it locally using TakeShape tooling. In a command prompt, navigate to your project and use the watch task:</p><pre>tsg watch</pre><p>Your project will then be compiled into the build folder, which was configured in the tsg.yml file. If you start a web server on this path, you should be able to see your site.</p><h3>4.5 Deploying the website</h3><p>Lastly, you need to deploy the site. TakeShape has plenty of deployment options. In this case, I’ll stick to Netlify since it’s easy to use and I’m already relying on their platform for the API.</p><p>Open your project settings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/0*oiFtkJlX-p_g7CKE.png" /></figure><p>Then, you’ll see the Netlify in the <em>Integrations</em> panel. Connect your Netlify account.</p><p>Next, you’ll need to create a <em>Static site</em> in TakeShape:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/0*T_u2CS771quWGyKZ.png" /></figure><p>Fill the form by selecting Netlify as provider. You’ll be able to generate a new Netlify site directly from this form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RalCriOfzW94q4xb.png" /></figure><p>Now, the deployment.</p><p>It’s very simple using TakeShape CLI. Personally, I like to deploy my sites using the site ID. You can find it in your browser URL when you’re in the edit form of the static site:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/0*YcyUNIkyMwqvK3Vt.png" /></figure><p>Then, in your terminal:</p><pre>tsg deploy --site &lt;site_id&gt;</pre><p>Bing! Your website should now be live, and the API should work as expected.</p><h3>Live demo &amp; GitHub repo</h3><p>In case you were wondering how to manually monitor and accept the messages submitted in the form by users, this GIF shows you the way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YnEEMD2IZR0nm5A4.gif" /></figure><blockquote>See the live demo <a href="https://gratitudejournal.netlify.com/">here</a></blockquote><blockquote>See the GitHub repo <a href="https://github.com/snipcart/gratitude-journal">here</a></blockquote><h3>5. Closing thoughts</h3><p><em>Proof</em> <em>#3 that GraphQL is now integral to web development:</em> It’s awesome and integrates so well into modern dev workflows. It’s hard not going back to it once you’ve tasted it.</p><p>I had a blast using TakeShape and Netlify functions. These tools make it possible to create full-blown applications without having to deal with servers and infrastructure. You know that’s what we’re all about.</p><p>The TakeShape team really did a solid job with their application. Their dashboard is clear and simple, their API is exactly how we expect it to be and building relationships between content types is totally straightforward.</p><p>I spent more or less a day putting everything together. I didn’t hit any major hiccups and it was a pretty smooth development ride all around.</p><p>I didn’t feel the need to push anything further for this demo. It was meant to be pretty simple and to show how we can insert data into the CMS programmatically. I think this stack would be a great fit for a blog or any content-driven application.</p><p>I would also love to eventually use TakeShape for a straight-up e-commerce integration with Snipcart, so I’m saving this on the roadmap! ;)</p><p><em>Any questions? Feel free to hit comments to give us your thoughts, feedback, and questions. If you enjoyed this post, take a second to 👏 or share on Twitter!</em></p><p><em>Originally published on the </em><a href="https://snipcart.com/blog/headless-cms-graphql-takeshape-tutorial"><em>Snipcart blog</em></a><em> and in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f4e22e4815c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/why-headless-cms-and-graphql-are-a-natural-fit-4f4e22e4815c">Why headless CMS and GraphQL are a natural fit</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build an e-commerce shop with Python, Django, & Wagtail]]></title>
            <link>https://medium.com/free-code-camp/how-to-build-an-e-commerce-shop-with-python-django-wagtail-3dd2043f89e7?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dd2043f89e7</guid>
            <category><![CDATA[ecommerce]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Thu, 07 Feb 2019 17:00:36 GMT</pubDate>
            <atom:updated>2019-02-07T17:00:36.493Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JGDhEOqWVmzqofJ7xSS0KA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/D9Zow2REm8U?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Hitesh Choudhary</a> on <a href="https://unsplash.com/search/photos/python?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>“D-J-A-N-G-O. The D is silent.”</p><p>Man, I love that line. So badass.</p><p>But the eponymous character from Quentin Tarantino’s masterpiece isn’t the only badass <em>Django</em> in town.</p><p>So is the popular Python framework of the same name.</p><p>Today, I’m leaving the realm of <a href="https://snipcart.com/blog/react-seo-nextjs-tutorial">JavaScript</a> <a href="https://snipcart.com/blog/vuejs-ecommerce-headless-buttercms">frameworks</a> for a quick venture into <strong>Django e-commerce.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*fPqzRU6dS2rqcqJX.gif" /></figure><p>In this post, I’ll answer legitimate questions you might have when starting a new e-commerce project, such as:</p><blockquote><em>Is Python the right language for my project? And Django the right framework? Which tools or plugins should I use?</em></blockquote><p>Then, I’ll show you our homemade recipe for Django-powered e-commerce success with a step-by-step <strong>Wagtail CMS</strong> tutorial:</p><ul><li>Creating a new Wagtail site.</li><li>Adding Snipcart configuration settings.</li><li>Generating database migrations.</li><li>Creating new products for your Django store.</li><li>Crafting an e-commerce template.</li></ul><p>Let’s start with the basics.</p><h3>The State of Python</h3><p>One of the main reasons to pick Django as a framework is its <a href="https://www.python.org/"><strong>Python</strong></a> foundation.</p><p>A general purpose, dynamic programming language, Python was developed by ex-Googler Guido van Rossum in the late 80’s. A fan of Monthy Python, he took one-half of the name to baptize his programming project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/0*_qhVVhN3FmGXlRWE.gif" /></figure><p>He wasn’t joking though. To say that Python has become “popular” is an understatement.</p><p>Today, it’s used by hundreds of thousands of developers all over the world. <a href="https://stackoverflow.blog/2017/09/06/incredible-growth-python/">As StackOverflow puts it</a>:</p><blockquote><em>The term “fastest-growing” can be hard to define precisely, but we make the case that </em><strong><em>Python has a solid claim to being the fastest-growing major programming language.</em></strong></blockquote><p>A few reasons explain the Python love:</p><ul><li>Its grammatical readability is awesome.</li><li>It has a fast learning curve for newcomers.</li><li>It boasts a long-lasting, <a href="https://pypi.python.org/pypi">solid ecosystem of libraries</a> &amp; community</li><li>It’s now the standard language for <a href="https://jakevdp.github.io/PythonDataScienceHandbook/">data science</a> &amp; <a href="https://machinelearningmastery.com/machine-learning-in-python-step-by-step/">machine learning</a>.</li><li>It powers great dev tools like <a href="https://snipcart.com/blog/pelican-blog-tutorial-search-comments">Pelican</a>, a neat static blog generator.</li><li>Reddit is written in Python. ;)</li></ul><h3>What about the Django framework?</h3><p><a href="https://www.djangoproject.com/"><strong>Django</strong></a> is an open source, <strong>high-level Python web framework</strong>. Its emphasis on reusable components makes it faster for developers to build web apps on top of Python. It presents itself as a <em>web framework for perfectionists with deadlines.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LnFnW_A9EdQ_hAbv.png" /></figure><p>Now maintained by the Django Software Foundation, it was originally written by two brilliant Lawrence Journal-World developers. Oh, and while Python draws its name from comedy icons, Django got his from a versatile guitar legend: <a href="https://www.youtube.com/watch?v=aZ308aOOX04">Django Reinhardt</a>!</p><p>As a full-stack framework, it overshadows pretty much any alternative tool out there. It’s fast, fully loaded, secure, scalable &amp; versatile. All characteristics you’ll probably want to apply to your e-commerce setup!</p><h3>Why use Django for e-commerce?</h3><p>While you can do <strong>a lot</strong> with Django, let’s keep the focus on what it brings to e-commerce and the different tools available to put together an online store.</p><p>First, here are some Django features to consider if you’re looking for the right framework to build a shop.</p><p>→ <strong>Scalability</strong></p><p>Django is perfect for e-commerce startups, as it’s a good fit for small websites, but also has scales perfectly with business growth. You can rely on Django to handle hundreds/thousands of visitors at a time. It’s built with independent components you can unplug or replace depending on your needs at any specific time.</p><p>→ <strong>Security</strong></p><p>With e-commerce, you want to make sure merchants and clients alike feel safe through your shopping experience. Django prevents a whole lot of common security mistakes which often are what weakens traditional PHP CMSs. For instance, Django hides your site’s source code from direct viewing on the web by dynamically generating web pages.</p><p>→ <strong>Feature-rich</strong></p><p>Compared to most frameworks, Django comes with way more features out-of-the-box. It allows you to build an app right off the bat. Perfect for supporting your online store with functionalities such as user authentification, content management or RSS feed. If something seems to be missing, you can count on Django’s community and plugins ecosystem to extend your app!</p><p>→ <strong>SEO-friendly</strong></p><p>SEO is paramount for any online business. While other frameworks don’t natively play well with search engines (mainly JavaScript frameworks, like <a href="https://snipcart.com/blog/vue-js-seo-prerender-example">Vue</a> or <a href="https://snipcart.com/blog/react-seo-nextjs-tutorial">React</a>), at least Django advocates best practices for SEO. Human-readable URLs and sitemap features are sure to please any marketing team.</p><p>Oh and also, <strong>it’s fast</strong>. Which is always great for both customer experience and SEO.</p><p>→ <strong>Reliable</strong></p><p>It has been crowd-tested for a while now, and the community surrounding it is largely supportive. It’s continuously updated by active developers; maybe you’ll even find yourself <a href="https://github.com/django/django">contributing</a>. ;)</p><p>There are a few noteworthy e-commerce solutions in the Python/Django ecosystem:</p><ul><li><a href="http://oscarcommerce.com/"><strong>Oscar</strong></a> — Domain-driven e-commerce for Django, open-source.</li><li><a href="https://getsaleor.com/"><strong>Saleor</strong></a> — An e-commerce storefront written in Python, open-source.</li><li><a href="http://django-shop.readthedocs.io/en/latest/index.html"><strong>Django-SHOP</strong></a> — A Django based shop system.</li><li><a href="https://www.shuup.com/ca/"><strong>Shuup</strong></a> — A single and multi-vendor application.</li></ul><p>You can explore more options through this extensive list of the best <a href="https://djangopackages.org/grids/g/ecommerce/">e-commerce packages</a>.</p><p>Now let me present you another cool stack for a complete and custom e-commerce setup with Django.</p><h3>Wagtail CMS + Snipcart e-commerce setup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*z0oU1TFRNK07_YI0.png" /></figure><p><a href="https://wagtail.io/">Wagtail</a> is a developer-first Django content management system. Free and open source, it was developed by the good-hearted folks at <a href="https://torchbox.com/">Torchbox</a>. It’s elegant, flexible, and, IMHO, kicks ass.</p><p>In the following Wagtail tutorial, the CMS will be in charge of creating and managing products that users will then be able to buy through a shopping cart.</p><p>By the end of it, you’ll have a solid Django-powered e-commerce site up and running.</p><p>Let’s get practical!</p><h3>Django e-commerce tutorial with Wagtail CMS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XwMhC32zSk4y9zY7.png" /></figure><blockquote><em>To continue with the movie references and because we’re working in Python, I’ll craft a Slytherin demo shop! Let’s see how it goes.</em></blockquote><p><strong>Pre-requisites</strong></p><ul><li>A <a href="https://app.snipcart.com/register">Snipcart account</a> (forever free in test mode)</li><li>Basic knowledge of Python</li></ul><h3>1. Creating a new Wagtail e-commerce site</h3><p>Make sure you have Wagtail installed. If not, refer to their <a href="http://docs.wagtail.io/en/latest/getting_started/index.html">installation documentation</a>.</p><p>Open a terminal and launch a new Wagtail site:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b8a384e2332d893f344199cc087a0e8/href">https://medium.com/media/7b8a384e2332d893f344199cc087a0e8/href</a></iframe><p>We have an extra step to complete the Wagtail setup, and it’s to install the wagtail.contrib.settings plugin that we&#39;ll require later on.</p><p>In your new Wagtail project, open the base.py file located in settings folder. Then, add wagtail.contrib.settings to the INSTALLED_APPS array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3b956a39e1dd6c334a0345885397b94/href">https://medium.com/media/f3b956a39e1dd6c334a0345885397b94/href</a></iframe><h4>1.1 Models definition</h4><p>The first thing you need to do is create your Page models. Wagtail uses these Django models to generate a page type.</p><p>Open the models.py file located in the home folder of your product. This is where you&#39;ll define all your custom models.</p><p>Create two different models:</p><ul><li>Product: defines the product you&#39;re selling.</li><li>ProductCustomField: defines a single product custom field.</li></ul><p>Let’s begin by importing required modules:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36b36545df946a24eb62b4f67e225e27/href">https://medium.com/media/36b36545df946a24eb62b4f67e225e27/href</a></iframe><p>Now add the Product model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09a0c033a99b90afcc9ee75aa23ddf5d/href">https://medium.com/media/09a0c033a99b90afcc9ee75aa23ddf5d/href</a></iframe><p>And ProductCustomField:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca11c950f222497c1d347ab0651c92a2/href">https://medium.com/media/ca11c950f222497c1d347ab0651c92a2/href</a></iframe><h3>2. Adding Snipcart configuration settings</h3><blockquote><em>If you need more help for this part, refer to our documentation </em><a href="https://docs.snipcart.com/getting-started/installation"><em>here</em></a><em> and </em><a href="https://docs.snipcart.com/configuration/product-definition"><em>here</em></a><em>.</em></blockquote><p>Let’s make sure you can update the Snipcart API key directly from Wagtail’s dashboard. You’ll need to add site settings to do so.</p><p>Site settings are special fields that you can add to your models file. They’ll appear in the <em>Wagtail Settings</em> section of the dashboard.</p><p>Import this module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b04a25bdeda080ddbf2d87bb65f45e97/href">https://medium.com/media/b04a25bdeda080ddbf2d87bb65f45e97/href</a></iframe><p>Then add these:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55412456407c902ce562d317d315ff9c/href">https://medium.com/media/55412456407c902ce562d317d315ff9c/href</a></iframe><h3>3. Database migrations</h3><p>Now that your models are created, you’ll need to generate database migrations and run them.</p><p>In your terminal, use the makemigrations command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61b048b672970c355570e9a70f6a6b4e/href">https://medium.com/media/61b048b672970c355570e9a70f6a6b4e/href</a></iframe><p>You should see the following output:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eeb950b417af705e7daccadf2aa64acd/href">https://medium.com/media/eeb950b417af705e7daccadf2aa64acd/href</a></iframe><p>Once the migrations are generated, apply them on your database with the migrate command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba37d3de0b4220eadf4f359bc90ee044/href">https://medium.com/media/ba37d3de0b4220eadf4f359bc90ee044/href</a></iframe><p>It will take a couple of seconds; Wagtail will set up its own database schema along with the models you just defined.</p><p>Finally, create your first CMS user with the createsuperuser command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1499b9f179371e98a2ca1fd99b3fed08/href">https://medium.com/media/1499b9f179371e98a2ca1fd99b3fed08/href</a></iframe><p>Don’t forget the username and password you picked — you will need them to log into Wagtail’s dashboard.</p><h3>4. Creating products</h3><p>Start by firing up your dev server with the Django dev server command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56cb6bfc4a10a1b4db08d5ff94d45d6b/href">https://medium.com/media/56cb6bfc4a10a1b4db08d5ff94d45d6b/href</a></iframe><p>Now open your browser and navigate to: http://localhost:8000/admin. Use the credentials you set up earlier to log in.</p><p>Select the Home page in Wagtail&#39;s menu. Then click on the Add child page button.</p><p>You’ll be asked to pick a type of page, select Product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ag25Kk0V2e8jAblk.png" /></figure><p>Enter the product details, then publish your new product:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/0*OnjAozD4qUbbB6mK.png" /></figure><p>You can create as many products as you wish.</p><h4>4.1 Adding Snipcart API key</h4><p>Remember the SnipcartSettings class you created? You&#39;ll be able to configure your API key by expanding the <em>Settings</em> menu and going to <em>Snipcart settings</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/0*rBjQbM9N66JToYtB.png" /></figure><p>Open Snipcart’s dashboard and get your public API key (Test or Live). Go back to Wagtail and paste it in the API key field.</p><p>Save your settings.</p><h3>5. Templating</h3><p>Your backend is now ready, your API key is configured, and your first products are created. Time to start building the site.</p><p>For this demo, I decided to use <a href="https://picturepan2.github.io/spectre/">Spectre.css</a> CSS framework. It’s straightforward and lightweight.</p><p>Open the base.html file located in snipcartwaigtaildemo/templates.</p><p>You’ll need to add references for Spectree.css and Snipcart. Add these lines in the head of your document:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8962cc437300bc126f6506906a555941/href">https://medium.com/media/8962cc437300bc126f6506906a555941/href</a></iframe><p>The Snipcart API key that you configured previously is available via:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf304feeae09f5e13e2781e74dd3ba15/href">https://medium.com/media/bf304feeae09f5e13e2781e74dd3ba15/href</a></iframe><p>Then, add the navbar and some other Spectre.css layout elements.</p><p>Replace the content of the whole body by these lines:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f106a7e746c76dc65ac003e48af7a39/href">https://medium.com/media/9f106a7e746c76dc65ac003e48af7a39/href</a></iframe><h4>5.1 Listing products</h4><p>The first template you need is your index, where products will be listed.</p><p>You’ll have to make your products available in your home page context. In any Wagtail Page, you can override a method name get_context.</p><p>You can add the data that the view will receive in parameters. In my case, I want to set the products context variable.</p><p>Open the models.py file in home folder and update the HomePage class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d93ea2ac0940a87bfd22c53705815568/href">https://medium.com/media/d93ea2ac0940a87bfd22c53705815568/href</a></iframe><p>Then, open the home_page.html file located in the home/templates/home folder.</p><p>Let’s create a simple page that will show each product image with a link to the product details.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a085127d23bd83f8ac4e6452850de85/href">https://medium.com/media/0a085127d23bd83f8ac4e6452850de85/href</a></iframe><h4>5.2 Product details</h4><p>The last template is the one showing individual product details along with the Snipcart buy button.</p><p>Also, it would be nice to be able to select product options directly on this page, before adding it to the cart. So I’ll add a way to choose all custom fields with options directly in the template.</p><p>Before writing some HTML, you have to update the view context. Django templates don’t give us 100% access to all Python methods and objects, so things like splitting a string do not work very well.</p><p>I decided to override the get_context method again. Maybe there&#39;s a better way to do that—let me know in the comments below! ;)</p><p>Open models.py from the home folder and add this method in the Product class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f58bbbaa37274894413e21b5b9eab2b/href">https://medium.com/media/4f58bbbaa37274894413e21b5b9eab2b/href</a></iframe><p>A custom_fields array will be available in the product.html template.</p><p>Create a file named product.html in home/templates/home folder. This is the template that will be associated with the Product page model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe6ecd3ec281de32fc7ff608048deb5b/href">https://medium.com/media/fe6ecd3ec281de32fc7ff608048deb5b/href</a></iframe><p>Then, add some JavaScript to update the Snipcart buy button when a custom field selection is made on the page.</p><p>Add the following script snippet before the endblock statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10e3ec556d7e3a17c2197df678302c17/href">https://medium.com/media/10e3ec556d7e3a17c2197df678302c17/href</a></iframe><p>This code updates the button data attributes when the select value changes.</p><p>If you click on the + button beside any product, you should see its details:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/0*KpIg2MVlYmId1vii.png" /></figure><p>You now have pretty strong foundations to start your e-commerce project using Django and Wagtail!</p><p>These frameworks are very powerful. You could quickly add some search functionalities, product suggestions, reviews, etc.</p><h3>Live demo &amp; GitHub repo</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/0*qBp89SAh-KJynlVL.png" /></figure><blockquote><em>See the </em><a href="https://snipcart-wagtail.herokuapp.com/"><em>live demo</em></a></blockquote><blockquote><em>See the </em><a href="https://github.com/snipcart/snipcart-wagtail-integration"><em>GitHub repo</em></a></blockquote><h3>Closing thoughts</h3><p>I really enjoyed working with Wagtail, as it’s simple and intuitive. I have to say that their documentation feels incomplete at times, though. At first, I wanted to make some changes on how routing would work and haven’t found anything in their docs about that.</p><p>I didn’t have Python installed on my laptop at the start, so setting up everything and having this demo up and running took me about a day, including hosting the demo. I figure it would be way faster for avid Python developers!</p><p>For further exploration, I think Wagtail could be a great headless CMS, especially with <a href="http://docs.wagtail.io/en/latest/advanced_topics/api/">their built-in API</a>. I think it could be cool to leverage it and strap it to tools like Nuxt or Gatsby to handle the front end.</p><p>Maybe another time! ;)</p><p><em>If you’ve enjoyed this post, please take a second to share it on </em><a href="https://twitter.com/home?status=A%20Guide%20to%20%23Django%20E-Commerce%20(Bonus%3A%20a%20Wagtail%20Shop%20Tutorial)%20https%3A//bit.ly/2cJLzxp%20%23webdev%20%23ecommerce"><em>Twitter</em></a><em>. Got comments, questions? Hit the section below!</em></p><p><em>Originally published at </em><a href="https://snipcart.com/blog/django-ecommerce-tutorial-wagtail-cms"><em>snipcart.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dd2043f89e7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/how-to-build-an-e-commerce-shop-with-python-django-wagtail-3dd2043f89e7">How to build an e-commerce shop with Python, Django, &amp; Wagtail</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Discovering Hexo: E-Commerce with Node.js Blog Framework]]></title>
            <link>https://codeburst.io/discovering-hexo-e-commerce-with-node-js-blog-framework-cd75ad51667d?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd75ad51667d</guid>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Mon, 14 Jan 2019 20:52:59 GMT</pubDate>
            <atom:updated>2019-01-17T15:51:21.621Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H3QPKQld73sBaUl_GkCLIQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/hsg538WrP0Y?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">drmakete lab</a> on <a href="https://unsplash.com/search/photos/hexagon?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Have you ever thought of adding e-commerce directly into your blog’s content?</p><p>After all, why not? With content marketing on the rise and bloggers taking over the world, you might as well have all the tools you may need to optimize that blog. Today, we’ll show you a really, really, <strong>REALLY</strong> easy way to do exactly that.</p><p>Let’s put us in context a little bit first.</p><p>Lately, we stumbled upon an article that caught our attention. Posted on Netlify’s blog, it talks about the rise of the <a href="https://jamstack.org/"><strong>JAMstack</strong></a> and lists the <a href="https://www.netlify.com/blog/2017/05/25/top-ten-static-site-generators-of-2017/">10 best static site generators in 2017</a>. Obviously, it’s still a matter of opinion and each of them will answer different developer’s needs, but still, it’s a nice way to start discovering them.</p><p>If you’ve been following our blog, you know it’s a movement we <a href="https://snipcart.com/blog/jamstack-clients-static-site-cms">believe in and support</a>.</p><p>We realized that we explored most of these static site generators on our blog in various tutorials of shopping cart integration, but we didn’t complete the top 3 yet. Well, let me tell you that we took this new mission to complete the podium very seriously.</p><p>The two first spots have already been covered with our integrations on <a href="https://jekyllrb.com/">Jekyll</a> and <a href="https://gohugo.io/">Hugo</a>. Now we’ll close it with <a href="https://hexo.io/"><strong>Hexo</strong></a>.</p><blockquote><em>You can find all these other static site generator’s tutorials </em><a href="https://snipcart.com/blog/categories/jamstack"><em>right here</em></a><em>.</em></blockquote><p>Hexo being primarily a blog framework we’ll show you how you can integrate a Snipcart housed buy-button into your blog platform. You’ll never know when you’ll want to give the option to your reader to buy a product you’re writing about, so why not sell this product directly on your blog?</p><p>Above all this, it’s nice to discover new tools and we love to do it for you, so here we go!</p><h3>What is Hexo?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TLqMtsW8e49aPt0Y.png" /></figure><p>That tagline says it all, right?</p><p><strong>Hexo is a tool designed with Node.js to fastly build impressive blog platforms.</strong></p><blockquote><em>Need a refresh about Node.js? Read our </em><a href="https://snipcart.com/blog/nodejs-ecommerce-harp-js-static"><em>blog post</em></a><em> about it! </em><a href="http://www.thecrazyprogrammer.com/2016/03/top-7-reasons-node-js-popular.html"><em>Here’s why it’s so popular</em></a><em>.</em></blockquote><p>Its Node.js foundations allow for incredible speed in generating hundreds of pages. It’s easy to use with a great Markdown support and a bunch of plugin options coming from recognized applications like Jekyll and <a href="http://octopress.org/">Octopress</a>. You can easily deploy your site, only hitting one command, to the likes of GitHub and Heroku.</p><p>The main problem with Hexo is probably that the majority of its vast community is Chinese-speaking, so it may be hard to find valuable tutorials or guides in English. Fortunately, the documentation is exhaustive and we hope that this piece of content can be of value to the English community.</p><p>As a static site generator, it assures you to get a website that is <em>fast</em>, <em>secure</em> and <em>flexible</em>. If you still don’t know that, yes, <a href="http://www.nytimes.com/2012/03/01/technology/impatient-web-users-flee-slow-loading-sites.html?pagewanted=all">speed is a pretty big deal</a> nowadays.</p><p>It presents itself has <em>a fast, simple &amp; powerful blog framework</em>, let’s see if it can hold up to those expectations!</p><h3>Hexo tutorial — E-commerce on a blog</h3><p>In this demo, we’ll find a way to add call to action buttons to a blog post that will allow a customer to buy the product that a post might talk about. It can be very interesting to create contextual shopping experiences.</p><p>I’m feeling a bit evasive today and I want to start a blog about space. I may want to show off some gear I’ve been gathering for an upcoming adventure on Mars, who knows? With the development of programs like <a href="http://www.spacex.com/">SpaceX</a>, better be ready. Let’s see what happens with that…</p><p>We’ll start by installing hexo. It’s very simple:</p><pre>npm install hexo -g</pre><p>We need to install Hexo via <a href="https://www.npmjs.com/">npm</a>, we’ll then have access to their CLI that will allow us to create our first site. Once it’s installed just type this in your prompt:</p><pre>hexo init yoursite</pre><p>Hexo will automatically clone a git repository and run some installation commands. Once it’s done you can simply run hexo server and it will automatically start a server from your local directory. To be honest, I was kind of blown away by how simple it is to create a blog using Hexo.</p><h3>Configuration</h3><p>First thing we’ll do is to add the Snipcart API key to the _config.yml file. This way it won&#39;t be hardcoded in the site itself.</p><p>For this, you’ll need a <a href="https://app.snipcart.com/register">Snipcart account</a>. (Which is completely free as long as you use it in test mode!)</p><p>We’ll open the _config.yml file and add a new configuration setting:</p><pre>snipcart_api_key: YOUR_API_KEY</pre><p>You will have access to this setting directly in your template through the config object.</p><h3>Adding Snipcart</h3><p>We’ll stick with the default theme for this case, but it’s very easy to install new themes or a build a new one. The <a href="https://hexo.io/docs/">Hexo documentation</a> is very well done so we’ll skip this part.</p><p>We’ll start by adding Snipcart necessary files; open head.ejs file and add the following lines:</p><p>Please note that we are using the variable we defined in our config file earlier.</p><p>Then, we need to add the buy button. One cool thing about Hexo is their support of Font-Matter, you can basically add any settings to a markdown article and you’ll have access to these settings in the templates.</p><p>We’ll start by creating a new article in the source/_posts folder. We&#39;ll name it: steps-for-a-new-life-on-mars.md for our demo here.</p><p>The content will be the following:</p><p>Nice stuff! We’ve got a killer of a blog post right there.</p><p>Please note that I added some product details in the post header; we’ll have access to that in our templates. So whenever a product is defined in an article we’ll be able to add a call to action button that will make it possible to buy the item we are talking about in the post.</p><p>We’ll then open the article.ejs template, which is the template used to render a blog post article.</p><p>Just before the &lt;footer&gt; node, we&#39;ll add the Snipcart button:</p><p>The button will be shown only if a product has been defined in the article. I also added a new CSS class named article-buy-now in the article.styl file to make it look better.</p><p>If you refresh your browser and visit the article page you should see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GvFQB6MkVSZk6HHD.png" /></figure><p>And if you click on the buy button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GHrWzv5KVvxRm6qM.png" /></figure><h3>Ready for space yet?</h3><p>That’s about it! Simple isn’t it? You can see a <a href="https://snipcart-hexo.netlify.com/"><strong>live demo</strong></a> and browse the <a href="https://github.com/snipcart/snipcart-hexo-integration"><strong>source code</strong></a> on GitHub.</p><p>It only took me an hour top to have it working. I really think Hexo is one of the easiest and powerful blogging static site generator out there at the moment. Up to this day, I honestly think it was one of the simpler demos I had to put up. So, I guess our friends at Netlify were right to put in the top 3 and I raise my hat (or astronaut helmet) to them. ;)</p><p><strong>Node.js</strong> is big right now and it’s always quite fun for us to work with it.</p><p>If this isn’t enough for you and you want to dive deeper into Hexo you can visit the <a href="https://github.com/hexojs/awesome-hexo"><strong>awesome list</strong></a> where you can find resources, themes, plugins, etc. You can also find a longer <a href="https://www.staticgen.com/">list of static site generators</a> open to use right now if you can’t wait for us to get through all of them. ;)</p><p>Any suggestions of static site generator you would like us to tackle next time? Hit us in the comments!</p><blockquote><em>Like the Hexo + Snipcart combo? Here’s a clean and simple </em><a href="https://gumroad.com/l/hexo-theme-milan"><em>e-commerce theme</em></a><em> using both, available on Gumroad!</em></blockquote><p>If you’ve enjoyed this post, please take a second to 👏 + share it on Twitter. Got comments, questions? Hit the section below!</p><p><em>I originally published this post on the </em><a href="https://snipcart.com/blog/react-ecommerce-gatsby-tutorial"><em>Snipcart blog</em></a><em> and shared it in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd75ad51667d" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/discovering-hexo-e-commerce-with-node-js-blog-framework-cd75ad51667d">Discovering Hexo: E-Commerce with Node.js Blog Framework</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Dive into VuePress: Craft a Clean Documentation & Blog]]></title>
            <link>https://medium.com/hackernoon/deep-dive-into-vuepress-craft-a-clean-documentation-blog-de6ac8e0c917?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/de6ac8e0c917</guid>
            <category><![CDATA[documentation]]></category>
            <category><![CDATA[blog]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Mon, 29 Oct 2018 19:13:18 GMT</pubDate>
            <atom:updated>2019-04-29T11:32:06.904Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Phg_9259Vl8tNHyCh_W3sg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/-FPFq_trr2Y?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Daniel McCullough</a> on <a href="https://unsplash.com/search/photos/architect?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Have you ever skimmed through the Vue.js documentation?</p><p>Anyone familiar with it will tell you:</p><p>It&#39;s <strong>magnificent</strong>.</p><p>To build it, Evan You, <a href="https://snipcart.com/blog/vue-js-seo-prerender-example">Vue</a>&#39;s creator, came up with VuePress.</p><p>For an upcoming side project of ours, we decidedly wanted some of that docs&#39; magic. This post is an account of that process.</p><p><strong>You’ll learn what VuePress is and what’s so great about it. You’ll learn how to quickly set up a neat Vue.js-powered documentation &amp; blog.</strong></p><p>Steps for the technical tutorial:</p><ol><li>Installing VuePress.</li><li>Generating documentation content.</li><li>Adding a blog to VuePress.</li><li>Inserting the blog in the navigation.</li></ol><p>It&#39;s easy &amp; straightforward—you&#39;ll be done in no time.</p><h3>A bit of context for our VuePress project</h3><p>What&#39;s this side project I&#39;m talking about? Well, it started with a pain any blog editor or webmaster knows too well:</p><p><strong>Cringe-worthy outreach emails.</strong></p><p>You know the kind of email that praises your blog content and offers some guest posts about real estate... on a WEB DEVELOPMENT platform?!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/0*1mRgQURwNSFicr3s.gif" /><figcaption>C’mon man, try a little harder!</figcaption></figure><p>We thought it was time to put a spotlight on this stinky <a href="https://hackernoon.com/tagged/marketing">marketing</a> technique. To do so, we built a simple <a href="https://gohugo.io/">Hugo</a> static site where anyone can publish anonymous and funny examples of how not to outreach. The name of the site? <strong>Outreach Shame</strong>.</p><blockquote>Note: Outreach Shame isn’t live yet. It presented us with a good use case to build VuePress documentation. You’ll find real guidelines in the documentation we created (link further down). Follow them to send us your lousy outreach examples; they’ll appear on the site once it goes live.</blockquote><h3>What is VuePress?</h3><p>In a few words, <strong>VuePress is a minimalistic Vue-powered static site generator.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*i54fFrW1MWlpLXOe.png" /></figure><p>As mentioned earlier, the original goal behind its design was to generate documentation. Its system also allows you to write a theme for almost anything.</p><p>I&#39;ve read in a few places that VuePress <a href="https://vuepress.vuejs.org/guide/#todo">is not blog-aware yet</a>. Although it does miss some blogging features out-of-the-box, it&#39;s still well-suited for simple blogs, as you&#39;ll see later on.</p><p>VuePress is only a few months old but <a href="https://www.staticgen.com/">has already made some waves</a> mainly because it comes directly from the brain behind Vue.js. Let&#39;s see what <a href="https://www.youtube.com/watch?v=lIv1ItUzktc">the man has to say</a> about VuePress:</p><blockquote>“It’s special in the sense that it completely uses Vue for its layout. Because Vue is a frontend framework it’s actually using a few server-side rendering capabilities to do the static HTML generation. The result is, when you write your layout or your theme for a VuePress site it’s actually a universal application that runs both on the server and on the client.”</blockquote><p>The server is skipped though. Every route of the app is virtually rendered to generate static HTML files that load as a Vue.js single page application.</p><h3>Why should you use VuePress?</h3><p>As you&#39;re probably aware, <a href="https://snipcart.com/blog/choose-best-static-site-generator">there are tons of static site generators</a> out there. So where does VuePress stand out?</p><p>→ <strong>Its unique use of Markdown &amp; Vue components</strong></p><p>VuePress handles the conversion of Markdown to HTML by default. This makes generating new content very easy. <em>You can even</em> <a href="https://vuepress.vuejs.org/guide/using-vue.html#using-vue-in-markdown"><em>use Vue components</em></a><em> directly in your Markdown files</em>. It allows you to add custom or dynamic features to your website smoothly. Other built-in <a href="https://vuepress.vuejs.org/guide/markdown.html">Markdown features</a> include a YAML front-matter to set predefined variables or create custom ones that VuePress will automatically interpret.</p><p>→ <strong>Its multi-language support from the get-go</strong></p><p>Out-of-the-box, VuePress lets you write several versions of your site in as many languages as you need. (!)</p><p>→ <strong>Its neat default &amp; custom themes</strong></p><p>The least I can say is that the VuePress default theme looks <em>clean</em>. It&#39;ll do the job for any fast implementation of technical documentation. It&#39;s responsive &amp; fully-featured (search capabilities, navbar &amp; sidebar, optional homepage, etc.). Then, <a href="https://vuepress.vuejs.org/guide/custom-themes.html#custom-themes">customizing the default theme</a> or starting from scratch is simple and fun.</p><h3>Why VuePress over Nuxt.js for a Vue SPA?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r_LyLNEcWrVc1atA.png" /></figure><p>If you&#39;re into the whole Vue.js ecosystem you surely already know about the framework for universal apps that is <a href="https://nuxtjs.org/">Nuxt.js</a>. It&#39;s way more than an SSG, but its nuxt generate feature allows you to create a static version of an app. VuePress was inspired by that exact feature.</p><p>Both have very different goals. Everything you can do in VuePress you&#39;ll also be able to achieve with Nuxt.js. VuePress simplifies the things its good at though, which for the moment include documentation &amp; very small SPAs. If that&#39;s all you need, VuePress works wonders, while Nuxt.js might quickly feel overkill.</p><p>On the other hand, you couldn&#39;t use VuePress for any real <a href="https://snipcart.com/blog/cockpit-cms-tutorial-nuxtjs">app development</a>. There&#39;s no debate you should go with Nuxt.js here.</p><p>Nuxt.js is often mentioned as a great tool to <a href="https://snipcart.com/blog/vue-js-seo-prerender-example">handle Vue.js SPA SEO</a> issues. It turns out VuePress does the same to ensure that all content is rendered and crawlable by search engines.</p><p>I’ll close this argument by saying that I was very impressed by VuePress. It’s so simple that I had to think hard about a more complex use case to fill this tuts!</p><p>Anyway, let&#39;s see how it went! ;)</p><h3>VuePress tutorial: Crafting documentation &amp; blog platform</h3><p>For my use case, I&#39;ll go on and use VuePress for Outreach Shame&#39;s documentation. While I&#39;m at it, why not add a blog to support the project as well?</p><p><strong>Prerequisites:</strong></p><ul><li>Have npm installed</li><li>A basic understanding of Vue.js</li></ul><h4>1. Installing VuePress</h4><p>VuePress is a Node application that needs to be installed via npm or yarn.</p><p>Open a terminal and type:</p><pre>npm i -g vuepress</pre><p>VuePress will then be available directly in your command line. Two useful commands here:</p><ul><li>vuepress dev starts a development server with automatic reload and all the dev goodies. This is the command you&#39;ll use when developing your website.</li><li>vuepress build builds your website. The generated assets will be ready to deploy on any hosting service.</li></ul><h4>2. Generating documentation content</h4><p>You can add VuePress to any repository with a README.md file. This is why VuePress&#39; main selling point is cleaner documentation.</p><p>At the root of your project, create a README.md file. It&#39;s going to be your site index, the home page. My content:</p><pre># Outreach shame </pre><pre>Outreach Shame is the place to expose every lousy outreach emails. It’s every online marketer’s worst nightmares. These emails are everything you shouldn’t do to promote your business &amp; content. Everything’s kept anonymous for good fun above all. </pre><pre>## How can you contribute? </pre><pre>Simple, forward us the shameful email to *outreachshame@snipcart.com* where it’ll be up for moderation. We kindly ask you to censor any personal information from the email before forwarding it to us. Once your submission is accepted by our team it’ll be published on the site outreachshame.com. </pre><pre>## How it does work? </pre><pre>Once a submission is approved, a commit is pushed in this [GitHub repository](https://github.com/snipcart/outreachshame). We then use [Forestry](https://forestry.io/#/) to make content editing and publishing easier for our dumb marketing folks. Once the entry is published, the site is built and deployed automatically on Netlify.</pre><p>Then, start your development server:</p><pre>vuepress dev</pre><p>When VuePress is ready, open a browser and navigate to http://localhost:8080.</p><p>You will see your website with the classy default VuePress theme.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OrTj9tXsqPbIYxar.png" /></figure><p>It&#39;d be nice to have a title in the navbar, right?</p><p>Well, customizing the default VuePress theme with quick configurations is easy peasy.</p><p>Start by creating a folder named .vuepress at the root of your project&#39;s folder. In this folder, add a file named config.js. This file will be used to customize the website. It&#39;s basically a <a href="https://hackernoon.com/tagged/javascript">JavaScript</a> file that should export a JSON object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a38c7abd09214b6e5a335f99991c89c/href">https://medium.com/media/1a38c7abd09214b6e5a335f99991c89c/href</a></iframe><p>If you reload your website, you&#39;ll see the title in the top left corner of the navbar.</p><h4>3. Adding a blog to VuePress</h4><p>As you can see, it&#39;s a walk in the park to build custom documentation for a project using VuePress. So let&#39;s push it further a bit and add a blog.</p><p>As I wrote earlier, VuePress allows you to write custom Vue components that can be inserted into any Markdown files. In this case, I&#39;ll build a component that will render the list of blog posts.</p><p>Create a folder named components in the .vuepress folder. You&#39;ll add your components in there. They&#39;ll then be automatically available globally; no need to import them.</p><p>Somehow VuePress has it covered.</p><p>Create a file named BlogIndex.vue—VuePress components use the single file component pattern.</p><p>A component created in a VuePress application gets some computed properties out of the box. You can access this.$site which gives you metadata of the website, with each page in it. This is going to be useful in this case because this is how we&#39;ll get the blog posts.</p><p>You also have this.$page giving you information about the current page context.</p><p>Here is the code you will need in your component file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7be5c8608163d28e99a8b9e1367e25a7/href">https://medium.com/media/7be5c8608163d28e99a8b9e1367e25a7/href</a></iframe><p>The template itself is pretty basic. I iterated through the list of posts and listed them all. I&#39;m using the router-link component available through vue-router that VuePress comes equipped with.</p><p>I added a computed property named posts. This property gets all the blog posts from the site pages. When you create new blog posts, you&#39;ll need to specify some front matter information, such as the post date. Newest posts will appear first.</p><p>The component is ready! Now you&#39;ll need to add some content.</p><p>Create a folder named blog at the root of the project. In this folder, add a README.md file. This is going to be the blog index, and this is where you&#39;ll include the BlogIndex component.</p><pre>--- <br>blog_index: true <br>--- </pre><pre># Blog </pre><pre>Welcome on Outreach shame blog </pre><pre>&lt;BlogIndex /&gt;</pre><p>The blog_index front matter will be used to make sure that the blog index isn&#39;t listed in the posts. I&#39;m using it when filtering the posts in the posts computed property of the BlogIndex component.</p><p>Notice how easy it is to include a custom component. It&#39;s registered automatically by VuePress and can be included in any markdown file.</p><p>Next step is to add a first blog post. In the blog folder, create a filed named first-post.md.</p><p>You&#39;ll have to define a couple of frontmatter settings:</p><pre>&lt;!-- /blog/first-post.md --&gt;<br>--- <br>title: Bacon ipsum dolor amet hamburger pancetta ball tip <br>date: 2018-07-28 <br>description: <br>     Kielbasa tenderloin boudin bacon cupim, pastrami strip steak rump picanha meatloaf venison meatball ribeye. Burgdoggen t-bone jowl venison biltong andouille. Turducken shankle tongue landjaeger drumstick, pancetta porchetta. Brisket ham turkey andouille picanha. Pancetta chuck shank ham. <br>--- </pre><pre># First post </pre><pre>/* Your awesome content goes here */</pre><p>The properties title, description and date are important. They will be used when listing blog posts. Every post in the blog folder should have these defined.</p><h4>4. Inserting the blog in the VuePress navigation</h4><p>The last step will be to make the blog available via the navigation bar.</p><p>Open /.vuepress/config.js file again and add:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9e7628ff64611aa1348e0eaf6ac6d49/href">https://medium.com/media/d9e7628ff64611aa1348e0eaf6ac6d49/href</a></iframe><p>You&#39;ll need to specify the nav bar elements in the themeConfig property.</p><p>Then, reload your website. Click on the blog navigation item, you should have something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/951/0*nRXohjbshwc8Kzer.png" /></figure><h3>Live demo &amp; GitHub repo</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nMkMgb5dIlw-Th-N.png" /></figure><blockquote><em>See the live demo </em><a href="https://outreachshame-docs.netlify.com/"><em>here</em></a></blockquote><blockquote><em>See GitHub repo </em><a href="https://github.com/snipcart/outreachshame-docs"><em>here</em></a></blockquote><h3>Closing thoughts</h3><p>VuePress is straightforward. I only spent around two hours building this demo. That includes learning how VuePress works. It might just be the best tool out there to document your open source project or library.</p><p>Having the ability to use Vue components on top of that is a huge bonus. We&#39;re currently working on the third version of our shopping cart. This experiment forces me to seriously consider using VuePress to build its documentation!</p><p>However, as working with it confirmed, I don&#39;t think you should use VuePress to build applications. Frameworks like Nuxt and Gatsby would be better options to develop a more complex application.</p><p><em>If you&#39;ve enjoyed this post, please take a second to </em><a href="https://twitter.com/home?status=Deep%20Dive%20into%20%23VuePress%3A%20Craft%20a%20Clean%20Documentation%20%26%20Blog%20https%3A//bit.ly/2KmWY46%20%20%23vuejs%20%23webdev"><em>share it on Twitter</em></a><em>. Got comments, questions? Hit the section below!</em></p><p><em>I originally published this on the </em><a href="https://snipcart.com/blog/vuepress-tutorial-vuejs-documentation"><em>Snipcart blog</em></a><em> and shared it in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de6ac8e0c917" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/deep-dive-into-vuepress-craft-a-clean-documentation-blog-de6ac8e0c917">Deep Dive into VuePress: Craft a Clean Documentation &amp; Blog</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Webtask Backend-as-a-Service: Quick Serverless Tutorial]]></title>
            <link>https://medium.com/free-code-camp/webtask-backend-as-a-service-serverless-e-commerce-tutorial-fb3d1240412c?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb3d1240412c</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[ecommerce-web-development]]></category>
            <category><![CDATA[functions-as-a-service]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Tue, 02 Oct 2018 14:32:46 GMT</pubDate>
            <atom:updated>2018-10-02T17:38:29.138Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EfrEX0yGHBu3j_YtGdOs9w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/T7s_TnKO-dk?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Sharosh Rajasekher</a> on <a href="https://unsplash.com/search/photos/cloud?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The word <strong>serverless</strong> is buzzing through dozens of dev circles today.</p><p>It has been for a while now.</p><p>I’ve been meaning to exit my code editor and come talk about the trend here. Especially since I discovered <a href="https://webtask.io/">Webtask</a>, a few months ago.</p><p>So, in between bug fixes, support &amp; new features, I finally took some time to sit down and write a little.</p><p>In this post, I’ll touch on serverless architectures and Backend as a Service tools a bit. But I’ll mostly focus on offering a simple yet meaningful serverless e-commerce tutorial. And I’ll use our own HTML/JS shopping cart platform &amp; Webtask to do so.</p><h4>What is a serverless architecture, and why should you care?</h4><p>Like <a href="https://snipcart.com/blog/jamstack-clients-static-site-cms">the “JAMstack”</a>, <strong>serverless architecture</strong> is a new web development trend subscribing to the “rise of the frontend” paradigm we’re in. In layman’s terms, it’s a way to run server-side code in the cloud without worrying about web servers, routing, etc. It relies heavily on <strong>Backend as a Service (Baas)</strong> third parties such as Webtask, or the most popular kid on the block: <a href="http://docs.aws.amazon.com/lambda/latest/dg/welcome.html">AWS Lambda</a>. With these BaaS services, you just write code and let them take care of all the underlying infrastructure. Scaling-wise, such an approach is quite awesome: the abstraction layer these services offer can handle peaks of traffic on your site wonderfully.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/0*SRdTxRbRT202iPXR.png" /><figcaption><em>I could go on about serverless architecture, but I’d strongly advise reading </em><a href="http://martinfowler.com/articles/serverless.html"><em>Mike Roberts’ post on Martin Fowler’s blog</em></a><em> (credits for the image above go to this post). He’s done an impressive job illustrating the ins and outs of the serverless approach.</em></figcaption></figure><p>As you may know, we’re big fans of <a href="http://jamstack.org/">the JAMstack</a> (JavaScript, APIs &amp; Markup) here at Snipcart. It’s another vibrant expression of the “rise of the frontend” paradigm I mentioned. We’ve written whole tutorials on how to run e-commerce with static site generators such as <a href="https://snipcart.com/blog/static-site-e-commerce-part-2-integrating-snipcart-with-jekyll">Jekyll</a> or <a href="https://snipcart.com/blog/hugo-tutorial-static-site-ecommerce">Hugo</a>, and even with <a href="https://www.contentful.com/blog/2016/02/10/snipcart-middleman-contentful/">API-first CMS like Contentful</a>. From cost to tech stacks shifts, I believe such an approach will deeply impact how businesses handle web development in the next years.</p><p>However, I’m aware that it has its limits: a modern static site is <strong>raw content</strong>, which means using dynamic features such as webhooks would be impossible without <em>some</em> server-side code. That’s where Webtask comes in.</p><h4>Webtask: Backend-as-as-Service for Targeted Tasks (or FaaS)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6cXnCOKnAjNlzCvv.png" /></figure><p>Webtask is a neat service crafted by <a href="https://auth0.com/">Auth0</a>, the good folks who made a serious dent in the online authentication world. Acting as a Function as a Service, it basically removes the need to configure a backend for simple mobile or single-page apps. Often <a href="http://thenewstack.io/often-choose-webtask-lambda/">compared to AWS Lambda</a>, it allows developers to write server-side logic &amp; functions executed via HTTP calls. So it’s one of the best Backend as a Service tools out there for developers who’d rather focus on the frontend than configure the backend.</p><p>Now let’s see how perfect it is for the use case we’ll explore in this serverless tutorial.</p><h4>Serverless e-commerce tutorial: webhooks &amp; custom shipping rates</h4><p>At Snipcart, I believe one of our most powerful features is <a href="https://docs.snipcart.com/configuration/shipping-providers#webhooks-shipping-api">the Webhooks Shipping API</a>. Simply put, it gives you full control on how your e-commerce site handles shipping.</p><p>However, leveraging this feature requires running server-side code. So if you wanted to use a JAMstack set up with a static site generator, you’d be screwed. Thanks to Webtask, however, you’re not! In this serverless tutorial, we’ll use it to host the e-commerce shipping function we need directly via their platform.</p><h4>Our simple serverless e-commerce use case</h4><p>Now, let’s pretend we have a static e-commerce site running with Snipcart.</p><p>And let’s say we want to offer three special shipping rates:</p><ul><li>One for customers in our Québec hometown</li><li>One for US customers</li><li>One for all other international customers</li></ul><h4>1. How to create the Webtask function</h4><p>First of all, let me explain a little how our shipping API works. Snipcart sends all the order details to a URL you can specify in your merchant dashboard. Using this data, you can then write code to return available shipping rates to your end customers.</p><p><strong>Start by creating an account on </strong><a href="https://webtask.io/"><strong>https://webtask.io/.</strong></a><strong> Once it’s done, follow their steps to install the Webtask CLI via </strong><strong>npm.</strong></p><p>We’ll now create a file named shipping_task.js. It&#39;ll contain all the code needed to parse the order details received from Snipcart and return the available shipping rates.</p><p>Let’s start by exporting a module that Webtask will understand.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5871089ed10fa0c47da71346a0ad206/href">https://medium.com/media/b5871089ed10fa0c47da71346a0ad206/href</a></iframe><p>The first parameter, context, contains the data Snipcart will send to your application. Webtask takes care of parsing the JSON, and you can access all the event details along with the order via context.body.</p><p>With the code above, our task would return the request body that it received; pretty useless. ;)</p><p>Now let’s say we want to offer free shipping for customers in Québec.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/738f2c613d43649d42b6d1099ea522d1/href">https://medium.com/media/738f2c613d43649d42b6d1099ea522d1/href</a></iframe><p>Ain’t that nice? However, with this code, customers outside Québec won’t have any shipping options. So we’ll make sure to return a standard shipping rate in case the order does not match our conditions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4690ee81dbb984b21eb4e72f1a2bbe33/href">https://medium.com/media/4690ee81dbb984b21eb4e72f1a2bbe33/href</a></iframe><p>You can see below the complete code with some additional conditions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc113bd47542eb579fdb68bca5f47712/href">https://medium.com/media/cc113bd47542eb579fdb68bca5f47712/href</a></iframe><p>Once it’s done, we just need to push our code to Webtask.</p><pre>wt create shipping_task.js</pre><p>Webtask will then generate a URL; you should see it in your terminal.</p><p>Simply use this URL and when configuring the Webhooks Shipping API.</p><h4>2. Securing the serverless component</h4><p>With our current set up, anyone could send requests to this API, right? That’s not something we want. So we’ll make sure that the function only handles requests coming from Snipcart. We’ll use our request validation API to do so.</p><p>First, we’ll need to send a Snipcart secret API key to the task in order to call the request validation API. We don’t want to expose it directly through the code, so we’re going to use the secrets feature that Webtask has. It allows us to pass secret parameters to the task that will be encrypted and accessible via the context object.</p><p>When creating the task, I added the --secret switch:</p><pre>wt create shipping_task.js — secret snipcartApiKey=MY_SECRET_API_KEY</pre><p>You will then be able to access this value using context.secrets.snipcartApiKey. We&#39;ll also use the request module, so we&#39;ll need to require it at the beginning of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be866d72f7712a33240f1d070c093fe5/href">https://medium.com/media/be866d72f7712a33240f1d070c093fe5/href</a></iframe><p>When we make requests to your Webhooks, we always include a request token in the request headers. The header is named X-Snipcart-RequestToken. We&#39;ll access it through our context object again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f10d2d83d7806407a2bfc79b80cfccae/href">https://medium.com/media/f10d2d83d7806407a2bfc79b80cfccae/href</a></iframe><p><em>Please note that all the headers are in lower cases with Webtask.</em></p><p>Here are the options we’ll use to send the request to our API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3817ceac8a19515d92bec9f86009db9d/href">https://medium.com/media/3817ceac8a19515d92bec9f86009db9d/href</a></iframe><p>We’ll run this request and only execute the code in the callback if it’s successful.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05d8d9b4e27dcd1e62a431d200e08cf9/href">https://medium.com/media/05d8d9b4e27dcd1e62a431d200e08cf9/href</a></iframe><p>So my whole function now looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3cab081861d65430a3d4ebb8ed2b13a/href">https://medium.com/media/a3cab081861d65430a3d4ebb8ed2b13a/href</a></iframe><h4>Closing words on Webtask &amp; the serverless approach</h4><p>Coming up with this little Webtask function for a static site running Snipcart took me less than two hours. Of course, we could’ve focused on other e-commerce functions: handling webhooks to make the bridge between Snipcart and external accounting systems, automating digital goods delivery, and more.</p><p>I really believe there are tons of exciting serverless use cases developers should try to handle with Auth0’s Webtask. Any integration with an API, or delegating more long-running/CPU-consuming jobs would make much sense!</p><p>The serverless approach is gearing up to have quite an impact on our work as developers. As we’re now seeing with RESTful APIs, many services will start relying on functions developed or hosted by others. The near future will bring more and more microservices hosted in environments like Webtask &amp; AWS Lambda. Especially coupled with the rise of <a href="https://snipcart.com/blog/vue-js-seo-prerender-example">frameworks like Vue</a> &amp; the JAMstack.</p><p>So I sincerely hope this post inspires developers to leverage the FaaS power of Webtask on different serverless projects, e-commerce or not. And of course, I’d be more than interested to have a look at <a href="https://snipcart.com/blog/sell-event-tickets-website">such set ups</a>. You can shoot us an email at geeks@snipcart.com if you got something similar to share, or if you have questions regarding this approach!</p><p><em>Liked the post? Take a second to 👏 and </em><a href="https://twitter.com/home?status=Using%20%40auth0%27s%20%40webtaskio%20FaaS%20on%20a%20%23serverless%20e-commerce%20tutorial%20http%3A//bit.ly/2e5vm8d%20%23webtask"><em>share it on Twitter</em></a><em>! Hit the comments for thoughts and questions.</em></p><p><em>Originally published on the </em><a href="https://snipcart.com/blog/webtask-baas-serverless-tutorial"><em>Snipcart blog</em></a><em> and in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb3d1240412c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/webtask-backend-as-a-service-serverless-e-commerce-tutorial-fb3d1240412c">Webtask Backend-as-a-Service: Quick Serverless Tutorial</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue.js SEO-Friendly SPAs: Tips, Tools & Prerender Example]]></title>
            <link>https://medium.com/hackernoon/vue-js-seo-friendly-spas-tips-tools-prerender-example-c160e0282eaf?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c160e0282eaf</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[framework]]></category>
            <category><![CDATA[seo]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Mon, 17 Sep 2018 18:19:40 GMT</pubDate>
            <atom:updated>2019-04-29T11:32:12.388Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f-Ey0tW6O_vFHz_RPZWh_A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/atSaEOeE8Nk?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Steven Lelham</a> on <a href="https://unsplash.com/search/photos/ranking?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Do you know about Hulu&#39;s <a href="https://hackernoon.com/tagged/javascript">JavaScript</a> fail story?</p><p>Back in 2016, the online video streaming service experienced a <a href="https://www.linkresearchtools.com/case-studies/hulu-javascript-fail-visibility-drop/">56% visibility drop due to bad JS handling</a>.</p><p>A nightmare no business wants to go through.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/0*_Z6XxG4pkAqLcUaP.gif" /></figure><p>To avoid such disasters, you have to know what you&#39;re doing when working with modern JavaScript frameworks.</p><p>Here at Snipcart, <a href="https://snipcart.com/blog/progressive-migration-backbone-vuejs-refactoring">we love Vue.js</a>, but are entirely aware of the <a href="https://hackernoon.com/tagged/seo">SEO</a> issues with a Vue.js singe-page application.</p><p>In this post, I want to show JS developers how easy it is to make Vue SEO-friendly.</p><p>I&#39;ll go through:</p><ul><li>General SEO tips you should always apply.</li><li>Specific Vue.js SPA SEO issues.</li><li>Tools to fix them with server-rendering &amp; prerendering.</li><li>A technical Vue.js SEO example using prerender-spa-plugin.</li></ul><blockquote>This post was followed by two other pieces about JS frameworks SEO issues, <a href="https://snipcart.com/blog/react-seo-nextjs-tutorial">for React</a>, and <a href="https://snipcart.com/blog/angular-seo-universal-server-side-rendering">Angular</a>.</blockquote><h3>General SEO tips</h3><p>Before we explore SEO issues specific to JavaScript SPAs, let&#39;s cover the best practices developers should adopt when building search engine optimized sites.</p><blockquote>As someone already said: “The best place to hide a dead body is the second page of Google search.”</blockquote><p>This shortlist is inspired by some of the biggest SEO players such as Moz, Backlinko, and Ahrefs. I&#39;ve added links to their resources should you want to dig deeper.</p><h4>→ Meta tags 🏷️</h4><p>Meta tags are low-hanging fruits. They let you show search engines precisely what your content is about.</p><p>However, not all tags are born equal. You should focus your energy on only a few of them, namely:</p><ul><li><strong>Meta content type</strong>—Has to be present on every page, declares your character set for the page.</li><li><strong>Title</strong>—Choose a unique title that catches searchers attention while accurately describing the content of the page. Keep it concise!</li><li><strong>Meta description</strong>—It should convince SERP (Search Engine Results Page) visitors to click on your link to find answers to their specific research intent.</li><li><strong>Viewport</strong>—Essential for a good mobile experience.</li></ul><p>Learn more: <a href="https://moz.com/blog/seo-meta-tags">SEO Meta Tags</a>, by Moz.</p><blockquote><a href="https://github.com/declandewet/vue-meta">vue-meta</a> is a solid tool to manage page meta info in Vue 2.0 components.</blockquote><p><strong>Social tags</strong> can also have a significant impact on SEO as they help your content to spread on social platforms, thus increasing your SEO social signals. There are specific tags for all the big platforms (Facebook, Twitter, Pinterest, Google+).</p><p>Learn more: <a href="https://moz.com/blog/meta-data-templates-123">Must-Have Social Meta Tags for Twitter, Google+, Facebook, and More</a>, by Moz.</p><h4>→ Mobile optimization 📱</h4><p>If you&#39;re not aware of <a href="https://webmasters.googleblog.com/2018/03/rolling-out-mobile-first-indexing.html">Google&#39;s mobile-first indexing</a>, well, consider this a wake-up call! Google is now giving more love to smooth mobile experiences than desktop ones, and so should you.</p><p>Learn more: <a href="https://backlinko.com/mobile-seo-guide">Mobile SEO: The Definitive Guide</a>, by Backlinko.</p><blockquote><a href="https://www.nativescript.org/vue">NativeScript</a> powers cross-platorm Vue.js mobile apps.</blockquote><h4>→ HTTPS 🛡️</h4><p>A missing HTTPS certification or a broken config could penalize your website. Isn&#39;t it understandable that search engines are inclined to push sites that are trusted and certified, after all?</p><p>Even if it wasn&#39;t for SEO, you probably want to offer a platform as secure as possible for your users. So no reason not to meet this criterion!</p><p>Learn more: <a href="https://ahrefs.com/blog/http-vs-https-for-seo/">HTTP vs. HTTPS for SEO: What You Need to Know to Stay in Google’s Good Graces</a> by ahrefs.</p><h4>→ Page speed 🚀</h4><p>People&#39;s attention span is short—searchers are quick to abandon slow-loading pages. Google knows that and, to offer the best UX to searchers, will penalize slow sites.</p><p>Googlebots themselves are pretty impatient and won&#39;t wait for a script <a href="https://www.elephate.com/blog/ultimate-guide-javascript-seo/">longer than 5 seconds</a>. If you don&#39;t meet this timeout, you risk not getting your content rendered adequately. Speed it up!</p><p>Learn more: <a href="https://moz.com/learn/seo/page-speed">On-Site SEO: Page Speed</a>, by Moz.</p><h4>→ Sitemap 🗺️</h4><p>A sitemap act as a map of your site&#39;s architecture for searchbots. Included in it should be the pages you consider to be good-quality landing &amp; navigation pages, worthy of indexation.</p><p>Sitemaps might not be that useful for smaller websites, but it&#39;s still a valuable SEO tool to consider.</p><p>Learn more: <a href="https://moz.com/blog/xml-sitemaps">XML Sitemaps: The Most Misunderstood Tool in the SEO&#39;s Toolbox</a>, by Moz.</p><blockquote>You can generate a sitemap.xml by <a href="https://github.com/40818419/vue-router-sitemap">vue-router configuration</a>.</blockquote><h4>→ Link building ⛓️</h4><p>Building your domain authority remains a key SEO tactic. How does Google know that you&#39;ve become an authoritative resource? By having other relevant domains linking to yours.</p><p>There&#39;s no secret formula here, <strong>to accomplish this you need to work hard at crafting great content</strong>. Content that others want to share and use as a resource on their own site!</p><p>The more links you get from relevant sources, the more your authority will rise, the more you&#39;ll earn Google favors when it comes to rankings.</p><p>Learn more: <a href="https://backlinko.com/link-building">Link Building for SEO: The Definitive Guide</a>, by Backlinko.</p><h3>Specific Vue.js SPA SEO issues</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9cv6KmHCAFl7vjjN.gif" /></figure><p>For the following tutorial, I decided to use the blog demo we made in an earlier post because, honestly, it looks awesome. If you&#39;re interested in knowing how it was built, see <a href="https://snipcart.com/blog/vuejs-blog-demo">this post</a>.</p><p>Although it&#39;s a great piece of work visually, it&#39;s not set up ideally SEO-wise. It&#39;s a complete SPA that search engines might have a hard time crawling &amp; indexing.</p><p>Why is that?</p><p>A single-page application adds content to pages dynamically, which has its benefits, but brings two significant issues:</p><ol><li><strong>We don&#39;t really know to which extent Google is able to crawl and correctly render JS</strong>. They&#39;ve been saying for a few years now that Googlebots are capable of doing so. However, these claims are always followed by a &quot;but&quot; or two.</li><li><strong>Google isn&#39;t the only search engine out there</strong>. If things are still shaky on its side, we know for a fact that the other players out there don&#39;t crawl JS yet, thus not encountering the actual content of the page.</li></ol><p>So there&#39;s no way around it. If you want to make sure your Vue.js website/app ranks on SERPs, you have to act on it.</p><p>Your options?</p><h4>Server-side rendering</h4><p>With a SSR setup, you have the rendering logic done directly in the backend, in a Node.js environment. HTML views are then returned to the client, ready to be served to search bots.</p><p>It&#39;s excellent for time-sensitive apps where you want to offload as much logic as possible on the server, but comes with a cost. You&#39;re going to need a robust infrastructure to handle the stress added to the server, asking for more development time. You might also slow down your system in the process.</p><p>But if you can handle it, it&#39;s definitely the way to go for bigger apps. <a href="https://nuxtjs.org/"><strong>Nuxt.js</strong></a> is without a doubt the tool to use for your <a href="https://snipcart.com/blog/cockpit-cms-tutorial-nuxtjs">server-rendered Vue.js SPA</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/0*ZoMDPQ-Mu42vUWXg.png" /><figcaption>Nuxt.js rendering process <a href="https://nuxtjs.org/guide">[source]</a></figcaption></figure><h4>Prerendering</h4><p>Sometimes though, server-side rendering might feel overkill, like in my demo&#39;s case. For a small SPA with only a few pages, prerendering will do the trick just fine. Even more so if your only concern is SEO.</p><p>This way, there&#39;s no need to attach your Vue.js app to any server. Rendering is done client-side with the use of <a href="https://snipcart.com/blog/vue-js-plugin">third-party plugins</a> such as:</p><ul><li><a href="https://prerender.io/"><strong>Prerender.io</strong></a> - Compatible with all the most popular JS frameworks, including Vue.js.</li><li><a href="https://github.com/chrisvfritz/prerender-spa-plugin"><strong>prerender-spa-plugin</strong></a> - A webpack plugin that will compile your pages into static pages. Makes all the content available in the source, and indexing is a breeze.</li></ul><p>The latter is very easy to use and was build by a Vue.js core team member, so it suits my use case perfectly here.</p><h3>Vue.js SEO technical example using prerendering</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xKIjFGbO6zPzv6yd.jpg" /></figure><p>Time to fix the Vue.js blog shown earlier. Using <a href="https://github.com/chrisvfritz/prerender-spa-plugin">prerender-spa-plugin</a>, I&#39;ll have a Vue.js SPA that&#39;s in Google&#39;s good graces in no time.</p><p><strong>Prerequisites</strong></p><ul><li>Basic knowledge of Vue.js</li><li>Node.js &amp; npm installations</li></ul><h4>1. Installing prerender-spa-plugin</h4><p>Configuring the plugin is very easy. In this setup, I&#39;ll run the plugin only when building for production. You don&#39;t need prerendering when actively developing components.</p><p>Start by installing the plugin from npm.</p><pre>npm install prerender-spa-plugin</pre><p>Then, open the build/build.js file. There, you&#39;ll find custom things for production build. You&#39;ll need to import the plugin and add these lines at the top of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ef89ad241d12cd3bd69b29dc3ceb793/href">https://medium.com/media/1ef89ad241d12cd3bd69b29dc3ceb793/href</a></iframe><p>Why PrerenderSPAPlugin.PuppeteerRenderer? Because you&#39;ll need to customize the renderer. You need to inject some data to let the application know when it&#39;s being prerendered. There are things like Disqus comments that you won&#39;t be able to prerender.</p><blockquote>In a 100% SEO-friendly app it would be preferable to benefit from these comments as rendered content on your pages. If your working with a static site generator, you should consider <a href="https://staticman.net/">Staticman</a> for static user-generated content.</blockquote><p>You&#39;ll need to fetch all your blog entries and generate a list of routes that will be prerendered. In a real production scenario, you&#39;d probably want to call your headless CMS API or an external service that provides your content. In this case, the data lives in simple JSON files.</p><p>Read the feed.json file and generate your routes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aae15768114bdb9dbc990d082bba31f/href">https://medium.com/media/0aae15768114bdb9dbc990d082bba31f/href</a></iframe><p>Add the plugins to webpack configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58d9f7ccc18223aac203015ab329d59d/href">https://medium.com/media/58d9f7ccc18223aac203015ab329d59d/href</a></iframe><p>You&#39;ll have access to the window.__PRERENDER_INJECTED.prerendered variable when the site is prerendering. This will be useful to exclude content you don&#39;t want to prerender.</p><p>You also need to specify the wait for the app.rendered event.</p><p>In this demo, I&#39;ll use <a href="https://github.hubspot.com/pace/docs/welcome/">Pace</a>, which is an awesome library to add a progress bar on a site quickly. However, you need to make sure the loading is complete before prerendering the site if not, you&#39;ll end up getting a random progress bar on multiple pages.</p><h4>2. Configuring Vue.js for prerender</h4><p>You&#39;ll then need to apply some minor changes in some components. To do so, I changed the way Pace was started. I got rid of the startup.cs file and put everything in main.js.</p><p>Open the main.js file and change how your App components are included:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ae9db020e672c0e70095319aa7dc317/href">https://medium.com/media/6ae9db020e672c0e70095319aa7dc317/href</a></iframe><p>Once your main component is mounted, start Pace and dispatch the app.rendered event when initial loading is completed.</p><p>The last update you have to do is to make sure Disqus is not loaded when the site is being prerendered.</p><p>Open BlogPost.vue file where you&#39;ll include the Disqus component.</p><p>Because of the way the prerender-spa-plugin was configured at first, you&#39;ll have access to a variable that indicates you that the site is being prerendered.</p><p>Update the showComments method this way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39ce1d2498ea78ab0adc00f63c903cba/href">https://medium.com/media/39ce1d2498ea78ab0adc00f63c903cba/href</a></iframe><h4>3. Building your SEO-friendly Vue.js site</h4><p>You&#39;re now ready to build your site. In your terminal use this command:</p><pre>npm run build</pre><p>Then, if you look at the dist folder, you should see all the posts in the read folder. A static HTML file has been generated for each post:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/0*peLNQKzmocTTEZ6D.png" /></figure><p>That&#39;s it! How easy was that, right?</p><h3>Live demo, Github repo</h3><blockquote><em>See the live demo </em><a href="https://vue-blog-prerendered.netlify.com/"><em>here</em></a></blockquote><blockquote><em>See GitHub repo </em><a href="https://github.com/snipcart/vue-blog-demo"><em>here</em></a></blockquote><h3>Closing thoughts</h3><p>prerender-spa-plugin is a neat plugin. It&#39;s effortless to configure and flexible enough to handle async use cases like the one we had with Pace.</p><p>I spent about 2 hours to figure out how to wire all these things together and update our initial application to support prerendering.</p><p>As I mentioned earlier, the plugin is straightforward and best suited for simple applications like a static blog. If you want to build something more complicated, I&#39;d suggest you look at Nuxt instead!</p><p><em>If you enjoyed this post, please take a second to share it on Twitter. Got comments, questions? Hit the section below!</em></p><p><em>I originally published this post on </em><a href="https://snipcart.com/blog/vue-js-seo-prerender-example"><em>the Snipcart blog</em></a><em> and shared it in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c160e0282eaf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/vue-js-seo-friendly-spas-tips-tools-prerender-example-c160e0282eaf">Vue.js SEO-Friendly SPAs: Tips, Tools &amp; Prerender Example</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Build & Host a (Very Fast) Hugo Static E-Commerce Site]]></title>
            <link>https://codeburst.io/how-to-build-host-a-very-fast-hugo-static-e-commerce-site-3554ba0eb802?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3554ba0eb802</guid>
            <category><![CDATA[ecommerce-web-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Sat, 08 Sep 2018 19:44:56 GMT</pubDate>
            <atom:updated>2021-03-03T14:33:01.006Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ovxiChVYUpIO9jnYFDSn0w.jpeg" /></figure><p>Time to dive into the fast-moving world of <a href="https://jamstack.org/">JAMstack</a> &amp; static web development once again! Our previous posts on handling <a href="https://snipcart.com/blog/ecommerce-checklist">e-commerce</a> with static site generators such as <a href="https://snipcart.com/blog/static-site-e-commerce-integrating-snipcart-with-middleman">Middleman</a> and <a href="https://snipcart.com/blog/static-site-e-commerce-part-2-integrating-snipcart-with-jekyll">Jekyll</a> were quite successful, so why stop there, huh?</p><p>Ladies and gentlemen, today, we’re going to prove how easy it is to set up e-commerce on static sites once again. And this time, we’ll use an in-depth <a href="https://gohugo.io/">Hugo</a> website tutorial to do so. :)</p><p>The following guide will show you:</p><ol><li>How to build your static site using Hugo’s site generator;</li><li>How to (easily) integrate Snipcart’s shopping cart platform on top of it;</li><li>How to deploy your Hugo e-commerce site on Netlify.</li></ol><p>But first, a word on the pivotal tool we’re going to use throughout.</p><h4>Hugo: a FAST Golang static site generator</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*byWW6DWjPd8Q-jWOTSUj9g.png" /></figure><p><strong>Hugo</strong> could mean different things to different people. Bookworms might think of Les Misérables’ legendary author. Cinephiles might think of the little boy in Scorsese’s 2011 film. But if you’re a <strong>developer, </strong>here’s what you should be thinking: <strong><em>blazing</em></strong><em> fast &amp; modern static website engine</em>.</p><p>Written in <a href="https://golang.org/">Go</a> by Steve Francia aka <a href="https://twitter.com/spf13"><strong>spf13</strong></a>, Hugo comes off as one of the most efficient ways we’ve seen to build, manage and update modern static sites. It’s easy to install on any platform, plus you can host it anywhere — we suggest <a href="https://www.netlify.com/blog/2016/09/21/a-step-by-step-guide-victor-hugo-on-netlify/">Netlify</a>, as you’ll see later. And its build times are just off the charts (~1 ms per page).</p><p>Today, I’ll show you how to use Snipcart and Hugo to build an old school Star Trek shop on a static site. Why Star Trek, you ask? Because we <a href="https://snipcart.com/blog/integrating-snipcart-with-kirby-cms-to-enable-e-commerce">already did Star Wars</a>.</p><blockquote>Psst<em>: still wondering what the heck static site generators are and why they matter? Give </em><a href="https://davidwalsh.name/introduction-static-site-generators"><em>Eduardo Bouças’ intro post</em></a><em> a read.</em></blockquote><h4>Hugo tutorial: site, products, templates &amp; deployment</h4><h4>1. Installing Hugo &amp; building your new static website</h4><p>First of all, you need to install the generator on your computer and create a new website. It’ll take you maybe 10 minutes, following the <a href="https://gohugo.io/getting-started/quick-start/">Hugo Quickstart documentation</a>.</p><p>Once you’ve downloaded the appropriate version on <a href="https://github.com/gohugoio/hugo/releases">Hugo’s GitHub repo</a>, installation is a breeze (as explained in the docs above). So let’s focus on creating the new Hugo site.</p><p>We’ll use the appropriate CLI command to do just that:</p><pre>hugo site new snipcart-hugo</pre><p><strong>Architecture</strong></p><p>This command will generate a basic skeleton for your project. You should have a site directory that looks like this:</p><pre>│ config.toml<br>│<br>├───archetypes<br>├───content<br>├───data<br>├───layouts<br>├───static<br>└───themes</pre><p>The config.toml file will contain the site settings. For our demo, we won&#39;t need to explore this too much, as we&#39;ll be doing something fairly simple with the site itself.</p><p>It’s not necessary to go too deep into <a href="https://gohugo.io/getting-started/">the inner workings of Hugo here</a>. Basically, during this tutorial, we’ll be creating files in the data folder, which is used to store additional data that might be used to generate the site.</p><p>We’re also going to add some templates in the layouts folder, which is the default location to store Hugo templates.</p><p>The static folder can be used to store any static assets such as CSS, JavaScript files or images. In our demo, we&#39;ll add an images folder containing our product images.</p><p>Of course, we advise you to get a little familiar with the Hugo documentation before looking into a full-fledged Snipcart integration.</p><p><strong>Themes</strong></p><p>We also decided not to install any specific theme for this demo (we’ll use a CSS framework to style our site later on), but there are many open source themes available. <a href="https://code.tutsplus.com/tutorials/make-creating-websites-fun-again-with-hugo-the-static-website-generator-written-in-go--cms-27319">This post</a> discusses how to set up themes on your Hugo website, so you might want to give it a read. It also explores basic site creation with Hugo in more details (Hello World, blog, photo gallery, etc.).</p><p>You can also check out the official repository for some of the best Hugo themes <a href="http://themes.gohugo.io/">here</a>.</p><h4>2. Creating a static JSON file for our store’s products</h4><p>Okay! Now, let’s set up our products: a Klingon dictionary and a phaser. We <em>could</em> have used a headless or static CMS for this part (we’ve <a href="https://www.siteleaf.com/blog/jamstack-ecommerce/">done it</a> <a href="https://www.contentful.com/blog/2016/02/10/snipcart-middleman-contentful/">before</a>). But for the humble purpose of this post, we’re going to create a static .json file to contain our products.</p><p>Hugo provides a very neat function called getJSON. It can be very handy when some of your data comes from either a headless CMS or any API that returns JSON.</p><p>As our JSON file is directly in the data folder we could have used .Site.Data.Products instead of calling the getJSON method, but here we wanted to show that it&#39;s also possible to interact with a remote API.</p><p>We’ll need to put a new file named products.json in the data folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/775a5438d15a069b604f3e6cb6e02a8b/href">https://medium.com/media/775a5438d15a069b604f3e6cb6e02a8b/href</a></iframe><h4>3. Generating your Hugo templates</h4><p>Next step is to set up the different layouts for our site. The most important is the header template, where we’ll add the <a href="https://docs.snipcart.com/getting-started/installation">Snipcart dependencies</a>.</p><p>We’ll also create a main template where we’ll loop inside of our products to show a summary and add a Snipcart “buy button”.</p><p><em>Note: Snipcart products are defined directly in the HTML markup with simple data attributes. </em><a href="https://docs.snipcart.com/configuration/product-definition"><em>Details here</em></a><em>.</em></p><p>In the layouts folder, we’ll put a new <strong>index.html</strong> template. This file will be used as default and will be the first one generated by Hugo.</p><p><strong>layouts/index.html</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2131908dc35f39bc94c460e97eab9c61/href">https://medium.com/media/2131908dc35f39bc94c460e97eab9c61/href</a></iframe><p>At the beginning of the post, we wrote about the getJSON method. We&#39;re going to use this in our index.html template.</p><p>We’ll retrieve products from the JSON file defined earlier. Then, we’ll loop through the product and render the product.html partial template.</p><p>As you can see, we also import a <strong>header.html</strong>, <strong>footer.html</strong> and <strong>product.html</strong> file. Let’s take a detailed look at these. Before going further, we’ll head to the layouts folder again and create a “partials”. If partials file aren’t put in this folder, Hugo won’t be able to recognize them as partial templates and the {{ partial … }} syntax won’t work at all. The other important thing to know about this file is the dot “.” after product.html. It means that you include the current product data in the product.html template.</p><p><strong>layouts/partials/header.html</strong></p><p>Like mentioned above, this file is the most important one. It’s a simple HTML header file with Snipcart dependencies. Place it in the partials folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac3042f9260a766b9e99494632b45d76/href">https://medium.com/media/ac3042f9260a766b9e99494632b45d76/href</a></iframe><p>We decided to use the <a href="http://materializecss.com/">MaterializeCSS</a> framework to style this static e-commerce demo, but you could use any other CSS framework. I found this one quite easy to integrate, and it provided enough built-in components to put together something that looks neat!</p><p>You’ll also notice that Snipcart’s required files are included in this template and that we added a <a href="https://docs.snipcart.com/getting-started/the-cart#adding-a-cart-summary">cart summary</a> to give customers a shortcut to their ongoing order.</p><p>So! Next step: putting together the footer partial template, to complete the core of our HTML file.</p><p><strong>layouts/partials/footer.html</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc6da54ba8e80b6a7c497a963ec7ac1f/href">https://medium.com/media/cc6da54ba8e80b6a7c497a963ec7ac1f/href</a></iframe><p>Finally, we need to generate the template displaying our Star Trek product details. Let’s name the file <strong>product.html</strong>.</p><p><strong>layouts/partials/product.html</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b160cf3a80b970cd2f316ed8311490b/href">https://medium.com/media/7b160cf3a80b970cd2f316ed8311490b/href</a></iframe><p>Since we passed the current product in our <strong>index.html</strong> template, we can now use every data fields in our <strong>JSON</strong> file. Here, I used them to populate my Snipcart buy button and add the product title + description.</p><p>Time to start our Hugo server and test that fancy website!</p><pre>hugo server</pre><p>(I’m saving my Star Trek shop screenshot for the end; brace yourselves)</p><h4>4. Setting up Hugo deployment on Netlify</h4><p>Last but not least: hosting the whole thing!</p><p>We decided to deploy our Hugo demo using the amazing service by our friends at <a href="https://netlify.com/">Netlify</a>.</p><p>Before doing anything in Netlify, I suggest creating a .gitkeep file in your content folder. This folder is required by Netlify&#39;s build bot. And as we did not lodge any files in this folder, Git is going to dismiss it.</p><p>Once the .gitkeep file is in place, you can use their interface to easily deploy your website in a few seconds. Here&#39;s a sneak-peek of our old school Star Trek shop deploy configuration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*hUMlB-Lt8r1bfPcFQsYr8A.png" /></figure><p>Netlify will automatically pull your code on GitHub and deploy your website. And that’s it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*OFXzpqp-0dsHeh8P.jpg" /></figure><h4>Live Hugo website example + GitHub repo</h4><p>So, time to reveal our Star Trek masterpiece, folks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/0*3Ei9ACDEC8QXybL7.png" /></figure><p>Is your mind blown or what? Now go check both the site and the code out for yourself:</p><blockquote><em>See </em><a href="http://snipcart-hugo.netlify.com/"><em>live Snipcart + Hugo demo</em></a></blockquote><blockquote><em>See </em><a href="https://github.com/snipcart/snipcart-hugo-integration"><em>GitHub code repo</em></a></blockquote><h4>Conclusion &amp; further resources</h4><p>I think our job here is done, friends!</p><p>In case you’re wondering if the end result is speedy enough, you can use another cool Netlify tool: <a href="https://testmysite.io/">Testmysite.io</a>. We scored 87/100 with the demo; not too shabby.</p><p>BTW, if you’re building a serious/client JAMstack site, you might want to consider monitoring its performance with <a href="https://speedtracker.org/">this free open source tool</a>. Also, tech-savvy teams might want to look into <a href="https://www.keybits.net/post/publishing-workflow-for-teams-using-static-site-generators/">this publishing workflow with Hugo</a>.</p><p><strong>Clients</strong></p><p>Think dealing with Markdown-driven static content files won’t cut it for clients? Some cool tools can help edit and manage content on top of Hugo. We suggest throwing one of the following static CMS into the mix:</p><ul><li><a href="https://forestry.io/">Forestry.io</a></li><li><a href="https://www.datocms.com/">DatoCMS</a></li><li><a href="https://appernetic.io/">Appernatic</a></li><li><a href="https://github.com/netlify/netlify-cms">Netlify CMS</a></li></ul><blockquote><em>For further reading regarding JAMstack tools for clients, limits, &amp; benefits, </em><a href="https://snipcart.com/blog/jamstack-clients-static-site-cms"><em>check out this comprehensive guide</em></a><em>.</em></blockquote><p>Playing with Hugo was a joy. Its documentation was on point, and its near-instantaneous speed made the engineer in me smile every time I re-built my site. Crafting this Snipcart + Hugo website tutorial took me around two hours. And that’s including styling the site with MaterializeCSS and hosting it on Netlify.</p><p>It always delights me to see how good a match modern site generators are for our HTML/JS shopping cart. :)</p><p>Now stop reading this blog and go build something awesome.</p><p><em>Got any questions regarding this Snipcart + Hugo tutorial? Any other static site generators you’d like covered? Hit the comments for any questions, suggestions or casual Klingon talk. And if you enjoyed this post, take a second to 👏 + </em><a href="https://twitter.com/home?status=Steps,%20Code%20Snippets%20%26%20Repo%3A%20Creating%20an%20Ecommerce%20Static%20Site%20w/%20%40spf13%27s%20Hugo%20%20http%3A//buff.ly/2moYM0U%20%23JAMstack%20%23staticweb%20%23gohugo"><em>share it on Twitter</em></a><em>!</em></p><p><em>Originally published on the </em><a href="https://snipcart.com/blog/hugo-tutorial-static-site-ecommerce"><em>Snipcart blog</em></a><em> and in </em><a href="https://us5.list-manage.com/subscribe?u=c019ca88eb8179b7ffc41b12c&amp;id=3e16e05ea2"><em>our newsletter</em></a><em>.</em></p><figure><a href="http://bit.ly/codeburst"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i3hPOj27LTt0ZPn5TQuhZg.png" /></a></figure><blockquote>✉️ <em>Subscribe to </em>CodeBurst’s<em> once-weekly </em><a href="http://bit.ly/codeburst-email"><strong><em>Email Blast</em></strong></a><strong><em>, </em></strong>🐦 <em>Follow </em>CodeBurst<em> on </em><a href="http://bit.ly/codeburst-twitter"><strong><em>Twitter</em></strong></a><em>, view </em>🗺️ <a href="http://bit.ly/2018-web-dev-roadmap"><strong><em>The 2018 Web Developer Roadmap</em></strong></a><em>, and </em>🕸️ <a href="http://bit.ly/learn-web-dev-codeburst"><strong><em>Learn Full Stack Web Development</em></strong></a><em>.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3554ba0eb802" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/how-to-build-host-a-very-fast-hugo-static-e-commerce-site-3554ba0eb802">How to Build &amp; Host a (Very Fast) Hugo Static E-Commerce Site</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Middleman Tutorial: Static E-Commerce on a Ruby Site Generator]]></title>
            <link>https://codeburst.io/middleman-tutorial-static-e-commerce-on-a-ruby-site-generator-d6621ac88fa7?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6621ac88fa7</guid>
            <category><![CDATA[ecommerce-web-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[static-site]]></category>
            <category><![CDATA[jamstack]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Wed, 08 Aug 2018 13:51:08 GMT</pubDate>
            <atom:updated>2018-08-09T15:40:58.003Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LhiNh-dEaYkgTMlXyinS_w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/xdbDEZSXC3o?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Frederik Trovatten</a> on <a href="https://unsplash.com/search/photos/between?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>“Static” websites have skyrocketed in the last years. They’re fast, scalable, secure and cheap. <a href="https://www.smashingmagazine.com/2015/11/modern-static-website-generators-next-big-thing/">For years</a>, dynamic sites had the upper hand regarding the functionality argument. Today, an abundance of dedicated APIs can be added to your frontend projects, and developers can do anything they want with static websites. We call this the <a href="https://jamstack.org/">JAMstack</a>! Coupled with new content management tools (static/headless CMS), this means <a href="https://snipcart.com/blog/jamstack-clients-static-site-cms">serious benefits for clients</a>.</p><p>From <a href="https://snipcart.com/blog/ecommerce-wyam-dot-net-static-site-generator">.NET toolkits</a> to <a href="https://snipcart.com/blog/snipcart-reactjs-static-ecommerce-gatsby">React-powered tools</a>, there are LOTS of site generators available.</p><p>Today, we’ll focus on handling e-commerce on a static site with a <strong>Middleman</strong> tutorial.</p><h4>Middleman, a powerful Ruby site generator</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TSAx1-8CumHr63-N.png" /></figure><p><strong>Middleman is a Ruby site generator offering a modern frontend framework used by the likes of MailChimp &amp; Thoughtbot.</strong></p><p>We first heard about it when we <a href="https://snipcart.com/blog/featured-sit-down-with-denvers-fake-diamonds-clothing">sat down with Dillon Morton</a>, a Colorado web developer. He had used the app to enable e-commerce on his clothing brand’s static site and seemed happy about it. We had been eager to try it ourselves for a while!</p><h4>Under the hood: A word on Ruby and Ruby e-commerce</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/0*jcK-8jFIEOgZe8Gc.png" /></figure><p>We’re hardwired geeks at Snipcart, always curious as to how the tools we experiment with are built. In this case, what’s used to power Middleman is the now-famous <a href="https://en.wikipedia.org/wiki/Ruby_%28programming_language%29">Ruby</a>. Created by Yukihiro Matsumoto in the mid-90’s, Ruby is a dynamic, general-purpose programming language. Its popularity surged after <a href="http://david.heinemeierhansson.com/">a smart fella</a> built a world-class tool on top of it in 2003: <a href="http://rubyonrails.org/">Ruby on Rails</a>. In fact, some keep confusing the MVC framework, Rails, with its original language.</p><p>Even stripped from its household web framework, Ruby remains a very cool development tool. It’s both fun and fast to write, effectively decreasing your project’s time-to-market. A little slower than languages like Go, it also promotes a strong open source philosophy and is surrounded by a solid, supportive community. In a sense, it shares many pros &amp; cons with Python, which we touched on <a href="https://snipcart.com/blog/django-ecommerce-tutorial-wagtail-cms">previously</a>.</p><p>If you’re looking at building a Ruby online shop, you’ll likely stumble upon Ruby on Rails e-commerce solutions first. Apps like Shoppe, Spree Commerce, and Solidus can be great starting points. But in certain cases, their hosting &amp; backend requirements might be overkill.</p><p>In this post, we’re proposing a different approach. A modular, JAMstack approach. We’re going to leverage Ruby through Middleman, generating a static site, and then use our HTML/JS shopping cart to enable e-commerce.</p><h4>Snipcart &amp; Middleman tutorial: getting started</h4><p><strong><em>Pre-requisites</em></strong><em>:</em></p><ul><li>Ruby installed (<a href="https://www.moncefbelyamani.com/how-to-install-xcode-homebrew-git-rvm-ruby-on-mac/">Mac install</a>, <a href="https://rubyinstaller.org/">Windows install</a>)</li><li><a href="https://guides.rubygems.org/">RubyGems</a> working</li><li>Min. knowledge of Middleman (<a href="https://middlemanapp.com/basics/install">documentation</a>)</li><li><a href="https://app.snipcart.com/register">Free Snipcart account</a> in Test mode</li></ul><p>First, install Middleman:</p><pre>gem install middleman</pre><p>In your project folder, you will need to init a new Middleman app using their CLI:</p><pre>middleman init</pre><p>Middleman will create the basic project structure: a source folder and some other files. The source contains templates, stylesheets, and any other assets.</p><p>To start a development server, just do:</p><pre>middleman server</pre><p>Your website will then run on a local server. Simply hit the local URL you see in the terminal.</p><h4>1. Adding Snipcart products to Middleman</h4><p>For this demo, we’ll use a simple yml file as our datastore. <a href="https://middlemanapp.com/advanced/data_files/">Middleman works great with this</a>, and it&#39;ll be easy to deploy/update.</p><p>At the root of your project, create a data folder named products.yml.</p><p>We can now add products with <a href="https://docs.snipcart.com/configuration/product-definition">Snipcart’s required properties</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b888469692e7fe73ab914b0b8d0c3be3/href">https://medium.com/media/b888469692e7fe73ab914b0b8d0c3be3/href</a></iframe><p>This data will be available in your Middleman templates using data.products.</p><h4>2. Setting up Middleman layout page with Bulma</h4><p>For styling this demo, I decided to use <a href="http://bulma.io/">Bulma</a>. It’s a kickass CSS framework based on Flexbox, which makes it easy to build good-looking sites. We’ll reference Bulma using their CDN server.</p><p>In our layout.erb file, we&#39;ll add the following lines to include Bulma and jQuery:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50ad78071feef678687236d26d354e25/href">https://medium.com/media/50ad78071feef678687236d26d354e25/href</a></iframe><p>Let’s first put a simple nav bar. Add these lines to the body tag just before &lt;%= yield %&gt;:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2dfe1cd5ed47f2b199cb7902fd1f9fd/href">https://medium.com/media/c2dfe1cd5ed47f2b199cb7902fd1f9fd/href</a></iframe><p>We’ll then add a footer in the layout page, just before closing the &lt;body&gt; tag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71887f9bea47158a505a7acb4903a7e9/href">https://medium.com/media/71887f9bea47158a505a7acb4903a7e9/href</a></iframe><p>And now let’s add a cart summary in the nav section to let customers access their cart from any page. This link will contain the current number of items in the cart. To do so, include Font Awesome to have access to some icons. Here we’ll just add their asset through a CDN in the layout file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3376f80dfdb3972df7ecf97c2bccb90c/href">https://medium.com/media/3376f80dfdb3972df7ecf97c2bccb90c/href</a></iframe><p>Of course, we also have to include <a href="https://docs.snipcart.com/getting-started/installation">Snipcart’s required files</a>. <a href="https://app.snipcart.com/">Log in</a> to your Snipcart dashboard, go to <em>Account Menu &gt; API Keys</em> and copy the code snippet. We’ll add it to the layout just after Bulma and jQuery.</p><p>We could just copy/paste it, but let’s stay consistent and use Middleman helpers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/724c46a1d451cb9771612c1b6aed6e48/href">https://medium.com/media/724c46a1d451cb9771612c1b6aed6e48/href</a></iframe><p>We’ll need to define an environment variable somewhere with our Snipcart API Key. Since I’ll use <a href="http://netlify.com/">Netlify</a> for deployment, I’ll add my variable in their dashboard, but you could just add it to your config.rb file.</p><pre>ENV[&quot;snipcart-api-key&quot;] = &quot;YOUR_API_KEY&quot;</pre><p>We’ll also need another environment variable to keep the base site URL. I set it in Netlify as well, but while developing I added this:</p><pre>ENV[&quot;base-url&quot;] = &quot;http://localhost:4567&quot;</pre><p>This will make sure Snipcart’s <a href="https://docs.snipcart.com/getting-started/security">crawlback validation</a> works as expected.</p><h4>3. Setting up your static site’s index</h4><p>On the main page, we’ll display all our products. Here, we can completely remove the content of the index.html.erb file in Middleman.</p><p>Start by adding the page title using yml markup:</p><pre>--- title: My awesome store ---</pre><p>Then, add some code to make a nice header on the site:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61fe0489dcc75fb4f861311b6cf05f7d/href">https://medium.com/media/61fe0489dcc75fb4f861311b6cf05f7d/href</a></iframe><p>We now want to display all products by getting them from the products.yml, and Middleman makes it very easy.</p><p>You can loop through your products using the data object.</p><pre>&lt;% data.products.each do |p| %&gt; &lt;%= p.name %&gt; &lt;% end %&gt;</pre><p>Let’s use the Bulma <a href="http://bulma.io/documentation/components/card/">Card component</a> combined to their <a href="http://bulma.io/documentation/grid/columns/">grid system</a> to show products.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c418049ab98b42758b13f235bc7089a2/href">https://medium.com/media/c418049ab98b42758b13f235bc7089a2/href</a></iframe><p>Please note the snipcart_button helper. We&#39;ll get back to that later, but it&#39;s basically going to be a helper to render a buy button with Snipcart&#39;s required attributes.</p><h4>4. Creating a product details page for your store</h4><p>First step here is to create a new erb template. Let&#39;s call it product.html.erb. Put it at the root of the source folder.</p><p>This template will display the product details and the Snipcart buy button.</p><p>For this tutorial, we’ll come up with a simple two columns template with the product image, Snipcart button, product name, and its description.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0453cdf7d31d878cb6bee3a413878a71/href">https://medium.com/media/0453cdf7d31d878cb6bee3a413878a71/href</a></iframe><p>Notice the snipcart_button product line here? Unfortunately, it won&#39;t magically work; we have a little extra step to do: creating a template helper to generate a Snipcart button.</p><p>Add this to your config.rb:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b06f8205cd4ef44fa52a9cb03b71b6dd/href">https://medium.com/media/b06f8205cd4ef44fa52a9cb03b71b6dd/href</a></iframe><p>With this, our add to cart button will show with product info when we call snipcart_button inside a template.</p><p>Now that we have a template and a helper to generate the Snipcart button, we need to be able to display it when the product URL is called. To do so, we have to use a Middleman proxy. To learn more about this, read <a href="https://middlemanapp.com/advanced/dynamic_pages/">this docs section</a>.</p><p>Add this to your config.rb:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6913b7faf4180ce6787f59338f363c8b/href">https://medium.com/media/6913b7faf4180ce6787f59338f363c8b/href</a></iframe><p>When defining products in products.yml, we specified a path. This path will be used as the unique URL to access the resource.</p><p>Now, open your browser (make sure your Middleman server is started) and hit a product page such as http://localhost:4567/products/chocolate-chip-cookies/. You should now see the product and be able to add it to your cart!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NK0mSHfUQEvHlmmZ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-2VpJMjljlFRkpND.png" /></figure><h4>5. Deploying your static e-commerce site using Netlify</h4><p><a href="https://www.netlify.com/">Netlify</a>’s an awesome product developed by talented developers we met a while ago. It allows developers to focus on the code while it <strong>builds, deploys and hosts their static sites</strong>. We’ll use it to deploy our Middleman demo.</p><p>First thing I suggest you do is create a file named .ruby-version at the root of your project. Netlify will use the version of Ruby specified there to build your site. As we are using the version 4.2.0 of Middleman, we must use a recent version of Ruby. I decided to use the 2.3.1 version. The created file will only contain this information.</p><p>In your Netlify dashboard, click on <em>Add a new project</em> and follow the steps to link your GitHub repository.</p><p>After that, Netlify will automatically detect that it’s a Middleman project and suggest a config. Just hit <em>Build your site</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*OaaweDpa1eAD1l3Q.png" /></figure><p>My site’s now deployed and blazing fast!</p><h4>GitHub repo &amp; live e-commerce demo</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*48IKSZw_RHDHhZvs.png" /><figcaption><em>Peep my “not-so-bad for a former backend guy” design. Thanks again, Bulma.</em></figcaption></figure><blockquote><a href="https://snipcart-middleman.netlify.com/"><em>See the live demo</em></a></blockquote><blockquote><a href="https://github.com/snipcart/snipcart-middleman-demo"><em>Check out the full GitHub repo</em></a></blockquote><h4>Closing thoughts</h4><p>Tools like Middleman and Netlify make building and deploying static websites more efficient than ever. Except getting Ruby to work A1 on my Windows machine, everything went smoothly while crafting this tutorial with Middleman. It took me about 4 hours, including deployment and learning Bulma basics. With more time, I could’ve put together stuff like a bigger inventory, multiple images per product, and advanced cart customization.</p><p>We hope this post gave you a good idea of how you can handle e-commerce or dynamic features on static sites!</p><p><strong>Check out all of our JAMstack tutorials </strong><a href="https://docs.snipcart.com/getting-started/guides-and-tutorials#jamstack-static-site-generators-amp-frameworks"><strong>here</strong></a><strong>.</strong></p><p><em>Have you used any of the tools mentioned in the post? If so, I’d love to hear your thoughts in the comments. And if you found the article valuable, feel free to 👏 or share it on Twitter!</em></p><p>I originally published this on <a href="https://snipcart.com/blog/static-site-e-commerce-integrating-snipcart-with-middleman">the Snipcart Blog</a> and shared it in our <a href="http://eepurl.com/cDZnQ9">newsletter</a>.</p><figure><a href="http://bit.ly/codeburst"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i3hPOj27LTt0ZPn5TQuhZg.png" /></a></figure><blockquote>✉️ <em>Subscribe to </em>CodeBurst’s<em> once-weekly </em><a href="http://bit.ly/codeburst-email"><strong><em>Email Blast</em></strong></a><strong><em>, </em></strong>🐦 <em>Follow </em>CodeBurst<em> on </em><a href="http://bit.ly/codeburst-twitter"><strong><em>Twitter</em></strong></a><em>, view </em>🗺️ <a href="http://bit.ly/2018-web-dev-roadmap"><strong><em>The 2018 Web Developer Roadmap</em></strong></a><em>, and </em>🕸️ <a href="http://bit.ly/learn-web-dev-codeburst"><strong><em>Learn Full Stack Web Development</em></strong></a><em>.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6621ac88fa7" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/middleman-tutorial-static-e-commerce-on-a-ruby-site-generator-d6621ac88fa7">Middleman Tutorial: Static E-Commerce on a Ruby Site Generator</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build a Gatsby & GraphQL App on Top of a Headless CMS]]></title>
            <link>https://medium.com/hackernoon/build-a-gatsby-graphql-app-on-top-of-a-headless-cms-2eb3f78e1dc5?source=rss-1a55c912c9e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2eb3f78e1dc5</guid>
            <category><![CDATA[headless-cms]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[gatsbyjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[apps]]></category>
            <dc:creator><![CDATA[Charles Ouellet]]></dc:creator>
            <pubDate>Mon, 19 Feb 2018 21:28:32 GMT</pubDate>
            <atom:updated>2019-04-29T11:32:14.200Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6T4SCXPlODJ-4R3KfW7FDg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/dnPniNPUe4o?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Dhaval Parmar</a> on <a href="https://unsplash.com/search/photos/code?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Last year has been a very exciting year for devs like us.</p><p>A whole lot of promising up-and-coming <a href="https://hackernoon.com/tagged/technologies">technologies</a> have made their place in the <a href="https://hackernoon.com/tagged/spotlight">spotlight</a>.</p><p>Maybe even so much that it’s hard to keep up.</p><p>To stay sane I guess we all have our favorites to focus on .</p><p>My team and I are big fans of everything related to the <a href="https://snipcart.com/blog/jamstack-clients-static-site-cms">JAMstack</a>, <a href="https://snipcart.com/blog/cockpit-cms-tutorial-nuxtjs">headless CMSs</a> &amp; <a href="https://youtu.be/hhwAVZz3CWo">static site generators</a>.</p><p>So let’s keep moving this way and pushing new boundaries with some of the great tools we’ve explored in the last few months.</p><p>In this post I’ll show you how to:</p><ul><li>Set up <strong>Grav CMS</strong> as a headless CMS.</li><li>Install React-powered static site generator <strong>Gatsby</strong>.</li><li>Create a source plugin to query the API with <strong>GraphQL</strong>.</li><li>The result? A small demo shop powered by <strong>Snipcart</strong>.</li></ul><p>Sounds heavy?</p><p>Bear with me, it’s gonna be fun.</p><p>Let’s start by quickly introducing these tools.</p><h3>What is Grav CMS?</h3><p>Simply put, <a href="https://getgrav.org/"><strong>Grav</strong></a><strong> is a modern open-source flat-file CMS</strong>. It actually won the CMS Critic award for “Best open source CMS” in 2016.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v9QKJfXTkoTphTSG.png" /></figure><p>In this case, I’ll use it solely for content management, decoupling the backend from the frontend, or, as we like to call it, as a <em>headless CMS</em>.</p><p>Why bother doing so you may ask?</p><p>Mainly because it’s a great way to mix the backend capacities of the CMS with some of the amazing frontend JS frameworks out there (React in our case).</p><p><em>For more info, we’ve already covered all the benefits of working with headless CMS in </em><a href="https://snipcart.com/blog/intro-api-first-headless-cms-directus"><em>another blog post</em></a><em>.</em></p><p>I wanted to try Grav this time because of its nice and extensible admin panel which makes it a great choice to manage a simple website.</p><p>It’s also a very easy to install, easy to learn <a href="https://snipcart.com/blog/flat-file-cms-php-kirby-tutorial"><em>flat file CMS</em></a> that is a great alternative to heavier systems like WordPress or Drupal.</p><h3>React static site with Gatsby.. and GraphQL</h3><p>After I’ve got my decoupled CMS running, I’ll go on strapping the static site generator <a href="https://www.gatsbyjs.org/docs/netlify-cms/">Gatsby</a> on top of it.</p><p>Gatsby is the most popular static site generator powered by React. I’ve <a href="https://snipcart.com/blog/snipcart-reactjs-static-ecommerce-gatsby">already played with it</a> a little bit, but this time I’ll try to push it even further.</p><p>Mostly because I think it’s awesome and deserving of the exploration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/965/1*mkjZJQDDnEw4U_BWzcDZKQ.png" /></figure><p>Here I want to use one of Gatsby’s great features which allows us to bring our own data from databases, SaaS services, APIs and, as in our current case, headless CMSs.</p><p><strong>This data is pushed directly into our pages using GraphQL.</strong></p><p>In our first blog post on <a href="https://snipcart.com/blog/graphql-nodejs-express-tutorial">this new technology</a> we stated that GraphQL is probably more than just a trendy tool and that it’s here to stay.</p><p>It really seems to be the case as it’s probably going <a href="https://x-team.com/blog/top-javascript-trends-2018/">to get even bigger in 2018</a>.</p><p>It’s nice to see it getting implemented with great tools such as Gatsby and I’m really pumped to give it another try today.</p><p>Okay, this is gonna be a long one, so without further ado, let’s get to work!</p><h3>Grav as headless CMS tutorial</h3><h3>Grav installation</h3><p>Grav is very easy to install. I used composer to do so. Simply open a terminal and type this command:</p><pre>composer create-project getgrav/grav snipcart-grav-headless</pre><p>Then, we want to start a web server, I chose to go with the built-in PHP development web server. <em>Note that I am on Windows</em>.</p><pre>cd snipcart-grav-headless php -S localhost:8000 .\system\router.php</pre><p>This will start a web server on your machine with Grav running. You can open http://localhost:8000 in a browser and you should see it in action.</p><h3>Grav CMS as headless setup</h3><p>We now have a working Grav instance. The first thing we’ll do is to add their awesome admin panel.</p><p><em>One thing I really like about Grav is that everything is a plugin, even their admin panel. ;)</em></p><p>We’ll install it via their own package manager.</p><pre>php .\bin\gpm install admin</pre><p>Once the installation is completed, open your browser to http://localhost:8000/admin. You will have a small form to fill to create your initial user, then you&#39;ll be logged in.</p><p>We’ll also add another plugin, this plugin will be responsible to serve the content we’ll add in Grav in JSON. We’ll then be able to use this data in our Gatsby application, later in the tutorial.</p><p><em>To be honest, I don’t think Grav CMS is widely used this way yet. I’ve got a hard time finding a plugin that does the job and that had a bit of documentation.</em></p><p>I started by looking at <a href="https://github.com/getgrav/grav-plugin-feed">feed</a> from the Grav team, but, it wasn&#39;t exporting all the data I needed. This plugin is really intended to generate a feed file for RSS readers and things like that.</p><p>The one I took is <a href="https://github.com/btopro/page-as-data">page-as-data</a>. Although it does not seem to be maintained anymore, it does the trick. It generates a JSON file containing everything I need to use Grav as a headless CMS.</p><h3>Defining products in Grav</h3><p>The last step will be to define our product <em>blueprint</em>. A blueprint in Grav allows you to define the structure of the content. In our case, we’ll create one that will define what is a product with Snipcart required options.</p><p>For this step, I prefer to open the folder in VS Code (or any other editor) and edit it manually. You’ll need to go to your themes folder. In my case, I kept the default one as I won’t really use Grav to display anything.</p><p>So I had to create the file in: /users/themes/antimatter/blueprints.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe9cdc9fe7d94c6a326fd494094c3efc/href">https://medium.com/media/fe9cdc9fe7d94c6a326fd494094c3efc/href</a></iframe><p>This new blueprint will give us a new “content type” in Grav CMS, with some default fields that will keep product information within Grav.</p><h3>Adding content in Grav</h3><p>We now have everything needed to make Grav work as we expect. We’ll add some content.</p><p>We’ll start by creating a new page which is going to be the parent of each product we’ll add.</p><p>Log into the Grav admin panel, the URL should be: <a href="http://localhost:8000/admin.">http://localhost:8000/admin.</a></p><p>Go to the <strong>Pages</strong> section, and click on the <strong>Add</strong> button in the top right corner and create a new page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/0*N_8niw5DigXP1NMy.png" /></figure><p>We can keep the default Page template. As we won’t use Grav to render the content it doesn’t really matter. However, we’ll need to instruct the page about its content.</p><p>Open the page in <strong>Expert</strong> mode to get access to the <em>frontmatter</em> and paste this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1c8de5b400e4c17792a67be1272fbb7/href">https://medium.com/media/e1c8de5b400e4c17792a67be1272fbb7/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jm78GPsNE19CLBm4.png" /></figure><p>Once the page is created and configured, we’ll define the products.</p><p>Click on the Add button again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/0*N-2XYhwwbfWJOaIs.png" /></figure><p>Make sure to select the correct parent page and template.</p><p>Once the page is generated, you’ll get a form, which uses the fields we defined in our blueprint earlier in the post.</p><p>You can set the product attributes there and hit the Save button. <strong>Make sure you are in Normal mode, not Expert</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cQjgNLgfyRx2NBvi.png" /></figure><p>Now that we have come content, we can open this URL in the browser: <a href="http://localhost:8000/products?return-as=json.">http://localhost:8000/products?return-as=json.</a></p><p>The page-as-data plugin will catch this request and return a JSON object containing our data. This is exactly what we&#39;re going to use as the data source in our Gatsby project.</p><h3>Gatsby setup using GraphQL schema</h3><h3>Installing Gatsby</h3><p>Our first move is to install Gatsby. <a href="https://www.gatsbyjs.org/docs/">Their documentation</a> is well done and you should refer to it if you want more information.</p><p>Make sure you have the classic NodeJS stack installed and have access to npm and then open a command line.</p><p>We’ll install the gatsby-cli package, this package will expose a couple of commands related to Gatsby.</p><pre>npm install --global gatsby-cli</pre><p>Then, we’ll scaffold our new project:</p><pre>gatsby new gatsby-site</pre><p>This will clone the necessary files and install the default starter.</p><h3>Creating a source plugin to query our API</h3><p>In order to get access to our data in Gatsby, we’ll need to craft a source plugin. The plugin allows to “source” data into nodes, a concept built into Gatsby. These nodes will generate a GraphQL schema that will be able to query in our components.</p><p>I have to say, this is a very nice approach. I love the fact that I can use GraphQL in Gatsby in such a fun and simple way.</p><p>Start by creating a pluginsfolder at the root of your workspace. You could also work with a standalone NPM package if you&#39;d prefer, but I decided to use the simplest way for this example.</p><p>My tree looks like this (note that I ran npm init in the folder to create the package.json file):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa48f195042f7543e11c69046ae31fe8/href">https://medium.com/media/aa48f195042f7543e11c69046ae31fe8/href</a></iframe><p>You can consider gatsby-node.js as the index.js of the package. This is where the plugin code will start.</p><p>Our plugin will basically make an HTTP call to our /products endpoint we&#39;ve built with Grav. Then, it will use the response payload and constructs the nodes that will be used by Gatsby to generate our GraphQL schema.</p><p>We’re going to use two packages for this. The first one being <a href="https://github.com/axios/axios">axios</a>, a very well known HTTP client in the Node community.</p><p>Then, <a href="https://github.com/angeloashmore/gatsby-node-helpers">gatsby-node-helpers</a> which will be very useful to generate nodes. A node in Gatsby contains, amongst other things, some internal data that must be hashed. Using the helpers will save us a lot of code.</p><pre>npm install --save axios gatsby-node-helpers</pre><p>Our Gatsby plugin must export a function named: sourceNodes, this function will call our API and create nodes.</p><p>Add a file named gatsby-node.js in your project if you did not already, and add this code in it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/246e8c371a144edb18cd3d9eab8aab63/href">https://medium.com/media/246e8c371a144edb18cd3d9eab8aab63/href</a></iframe><p>I think the code is quite straightforward and simple so I won’t explain it step by step.</p><p>Once it’s done, we’ll have to let Gatsby know about our plugin.</p><p>Open gatsby-config.js file. This file should be in your workspace root. In the plugins array, add &#39;grav-headless-plugin&#39;.</p><p>My config file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b22f25cdbf6f1e2b397989eb8a0fabbf/href">https://medium.com/media/b22f25cdbf6f1e2b397989eb8a0fabbf/href</a></iframe><p>You can then launch your Gatsby development server:</p><pre>gatsby develop --port 8001</pre><p>Gatsby comes with a pre-configured <strong>GraphiQL</strong>.</p><p>It’s a very clean GraphQL client that enables you to execute queries directly in your browser. Navigate to http://localhost:8001/___graphql in your browser.</p><p>You can then run this query:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/216cd6c0371337c7c390c2434dfa5edd/href">https://medium.com/media/216cd6c0371337c7c390c2434dfa5edd/href</a></iframe><p>This will return a JSON object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b1dabe35b57b3bfbd93b902cda509c3/href">https://medium.com/media/3b1dabe35b57b3bfbd93b902cda509c3/href</a></iframe><p>This data comes from Grav and is now available on your Gatsby site!</p><p>Awesome isn’t it?</p><h3>Generating the site</h3><p>The first thing we’ll do is to add Snipcart dependencies.</p><p><em>To do so you’ll need a </em><a href="https://app.snipcart.com/register"><em>Snipcart account</em></a><em> (forever free in test mode).</em></p><p>Open layouts/index.js file and add these lines in the &lt;Helmet&gt; component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84d8e9966d386358e38a1af4101223f0/href">https://medium.com/media/84d8e9966d386358e38a1af4101223f0/href</a></iframe><p>This will add required files to your site and Snipcart will work when we’ll need it. :)</p><p>As we want to create a static site, we’ll need to make a page per product. Luckily, Gatsby has it covered and exposes some methods we can use to do so.</p><p>Basically, we’ll query all of our products via GraphQL, then we’ll generate a page dynamically for each product using a generic component.</p><p>To do so, open gatsby-node.js file at the root of your workspace (not the one from the plugin we made). This is where we&#39;ll generate the pages.</p><p>We want to run this GraphQL query and create a page for each result:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/259268bc50f48701c34f0482781a1928/href">https://medium.com/media/259268bc50f48701c34f0482781a1928/href</a></iframe><p>In the gatsby-node.js file, we can export a function that will create these dynamic pages. The function is named createPages.</p><pre>exports.createPages = async ({graphql, boundActionCreators}) =&gt; { }</pre><p>Copy the two lines above. Notice that we inject graphql that will be used to query our datastore and boundActionCreators that will be used to build the page.</p><p>We’ll first get our products:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a078c64001ac03487d392a2289c9f215/href">https://medium.com/media/a078c64001ac03487d392a2289c9f215/href</a></iframe><p>With these results, we’ll create the pages automatically. For this, we’ll need to require two dependencies, path which comes with Node and slash to deal with Windows file system correctly.</p><p>You’ll have to install this one via npm.</p><pre>npm install slash --save-dev</pre><p>Include these two dependencies in the top of your file:</p><pre>const slash = require(&#39;slash&#39;) <br>const path = require(&#39;path&#39;)</pre><p>Now we’ll use the createPage method available in boundActionCreators to generate the pages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07df7b1e77d2800e3f047b955ec5c0ed/href">https://medium.com/media/07df7b1e77d2800e3f047b955ec5c0ed/href</a></iframe><p>As you can see, we get all the products, then for each of them we create a page. We pass the product ID to the context, which will be available in the Product component data.</p><p>My whole gatsby-node.js file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf62c37fb60a5806956d9a6d2f080154/href">https://medium.com/media/cf62c37fb60a5806956d9a6d2f080154/href</a></iframe><p>Speaking of Product component, this is what we&#39;ll do next. This component will be used to render each product page.</p><p>Create a file named product.js in your src/components folder.</p><p>This file will contain the Snipcart buy button and general information about the product. I won’t dig too much into React and styling things, but here’s my component code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8104ddb6bad600eaa66496d0dcfd9c96/href">https://medium.com/media/8104ddb6bad600eaa66496d0dcfd9c96/href</a></iframe><p>Note that we use GraphQL there too to fetch the product by its ID. The id value comes from the value we set in the context while we were in gatsby-node.js file.</p><p><em>I used CSS modules to manage my components CSS. I suggest you read </em><a href="https://www.gatsbyjs.org/tutorial/part-two/#css-modules"><em>this excellent tutorial</em></a><em> on the matter.</em></p><p>We also imported the Link component. You&#39;ll want to use it to add link between pages generated by Gatsby.</p><p>Once your site is loaded, React router will take care of the routing and it will create a very fluid experience. No full reloads, only speed!</p><p>If you hit this page (make sure that you have your Gatsby dev server running with gatsby develop): http://localhost:8001/products/hat-hat you should see an awesome product page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/0*Kg9yUsAvVlwNeElo.png" /></figure><p>Notice the Back to products link? This is intended to redirect to our products page that does not exist yet.</p><p>Let’s craft a page for it, in the pages folder, create a new file named products.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/292bb7411523bf1dc0cc58f5c2f7782e/href">https://medium.com/media/292bb7411523bf1dc0cc58f5c2f7782e/href</a></iframe><p>As we did earlier, we again use GraphQL to query our products. Products will then be available in component data object and we&#39;ll be able to render each of the products.</p><p>If you go to http://localhost:8001/products you should now have an awesome product listing page. :)</p><h3>Live demo &amp; GitHub repo</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*EjUoFdZVBpzLXsAN.png" /></figure><blockquote><a href="https://github.com/snipcart/snipcart-gatsby-grav"><em>See the open source repo on GitHub</em></a><em>.</em></blockquote><blockquote><a href="https://snipcart-gatsby-grav.netlify.com/"><em>See live demo here</em></a><em>!</em></blockquote><h3>Closing thoughts</h3><p>I had a blast doing this tutorial!</p><p>I enjoyed the simplicity of Grav, it took me only a couple of minutes to get it running. The hardest part was to find the plugins I was going to use it as a headless CMS and to be honest, the plugin I used in the demo is kind of limited.</p><p>I think Grav might be a good solution for a very basic scenario, but is not as advanced as other options we looked at in previous posts such as Directus or GraphCMS.</p><p>That being said, I’m well aware that it is not the way most people use Grav. ;)</p><p>For the frontend part with Gatsby, it’s been a real delight to use once again. This really is an awesome piece of software. The documentation is on point and it’s very well executed.</p><p>The GraphQL implementation makes the whole experience even more special.</p><p>I haven’t touched to React a lot, only with <a href="https://snipcart.com/blog/reactjs-wordpress-rest-api-example">a few tutorials</a> a while ago. Still, Gatsby was easy to learn and made me want to dig deeper into React.</p><p><em>If you’ve enjoyed this post, please take a second to </em><a href="https://twitter.com/home?status=Gatsby%20with%20GraphQL%20Schema%20Tied%20to%20Grav%20as%20Headless%20CMS,%20via%20%40snipcart%20http%3A//bit.ly/2nfeQVd%20%40gatsbyjs%20%40gravcms%20%23reactjs%20%23graphql"><em>share it on Twitter</em></a><em>. Got comments, questions? Hit the section below!</em></p><p>I originally published this on <a href="https://snipcart.com/blog/react-graphql-grav-cms-headless-tutorial">the Snipcart Blog</a> and shared it in our <a href="http://eepurl.com/cDZnQ9">newsletter</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2eb3f78e1dc5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/build-a-gatsby-graphql-app-on-top-of-a-headless-cms-2eb3f78e1dc5">Build a Gatsby &amp; GraphQL App on Top of a Headless CMS</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>