Using the Google Sheets API With Gatsby for Creating Fast Sites With Simple Data Sources

August 02, 2019 0 Comments

Using the Google Sheets API With Gatsby for Creating Fast Sites With Simple Data Sources

 

 

I recently prototyped an Acroyoga-focused side project, called ‘AcroTags’, using Gatsby and the Google Sheets API. The site was as fun to build and populate with data as it is to use for discovering Acroyoga videos. This post will explore why and how I made this site and cover the specific code I used to get Gatsby and Google Sheets to work nicely together. I hope this tutorial and the code samples allow you to quickly prototype your next idea using this very simple and powerful stack.

Background on an Acroyoga obsession and the Need for a Video Explorer Site

In the Summer of 2018, I became obsessed with the niche sport of Acroyoga. Acroyoga is the practice of doing yoga-inspired acrobatics with other people. Most people start with simple positions like ‘bird’, which you might have tried and called ‘airplane’ as a kid (holding a person up on your feet while laying down). From bird, Acroyoga progresses into hundreds of other positions and sequences of positions. Acroyoga scenes exist all over the world and the common vocabulary of positions allows strangers to meet up and easily ‘jam’ together.

There are thousands of ‘acroyogis’ on Instagram and YouTube and many of them post and share their latest creations constantly. For intermediate acroyogis like myself, I’m constantly saving, reposting, and curating from this firehose of Acroyoga content. When I’m trying something new at an Acroyoga jam, I’m often pulling my phone out to go through my favorited videos.

I’ve long found it challenging to find just the right video to work on. Sometimes I’m looking to learn something that is simply fun, novel, or playful. Other days, I might be looking for a challenging sequence right at the edge of my ability. It ends up taking me (and whoever I’m jamming with) too much time of hunting and pecking on our phones while sitting on a blanket in some park. I want a better Acroyoga videos site that makes it simpler to find the right video sooner.

My requirements for this Acroyoga videos site is that it be simple to add data to, load fast on mobile, and be generally intuitive to use. For this reason, I’m using Gatsby (and of course, React) and the Google Sheets API for this site. This simple stack will allow me to build a fast loading application that consumes data from a Google Sheet.

As such, this article will show how I built this site. I’ll show only the code samples that are Gatsby and Google Sheets specific but you can see all of the code in this repo: https://github.com/kpennell/acrotagsgatsbyblog. Finally, if you want to check out the demo app, that can be found here: https://acrotagsgatsbyblog.netlify.com.

Creating a Basic Gatsby Setup with Material-UI

Gatsby’s docs have a wide variety of Gatsby starters, each with different features and advantages. I opted to use a starter I built for another Gatsby tutorial I wrote. My starter has a simple file structure and is setup to use Material-UI (which I’ll use heavily for building this interface). There are a couple ways to use Material-UI in Gatsby but I found the gatsby-plugin-material-ui plugin route the easiest. Unless you have a very specific use case, save yourself the rabbit hole, install the plugin, and setup the theme within gatsby-config.js like so:

resolve: </span><span class="token template-string string">gatsby-plugin-material-ui</span><span class="token template-string template-punctuation string">,

My Google Sheet Dataset and How to Access it

Google sheets are pretty amazing if you think about it. While they might never be able to handle the millions of rows that Excel can, you can share and collaborate on them. There’s tons of add-ons, macros, and scripts you can use to extend the default functionality. Finally, you can consume the data from a Google Sheet using a fairly well-documented API. My Acroyoga videos data can be publicly accessed here. Here’s what my row headers look like and several of my rows:

Google Sheets UI

This is a list of Acroyoga videos from Youtube. I tagged these videos in column I (e.g. beginner, flow). The ‘thumbnail’ column is the Youtube video thumbnail while instructorimage are some dummy images I saved in a Google cloud storage bucket. The instructorimage column is not DRY here and I could find a way to store this separately in another sheet. This occasional lack of ‘DRYness’ is one potential downside of using a spreadsheet as a web application database.

To access this data, I first must ‘publish to the web’ (under the file menu). This makes it available for consuming via API. Next I needed to register for an API key with Google. Finally, I can GET this data using a Google Sheets API method called batchGet. My API call outputs my spreadsheet data as JSON, which I’ll now consume in my Gatsby app.

