{
    "componentChunkName": "component---src-templates-blog-post-tsx",
    "path": "/2021/how-to-add-twitter-card-images-to-your-gatsby-blog/",
    "result": {"data":{"site":{"siteMetadata":{"title":"Cole Striler","siteUrl":"https://www.striler.blog"}},"markdownRemark":{"id":"70a7dd72-1916-5cfc-9724-3be72fa33b6a","excerpt":"In this tutorial, I will show you a simple three-step process to add Twitter, LinkedIn, and Facebook card images to your Gatsby blog.  As it turns out, it’s a…","html":"<p>In this tutorial, I will show you a simple three-step process to add Twitter, LinkedIn, and Facebook card images to your Gatsby blog. </p>\n<p>As it turns out, it’s a bit more complicated than simply importing your thumbnail into the Seo component and passing it as a variable into the meta tags.</p>\n<p><strong>From a high-level</strong>: we will first define the thumbnail in the front matter of our blog post (<strong>step 1</strong>). We will then pull the thumbnail into <code class=\"language-text\">src/templates/blog-post.tsx</code> and pass it into the Seo component where the meta tags are defined (<strong>step 2</strong>). Finally, we will receive the thumbnail from within the Seo component and add it to the appropriate meta tags (<strong>step 3</strong>).</p>\n<p><strong>Result</strong>: At the end of this tutorial, you will be able to share links to your blog on Twitter, Facebook, and LinkedIn with custom images for each blog post.</p>\n<h2>Step 0: Create a Gatsby Blog</h2>\n<p>The Gatsby starter blog comes with the required packages for this tutorial. If you’ve already created your blog using the <a href=\"https://github.com/gatsbyjs/gatsby-starter-blog\">Gatsby Starter Blog Template</a>, you can <strong>skip this step</strong>. If you haven’t, you will need to run the code below to generate your new blog: </p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">gatsby new my-blog-starter https://github.com/gatsbyjs/gatsby-starter-blog</code></pre></div>\n<p>If you are not using the Gatsby starter blog template, make sure to install the following packages: <code class=\"language-text\">react-helmet</code>, <code class=\"language-text\">gatsby-source-filesystem</code>, <code class=\"language-text\">gatsby-transformer-remark</code>, <code class=\"language-text\">gatsby-remark-images</code>, <code class=\"language-text\">gatsby-transformer-sharp</code>.</p>\n<h2>Step 1: Insert the Image and Set the Front Matter Property</h2>\n<p>Navigate to the <strong><code class=\"language-text\">content/blog</code></strong> directory.</p>\n<p>Insert your thumbnail next to the <code class=\"language-text\">index.md</code> file inside a specific blog directory.</p>\n<p><strong>Note:</strong> It is recommended to use a thumbnail with dimensions 1200x628px.\n<span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 484px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/efab5f94d8107c439ba7600d1211351a/ff42b/imageLocation.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 63.92405063291139%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABYlAAAWJQFJUiTwAAACrUlEQVQ4y5WTS28bZRSGZ4/oT6hUpSGJhGDRhNhzv4/HnvHcPPY4jh3fEpp2UbVZtItWCKHEUFgATdpSseUPsWQNLEuV/oAHfdOKQncsnnm/7xy9Z85ozpF020U1LYI4JSr62H4LL2xj1HEbxbD+F5IThOi2Qzmec/Ll1+T9ioPZnOliieG47Coasm7S1Ixa3+ffcXGWxEMguhFBN4zwOl1sL8BrteuONcvBdP1aBYbj1d2IrxN5XcRsF8sLkERCFPTCDpppMT66zb0vzuikOePZnNFkShgnjKczZstD8nLA3nhCWpQczBccfn5MlGa1zg+PkGTDQdAU6Daq5aE6AY4b0I0SFNNBsT0Uy0WzfWTTQRUdWl59F3HhF3fFdJF0ZYd/UHfQlB3U3U9RyxL97gO0qIk/bOD0Glidz3DLBk62iy4Lz/Z/fAJpM1uxka3YzN/pVnrK1ekLrt75hbXyWzaKU7bKFev5GZu9FWvZGdfiU64nZ6y9RfgE0pXbv/M+Hxz/yfqD32h+9Sva6i/K55fkF5cUTy9pf/+K+MdXjH5+jf7NS4YvXmM8fsmHt4T3D6Tr5RPecF7rR4MLrmU/sDX8jo3RYz7Zu+DG5Jwb43O2J+d8PHxCY/YU5+ZzlMUzvOOfkBfPWHtbR9I0GVneJQx9NLXBwXLJycNHFEXOwXifqiqZTPaphn2m0wmDQY8o7tBNE9IsYbFcYNkWqtpA1JJkw0agir+lWzhhTJiVmF4LyxeE2H6IE7Tx2lGthuPXM2e6AWIxhG+7qdHUrHdz6LcjFN1kODvi6O59OklKNRpTDCrKakhZ7ZEUvXrmeoMh3awgzvI6l/X6TGZzOt30TUGxIWLqZd0gTIt6DYNOTJIXZGWfOM1riv6gfrEoJrQVdenmPZK8RzXaxwla/A0nJPe8x6FgkQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Add an image to your blog folder\"\n        title=\"Add an image to your blog folder\"\n        src=\"/static/efab5f94d8107c439ba7600d1211351a/ff42b/imageLocation.png\"\n        srcset=\"/static/efab5f94d8107c439ba7600d1211351a/c26ae/imageLocation.png 158w,\n/static/efab5f94d8107c439ba7600d1211351a/6bdcf/imageLocation.png 315w,\n/static/efab5f94d8107c439ba7600d1211351a/ff42b/imageLocation.png 484w\"\n        sizes=\"(max-width: 484px) 100vw, 484px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<p>Next, open up the <code class=\"language-text\">index.md</code> file and add a link to your thumbnail in the front matter.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">---\ntitle: Blog Title\ndate: \"2021-06-24T12:00:00.284Z\"\ndescription: \"Blog description\"\nthumbnail: \"./twitterImage.jpg\"\n---</code></pre></div>\n<h2>Step 2: Access the Thumbnail in Blog Post Component</h2>\n<p>Navigate to <strong><code class=\"language-text\">blog-post.tsx</code></strong>.</p>\n<p>Next, we need to access the thumbnail from within <code class=\"language-text\">blog-post.tsx</code> so that we can send it to the Seo component.</p>\n<p>Add the <code class=\"language-text\">thumbnail</code> field to your blog-post page query as shown below:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">export const pageQuery = graphql`\n  query BlogPostBySlug(\n    $id: String!\n    $previousPostId: String\n    $nextPostId: String\n  ) {\n    site {\n      ...\n    }\n    markdownRemark(id: { eq: $id }) {\n      ...\n      frontmatter {\n        title\n        date(formatString: \"MMMM DD, YYYY\")\n        description\n \n        thumbnail {\n          childImageSharp {\n            fixed(width: 1200) {\n              ...GatsbyImageSharpFixed\n            }\n          }\n        }\n        ...\n      }\n    }\n  }\n`</code></pre></div>\n<p>In the same file, go to the <code class=\"language-text\">BlogPostTemplate</code> component and add a constant named <code class=\"language-text\">thumbnail</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">const BlogPostTemplate = ({ data, location }) => {\n    const post = data.markdownRemark\n    const siteTitle = data.site.siteMetadata?.title || `Blog - Cole Striler`\n    const {previous, next} = data\n    const {siteUrl} = data.site.siteMetadata\n    const thumbnail = post.frontmatter.thumbnail\n    ...\n}</code></pre></div>\n<p>Now we can pass the thumbnail to the Seo component.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;Seo\n    title={post.frontmatter.title}\n    description={post.frontmatter.description || post.excerpt}\n    thumbnail={thumbnail}\n/></code></pre></div>\n<h2>Step 3: Add Thumbnail to Meta Tags in Seo Component</h2>\n<p>Navigate to <strong><code class=\"language-text\">seo.tsx</code></strong>.</p>\n<p>Finally, we need to receive the thumbnail from within the Seo component and add it to the meta tags.</p>\n<p>Update your code to pull in the thumbnail:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">const Seo = ({ description, lang, meta, title, thumbnail }) => {</code></pre></div>\n<p>Next, we need to build a custom link that tells the browser where to look for our new thumbnail. Add <code class=\"language-text\">imageSrc</code> and <code class=\"language-text\">image</code> to the Seo component under the static query.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">const {site} = useStaticQuery(\n    ...\n  )\nconst imageSrc = thumbnail &amp;&amp; thumbnail.childImageSharp.fixed.src;\nconst image = site.siteMetadata?.siteUrl + imageSrc;</code></pre></div>\n<p>The above code should build the correct link to your thumbnail, but if it doesn’t, you need to modify the path that generates your <code class=\"language-text\">imageSrc</code>.</p>\n<p>To find the source of your image, inspect the browser window and set a breakpoint at the beginning of the Seo component. Open up the local variables and find the path to the <code class=\"language-text\">src</code> within the <code class=\"language-text\">thumbnail</code> variable.</p>\n<p>In the far right of the screenshot below, you can see that the <code class=\"language-text\">src</code> of my thumbnail is under the path <code class=\"language-text\">thumbnail.childImageSharp.fixed.src</code>.\n<span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 630px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/958cbdacc17cb8f534f83b11a533afe9/757fd/thumbnailSource2.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 59.49367088607595%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB9klEQVQoz1WR227bMBBE9REF2iKuRS6Xd3KXkqiL41hynLS5NOhD/v9jCqMF0gKDeZrzMqcZp7mOE1FphQyeujJm6oi6TEzcpVzqdJjHwzCO/TAwlxhTTKkU5q5r1vsf58tjHZeQyNhgXTAugHYC9F4oCZh4VoaEikIlZRg0CUwCkzK52c7H9fwwLbc5lVTW3N2OidiFaByCFq1+ufj3J/W67d7ONz+3r6/rl5fT56e7T+/P2GzrctoudTry8ljWhc+HfDfqmrFm6LPs0nHu1rmbqh1YDSwHhqFAZTn3prnb7k/bA5eKnlNylYM0ukX8G43Jj0uYK9qyV7THslexRZDmm9BNP0xchhBZKaMAa/BCoJAoJQpx7ZOh72Ei4w0oaEG2YPey2wndQhMTWxd9yKgdSKUA9626Rqjd9Rm16Djzlr0n7zj4Ej3F0Hl3ANWkXJxPPmRro3bkEhFdZTBH4x0DFqn7cjyO08R5KjQx1UI1x0nID9i55FK1uSfKxClEZ61FxAXsfejGlBiRtc6IRamxhajMB2xsSGUKuRegAVCCvqqWECz/ouc3vl0zdQIGCZ1UCNiC/g/2MY21p3w9zxiP2rWgoqELvS7dZjPfCLWTuJPYSmyF+heO2pgcffDOOqu1FtcFMPUl15yKMV4IJf+IkKiN/w3gC3pmAjryhQAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"How to build a link to your thumbnail\"\n        title=\"How to build a link to your thumbnail\"\n        src=\"/static/958cbdacc17cb8f534f83b11a533afe9/f058b/thumbnailSource2.png\"\n        srcset=\"/static/958cbdacc17cb8f534f83b11a533afe9/c26ae/thumbnailSource2.png 158w,\n/static/958cbdacc17cb8f534f83b11a533afe9/6bdcf/thumbnailSource2.png 315w,\n/static/958cbdacc17cb8f534f83b11a533afe9/f058b/thumbnailSource2.png 630w,\n/static/958cbdacc17cb8f534f83b11a533afe9/40601/thumbnailSource2.png 945w,\n/static/958cbdacc17cb8f534f83b11a533afe9/78612/thumbnailSource2.png 1260w,\n/static/958cbdacc17cb8f534f83b11a533afe9/757fd/thumbnailSource2.png 2800w\"\n        sizes=\"(max-width: 630px) 100vw, 630px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<p><strong>Note</strong>: Do NOT use the following code to fetch your site URL</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">let origin = \"\";\nif (typeof window !== \"undefined\") {\n  origin = window.location.origin;\n}\nconst image = origin + imageSrc;</code></pre></div>\n<p>If you use the above code, when you run <code class=\"language-text\">gatsby build</code>, <code class=\"language-text\">location</code> and <code class=\"language-text\">origin</code> will not be available, which will set the <code class=\"language-text\">origin</code> variable to <code class=\"language-text\">undefined</code>. </p>\n<p>You MUST use the <code class=\"language-text\">siteUrl</code> generated from the <code class=\"language-text\">siteMetadata</code> to build the URL or else you may run into issues like <a href=\"https://stackoverflow.com/questions/67942759/twitter-card-images-not-working-on-gatsby-app\">this</a>.</p>\n<p>Finally, add the following image meta tags to your array of tags:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">{\n  property: `og:image`,\n  content: image\n},\n{\n  name: `twitter:image:src`,\n  content: image,\n},</code></pre></div>\n<p>Boom! That’s it.</p>\n<h2>Conclusion</h2>\n<p>Now you can add custom thumbnails to each blog post that will make your links much prettier on social media. You can use the following sources to verify that your card images are working as expected: <a href=\"https://cards-dev.twitter.com/validator\">Twitter Card Validator</a>, <a href=\"https://www.linkedin.com/post-inspector/inspect/\">LinkedIn Post Inspector</a>, and <a href=\"https://developers.facebook.com/tools/debug/\">Facebook Debugger Tool</a>.</p>","frontmatter":{"title":"How to Add Social Media Card Images to Your Gatsby Blog","date":"June 24, 2021","description":"In this tutorial, I will show you a simple three-step process to add Twitter, LinkedIn, and Facebook card images to your Gatsby blog.","thumbnail":{"childImageSharp":{"fixed":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAKABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIDBAX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAGnnxgwof/EABoQAAEFAQAAAAAAAAAAAAAAABEAAQIDEyD/2gAIAQEAAQUCypxEE44//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGxAAAQQDAAAAAAAAAAAAAAAAAAEREiEgMpH/2gAIAQEABj8CeVmy8KV8P//EABsQAAIBBQAAAAAAAAAAAAAAAAERIRAgMUFx/9oACAEBAAE/IQmQxkvaoAQOBxWf/9oADAMBAAIAAwAAABDAD//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EAB0QAQACAQUBAAAAAAAAAAAAAAERIQAgMVFhcfD/2gAIAQEAAT8Qu8OruiPawiJLafrgYZCVaHjR/9k=","width":1200,"height":628,"src":"/static/c2a424644823fb462980ce9cc0fd2656/47498/twitterImage.jpg","srcSet":"/static/c2a424644823fb462980ce9cc0fd2656/47498/twitterImage.jpg 1x"}}}}},"previous":{"fields":{"slug":"/2021/checkmates-in-life/"},"frontmatter":{"title":"You have to find the checkmates in life"}},"next":{"fields":{"slug":"/2021/looking-inside-vs-looking-from-within/"},"frontmatter":{"title":"Looking inside vs looking from within"}}},"pageContext":{"id":"70a7dd72-1916-5cfc-9724-3be72fa33b6a","previousPostId":"3b0a10ba-abb3-5e8e-b042-01d139d1f45c","nextPostId":"ba778cd3-eff5-51d5-be65-936e04df7006"}},
    "staticQueryHashes": ["3771180734","452190831"]}