Making Gatsby Pages for Each Google Sheet Row

To create Gatsby pages for each row, I’ll need to GET this Google Sheets JSON data within gatsby-node.js (I use Axios for this but fetch is also cool). There a couple of steps I’ll need to take to make these spreadsheet rows available as Gatsby nodes.

First, I need to format the Google Sheets JSON to be a big array of row objects with the row headers as properties. Google Sheets outputs the data as an array of arrays for each row like so:

With a couple of loops (see below), I can convert this array of arrays into an array of objects with properties.

const arrayOfItems = response.data.valueRanges[0].values
for (var i = 1; i < arrayOfItems.length; i++) {
for (var j = 0; j < arrayOfItems[i].length; j++) {
rowObject[arrayOfItems[0][j]] = arrayOfItems[i][j]

Here’s my new array of objects with the properties matching the first row.

"videoId": "yWIuqyYMobc",
"instructor": "Instructor 1",
"title": "Acro Yoga Basics | Partner Yoga Class | Beginner | Free Yoga",
"tags": "beginner, acro",
"videoId": "Uh5ADKbrSBU",
"instructor": "Instructor 1",
"title": "PARTNER YOGA POSES FOR BEGINNERS",

Next, I’d like for my comma-separated strings of tags (column I) to be arrays of tags (so I can .map over them in my React UI). This code allows me to do that:

let itemsArrayWithTagsArray = rows.map(function(item) {
item.tags = item.tags.split(",").map(item => item.trim())

Now, I’m ready to create Gatsby nodes like so:

itemsArrayWithTagsArray.map((item, i) => {
id: createNodeId(</span><span class="token template-string interpolation interpolation-punctuation punctuation">${</span><span class="token template-string interpolation">i</span><span class="token template-string interpolation interpolation-punctuation punctuation">}</span><span class="token template-string template-punctuation string">),
contentDigest: createContentDigest(item),
thumbnail: item.thumbnail,
instructor: item.instructor,
instructorimage: item.instructorimage,

I then used the Gatsby createPage method to create pages for each of these rows (shown here).

Making Gatsby Pages for Tags and Instructors

Every tag and instructor should have its own page (with a list of matching videos). I’ll now show you how I created these pages within gatsby-node.js.

If you went through the Gatsby tutorial at some point, you’re probably familiar with the flow of creating nodes then creating pages with those nodes. You can also create pages with what I’d call ‘derivatives of nodes’. Put more simply, you can, for example, loop over a node (like my rows node created in the previous section) and create pages with the result of your loop. This is what I did to create my tag pages, like so:

if (item.node.tags.length > -1) {
tags = tags.concat(item.node.tags)
const tagPath = </span><span class="token template-string string">/tag/</span><span class="token template-string interpolation interpolation-punctuation punctuation">${</span><span class="token template-string interpolation">_</span><span class="token template-string interpolation punctuation">.</span><span class="token template-string interpolation function">kebabCase</span><span class="token template-string interpolation punctuation">(</span><span class="token template-string interpolation">tag</span><span class="token template-string interpolation punctuation">)</span><span class="token template-string interpolation interpolation-punctuation punctuation">}</span><span class="token template-string string">/</span><span class="token template-string template-punctuation string">
component: path.resolve(</span><span class="token template-string string">src/templates/single-tag.js</span><span class="token template-string template-punctuation string">),

I did exactly the same for my instructors so I’ll skip it here for brevity. As you can see, createPage is using single-tag.js and single-instructor.js to create these pages. I’ll next show you, at a high-level, how I created these.

Creating the UI with Material-UI

As I mentioned at the beginning, I won’t show the code for each and every component here. But I will explain the general process I used to create this UI. I first created the templates, which are single-tag.js, single-item.js, and single-instructor.js. These templates are used in gatsby-node.js when the createPage methods are called.

My templates are fairly standard Gatsby templates with a React component at the top and a graphql query at the bottom. On the single-tag.js and single-instructor.js templates, I use filters in the query to get only the videos that match that tag. Here’s how that query looks:

export const tagPageQuery = graphql</span></div><div class="token-line"><span class="token plain"> query </span><span class="token function maybe-class-name">TagPage</span><span class="token punctuation">(</span><span class="token parameter">$tag</span><span class="token parameter punctuation">:</span><span class="token parameter"> String</span><span class="token punctuation">)</span><span class="token plain"> </span><span class="token punctuation">{</span><span class="token plain"></span></div><div class="token-line"><span class="token plain"> </span><span class="token function">allItem</span><span class="token punctuation">(</span><span class="token parameter">filter</span><span class="token parameter punctuation">:</span><span class="token parameter"> </span><span class="token parameter punctuation">{</span><span class="token parameter"> tags</span><span class="token parameter punctuation">:</span><span class="token parameter"> </span><span class="token parameter punctuation">{</span><span class="token parameter"> </span><span class="token parameter keyword">in</span><span class="token parameter punctuation">:</span><span class="token parameter"> </span><span class="token parameter punctuation">[</span><span class="token parameter">$tag</span><span class="token parameter punctuation">]</span><span class="token parameter"> </span><span class="token parameter punctuation">}</span><span class="token parameter"> </span><span class="token parameter punctuation">}</span><span class="token punctuation">)</span><span class="token plain"> </span><span class="token punctuation">{</span><span class="token plain"></span></div></code></pre></div><p>This query uses $tag from React context (which was created in gatsby-node.js) to do the filtering. If $tag is found in the array of tags (for that row), then it returns that row (which is a video). This is how I can create separate instructor and tag pages that only show those relevant videos (the videos with those specific tags or instructors).</p><p>At the top of my tag pages, I have a title that says &#x201C;24 videos tagged with [the tag]&#x201D;. To get the tag for that page, I need to pass pageContext as a prop to this template component. That allows me to access that $tag variable within the template (instead of a graphql query). Now I can make the title with the following code:</p><div class="gatsby-highlight"><pre class="language-jsx"><button class="css-93j64b">Copy<span class="css-16cz5zo erisdwh3">copy code to clipboard</span></button><code class="language-jsx"><div class="token-line"><span class="token punctuation">{</span><span class="token plain">itemsWithTag</span><span class="token punctuation">.</span><span class="token plain">length</span><span class="token punctuation">}</span><span class="token plain"> </span><span class="token punctuation">{</span><span class="token plain">itemsWithTag</span><span class="token punctuation">.</span><span class="token plain">length </span><span class="token operator">&gt;</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"> </span><span class="token operator">?</span><span class="token plain"> </span><span class="token string">&apos;videos&apos;</span><span class="token plain"> </span><span class="token punctuation">:</span><span class="token plain"> </span><span class="token string">&apos;video&apos;</span><span class="token punctuation">}</span><span class="token plain"> tagged </span><span class="token keyword">with</span><span class="token plain"></span></div><div class="token-line"><span class="token plain"> </span><span class="token tag punctuation">&lt;</span><span class="token tag class-name">Chip</span><span class="token tag"> </span><span class="token tag attr-name">icon</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript tag punctuation">&lt;</span><span class="token tag script language-javascript tag class-name">Label</span><span class="token tag script language-javascript tag"> </span><span class="token tag script language-javascript tag attr-name">className</span><span class="token tag script language-javascript tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript tag script language-javascript punctuation">{</span><span class="token tag script language-javascript tag script language-javascript">classes</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">chipLabel</span><span class="token tag script language-javascript tag script language-javascript punctuation">}</span><span class="token tag script language-javascript tag"> </span><span class="token tag script language-javascript tag punctuation">/&gt;</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"> </span><span class="token tag attr-name">label</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">pageContext</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">tag</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"> </span><span class="token tag attr-name">key</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">pageContext</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">tag</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript function">toString</span><span class="token tag script language-javascript punctuation">(</span><span class="token tag script language-javascript punctuation">)</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">tagChips</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag punctuation">/&gt;</span></div></code></pre></div><p>The first brackets (above) give me the length (the number of rows or videos that match the tag). Next I use the ternary expression so that if the length is more than 1, I show plural &#x2018;videos&#x2019; instead of &#x2018;video&#x2019;. Next, I&#x2019;m using a Material-UI chip component to show the value of pageContext.tag (which is the tag). And this is what it achieves in my template:</p><p><span class="gatsby-resp-image-wrapper"> <a class="gatsby-resp-image-link" href="http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/6095e/word-image-1.png"> <span class="gatsby-resp-image-background-image"></span> <img class="gatsby-resp-image-image" alt="Tagged videos" src="http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/73d01/word-image-1.png" srcset="http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/a3b1f/word-image-1.png 197w, http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/07137/word-image-1.png 393w, http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/73d01/word-image-1.png 786w, http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/a5918/word-image-1.png 1179w, http://jsfeeds.com/static/d264011f8f3fdb34e753a935527925e3/6095e/word-image-1.png 1266w"> </a> </span></p><p>If you&#x2019;d like to dive into the single-tag or single-instructor templates more, they can be found in <a href="https://github.com/kpennell/acrotagsgatsbyblog/tree/master/src/templates">the templates folder</a>.</p><p>When I first learned Gatsby, I found the folder structure, node and page creation methods really byzantine and confusing. I struggled to understand why there was no App.js and familiar parent-child component tree. I dug through the files looking for the React-router setup and was at a loss when I didn&#x2019;t find one. But once I got the hang of the Gatsby way of doing things, I began to love how easy it was to not only create prebuilt fast-loading pages (made up of React components) but also to link these pages together.</p><p>This video explorer site allows users to easily filter videos by clicking/tapping on the tags or instructors. This links them to a new page with only the matching videos. Thanks to Gatsby&#x2019;s Link component, coding this functionality is very easy. Here is a card for one of the videos (ie rows in the Google Sheet).</p><p><span class="gatsby-resp-image-wrapper"> <a class="gatsby-resp-image-link" href="http://jsfeeds.com/static/47caa9e7f6897e4afc3ac0d8ef108c31/a8200/word-image-2.png"> <span class="gatsby-resp-image-background-image"></span> <img class="gatsby-resp-image-image" alt="Video Card showing Acroyoga" src="http://jsfeeds.com/static/47caa9e7f6897e4afc3ac0d8ef108c31/a8200/word-image-2.png" srcset="http://jsfeeds.com/static/47caa9e7f6897e4afc3ac0d8ef108c31/a3b1f/word-image-2.png 197w, http://jsfeeds.com/static/47caa9e7f6897e4afc3ac0d8ef108c31/07137/word-image-2.png 393w, http://jsfeeds.com/static/47caa9e7f6897e4afc3ac0d8ef108c31/a8200/word-image-2.png 700w"> </a> </span></p><p>Linking to the instructor and tags is done with this code:</p><div class="gatsby-highlight"><pre class="language-jsx"><button class="css-93j64b">Copy<span class="css-16cz5zo erisdwh3">copy code to clipboard</span></button><code class="language-jsx"><div class="token-line"><span class="token tag punctuation">&lt;</span><span class="token tag class-name">CardContent</span><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">cardContent</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag punctuation">&gt;</span><span class="token plain"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">style</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript"> textDecoration</span><span class="token tag script language-javascript punctuation">:</span><span class="token tag script language-javascript"> </span><span class="token tag script language-javascript string">&quot;none&quot;</span><span class="token tag script language-javascript"> </span><span class="token tag script language-javascript punctuation">}</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">to</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript template-string template-punctuation string">/video/${kebabCase(item.node.title)}/</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token plain"> </span><span class="token tag punctuation">&lt;</span><span class="token tag class-name">Typography</span><span class="token tag"> </span><span class="token tag attr-name">variant</span><span class="token tag attr-value punctuation">=</span><span class="token tag attr-value punctuation">&quot;</span><span class="token tag attr-value">h5</span><span class="token tag attr-value punctuation">&quot;</span><span class="token tag"> </span><span class="token tag attr-name">component</span><span class="token tag attr-value punctuation">=</span><span class="token tag attr-value punctuation">&quot;</span><span class="token tag attr-value">h3</span><span class="token tag attr-value punctuation">&quot;</span><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">title</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag punctuation">&gt;</span><span class="token plain"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">to</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript template-string template-punctuation string">/instructor/${kebabCase(item.node.instructor)}/</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">style</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript"> textDecoration</span><span class="token tag script language-javascript punctuation">:</span><span class="token tag script language-javascript"> </span><span class="token tag script language-javascript string">&quot;none&quot;</span><span class="token tag script language-javascript"> </span><span class="token tag script language-javascript punctuation">}</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token tag script language-javascript tag"> </span><span class="token tag script language-javascript tag attr-name">src</span><span class="token tag script language-javascript tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript tag script language-javascript punctuation">{</span><span class="token tag script language-javascript tag script language-javascript">item</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">node</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">optimized_instructor_image</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">childImageSharp</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">fluid</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">src</span><span class="token tag script language-javascript tag script language-javascript punctuation">}</span><span class="token tag script language-javascript tag"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">label</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">item</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">node</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">instructor</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">instructorChips</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token plain"> </span><span class="token tag punctuation">&lt;</span><span class="token tag">div</span><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">tagArea</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag punctuation">&gt;</span><span class="token plain"></span></div><div class="token-line"><span class="token plain"> </span><span class="token punctuation">{</span><span class="token plain">item</span><span class="token punctuation">.</span><span class="token plain">node</span><span class="token punctuation">.</span><span class="token plain">tags</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">tag</span><span class="token plain"> </span><span class="token operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation">(</span><span class="token plain"></span></div><div class="token-line"><span class="token plain"> </span><span class="token tag punctuation">&lt;</span><span class="token tag class-name">Link</span><span class="token tag"> </span><span class="token tag attr-name">to</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript template-string template-punctuation string">/tag/${kebabCase(tag)}/</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"> </span><span class="token tag attr-name">style</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript"> textDecoration</span><span class="token tag script language-javascript punctuation">:</span><span class="token tag script language-javascript"> </span><span class="token tag script language-javascript string">&quot;none&quot;</span><span class="token tag script language-javascript"> </span><span class="token tag script language-javascript punctuation">}</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag punctuation">&gt;</span><span class="token plain"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">icon</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript tag punctuation">&lt;</span><span class="token tag script language-javascript tag class-name">Label</span><span class="token tag script language-javascript tag"> </span><span class="token tag script language-javascript tag attr-name">className</span><span class="token tag script language-javascript tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript tag script language-javascript punctuation">{</span><span class="token tag script language-javascript tag script language-javascript">classes</span><span class="token tag script language-javascript tag script language-javascript punctuation">.</span><span class="token tag script language-javascript tag script language-javascript">chipLabel</span><span class="token tag script language-javascript tag script language-javascript punctuation">}</span><span class="token tag script language-javascript tag"> </span><span class="token tag script language-javascript tag punctuation">/&gt;</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">tagChips</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div></code></pre></div><p>That&#x2019;s it. It&#x2019;s just {gfm-js-extract-pre-5} and the relative path, e.g. {gfm-js-extract-pre-6}. I don&#x2019;t need to worry about passing the right props or anything else. I know the page exists (because I built it using createPage in gatsby-node.js). I know the template will have the right data (because I setup the graphql query in that template file). I just need to Link to the right URL, and the pages will connect correctly. This creates a fast and intuitive user experience wherein the user can quickly click through the pages to find the right video.</p><h2 id="creating-a-simple-video-player-with-react-player"><a href="http://jsfeeds.com/go/using-the-google-sheets-api-with-gatsby-for-creating-fast-sites-with-simple-data-sources-5d799c8ea6aa5fde1fd4f6b6?s=rss#creating-a-simple-video-player-with-react-player" class="anchor"><svg height="16" width="16"><path/></svg></a>Creating a simple video player with React-Player</h2><p>Tag and instructor filtering functionality is great but what the user ultimately wants to do is watch an Acroyoga video. I used <a href="https://github.com/CookPete/react-player/issues">React-player</a> within the single-item.js (video template file) to load the Youtube video player within the app.</p><div class="gatsby-highlight"><pre class="language-jsx"><button class="css-93j64b">Copy<span class="css-16cz5zo erisdwh3">copy code to clipboard</span></button><code class="language-jsx"><div class="token-line"><span class="token tag"> </span><span class="token tag attr-name">className</span><span class="token tag script language-javascript script-punctuation punctuation">=</span><span class="token tag script language-javascript punctuation">{</span><span class="token tag script language-javascript">classes</span><span class="token tag script language-javascript punctuation">.</span><span class="token tag script language-javascript">player</span><span class="token tag script language-javascript punctuation">}</span><span class="token tag"></span></div></code></pre></div><p>Be it that most people are going to that video page to watch the video, I set autoplay to true (with the &#x2018;playing&#x2019; prop). You can see the entire video player component template <a href="https://github.com/kpennell/acrotagsgatsbyblog/blob/master/src/templates/single-item.js">here</a>.</p><h2 id="implementing-gatsby-image-with-remote-images"><a href="http://jsfeeds.com/go/using-the-google-sheets-api-with-gatsby-for-creating-fast-sites-with-simple-data-sources-5d799c8ea6aa5fde1fd4f6b6?s=rss#implementing-gatsby-image-with-remote-images" class="anchor"><svg height="16" width="16"><path/></svg></a>Implementing Gatsby Image with Remote Images</h2><p>After I created the necessary pages and components for my instructor, tag, and video pages, I had a pretty nice site built. A user could play videos and filter based on tag and instructor. I deployed my app to Netlify and tried it out on my phone. But something was missing.</p><p>A big reason why I (and many others) love Gatsby is its speed. After using heavy client-side rendered sites, Gatsby sites feel more modern and fast. It&#x2019;s nice to be able to explore content almost as fast as you can think. It&#x2019;s also nice not having to mess with server setup (vs. the simplicity of cloud storage).</p><p>But something about my new Acroyoga video site felt sluggish. I tried it on my older Android phone over 4g and, sure, it was ok, but not great. I next ran my demo site through <a href="https://developers.google.com/speed/pagespeed/insights/">Google&#x2019;s PageSpeed insights</a> and <a href="https://gtmetrix.com/">GTmetrix</a> tests. My fears were confirmed. The site was good but not great. PageSpeed Insights gave it to me straight, the images were hurting me:</p><p><span class="gatsby-resp-image-wrapper"> <a class="gatsby-resp-image-link" href="http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/001cd/word-image-3.png"> <span class="gatsby-resp-image-background-image"></span> <img class="gatsby-resp-image-image" alt="Lighthouse Showing Need for Properly Sized Images" src="http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/73d01/word-image-3.png" srcset="http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/a3b1f/word-image-3.png 197w, http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/07137/word-image-3.png 393w, http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/73d01/word-image-3.png 786w, http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/a5918/word-image-3.png 1179w, http://jsfeeds.com/static/08c74c9815a5873ff602f9c3456f0c0a/001cd/word-image-3.png 1502w"> </a> </span></p><p>This was such a clear and easy &#x2018;aha!&#x2019; moment. I hadn&#x2019;t optimized my images and was missing out on one of the best parts of Gatsby: <a href="http://jsfeeds.com/packages/gatsby-image/">gatsby-image</a>. Gatsby image gives Gatsby users a whole bunch of built-in image optimizations. Images will be properly optimized (so you&#x2019;re not loading image sizes you don&#x2019;t need to) and the images will load a small blurred version first before loading the complete image. This makes for much faster page loads and a better user experience.</p><p>I dug into my normal gatsby-image workflow but I quickly hit a snag. My previous use of Gatsby-image had been for local images whilst I was now trying to use remote images (cloud storage image links). Thankfully, with just a bit more searching, I found the right plugin to solve this: <a href="http://jsfeeds.com/packages/gatsby-plugin-remote-images/">gatsby-plugin-remote-images</a>.</p><p>Gatsby-plugin-remote-images fetches image URL links (e.g. <a href="http://super-image.png/">http://super-image.png</a>) and prepares them in a way that gatsby-image can use them. To make my cards load faster, I&#x2019;d need to optimize both the video thumbnail as well as the small instructor image. It makes no sense at all to load a 300+ pixel image of an instructor when all you really need are maybe 40 pixels max.</p><p><span class="gatsby-resp-image-wrapper"> <a class="gatsby-resp-image-link" href="http://jsfeeds.com/static/6ec55681aa05be1fd24f0e1437dcd059/6da3e/word-image-4.png"> <span class="gatsby-resp-image-background-image"></span> <img class="gatsby-resp-image-image" alt="Acroyoga Card with Optimized Images" src="http://jsfeeds.com/static/6ec55681aa05be1fd24f0e1437dcd059/6da3e/word-image-4.png" srcset="http://jsfeeds.com/static/6ec55681aa05be1fd24f0e1437dcd059/a3b1f/word-image-4.png 197w, http://jsfeeds.com/static/6ec55681aa05be1fd24f0e1437dcd059/07137/word-image-4.png 393w, http://jsfeeds.com/static/6ec55681aa05be1fd24f0e1437dcd059/6da3e/word-image-4.png 720w"> </a> </span></p><p>Here&#x2019;s what I had to add to my gatsby-config.js to configure the plugin:</p><div class="gatsby-highlight"><pre class="language-javascript"><button class="css-93j64b">Copy<span class="css-16cz5zo erisdwh3">copy code to clipboard</span></button><code class="language-javascript"><div class="token-line gatsby-highlight-code-line"><span class="token plain"> resolve</span><span class="token punctuation">:</span><span class="token plain"> </span><span class="token string">&quot;gatsby-plugin-remote-images&quot;</span><span class="token punctuation">,</span><span class="token plain"></span></div><div class="token-line gatsby-highlight-code-line"><span class="token plain"> name</span><span class="token punctuation">:</span><span class="token plain"> </span><span class="token string">&quot;optimized_thumbnail&quot;</span><span class="token punctuation">,</span><span class="token plain"></span></div><div class="token-line gatsby-highlight-code-line"><span class="token plain"> resolve</span><span class="token punctuation">:</span><span class="token plain"> </span><span class="token string">&quot;gatsby-plugin-remote-images&quot;</span><span class="token punctuation">,</span><span class="token plain"></span></div><div class="token-line gatsby-highlight-code-line"><span class="token plain"> imagePath</span><span class="token punctuation">:</span><span class="token plain"> </span><span class="token string">&quot;instructor_image&quot;</span><span class="token punctuation">,</span><span class="token plain"></span></div><div class="token-line gatsby-highlight-code-line"><span class="token plain"> name</span><span class="token punctuation">:</span><span class="token plain"> </span><span class="token string">&quot;optimized_instructor_image&quot;</span><span class="token punctuation">,</span><span class="token plain"></span></div></code></pre></div><p>This tells the plugin that it will be the item nodes (the nodes created in {gfm-js-extract-pre-7} for my videos) and to grab the instructor_image and thumbnail paths. Optimized_instructor_image and optimized_thumbnail are the aliases for the new gatsby-image friendly fields.</p><p>Next, I had to change my GraphQL queries to fit the gatsby-image way of getting images (vs. just getting an image url link string like I was doing before). Here&#x2019;s how {gfm-js-extract-pre-8} query now looks.</p><div class="gatsby-highlight"><pre class="language-javascript"><button class="css-93j64b">Copy<span class="css-16cz5zo erisdwh3">copy code to clipboard</span></button><code class="language-javascript"><div class="token-line"><span class="token keyword module">export</span><span class="token plain"> </span><span class="token keyword">const</span><span class="token plain"> </span><span class="token maybe-class-name">ItemPageQuery</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> graphql\
query ItemDetails($itemId: String!) {
item(id: { eq: $itemId }) {
fluid(maxWidth: 400, maxHeight: 250) {
optimizedinstructorimage {

You probably recognize those fluid and childImageSharp fields if you’ve worked with gatsby-image and gatsby-plugin-sharp before. The maxHeight: 50 part there scales the instructor images down from 267 pixels to 50. I run my site through GTmetrix again and lo and behold, I had shaved my site down from 543KB to 385KB.

Before

Original Performance Report

After

Updated Performance Report

This change immediately noticeable on my clunky old Android mobile device. This easy change will make a big difference for future Acroyogis on their phones on a blanket in park somewhere, about to learn something new.

Final Thoughts

Thanks the power of Gatsby, React, Material-UI, and Google Sheets, I’m now able to have a fast-loading video explorer site with intuitive linking and filtering. I can now go through and tag my Acroyoga videos collection, put it into this Google Sheet, and make it easier for me (and soon, others) to find the right video to try next. Of course, this is not the end of the road. Here’s some ideas of how I could make this site even better:

  • Setup a webhook so that Netlify automatically rebuilds the site once I change something in Google Sheets
  • Create a way that users can save their favorites in localStorage or possibly, in something like Firebase
  • On the individual videos page, link out to similar videos
  • Tell more about the instructors on their video pages

Thanks for reading this tutorial. If you get stuck on something, please feel free to reach out on Twitter @kyleapennell.


Tag cloud