Skip to content

Commit

Permalink
Added support for gatsby-plugin-image (#5)
Browse files Browse the repository at this point in the history
* Added support for gatsby-plugin-image

* Imports existing IGatsbyImageData type from gatsby-plugin-image

* Gets source for twitterImage separately
  • Loading branch information
crock authored May 14, 2021
1 parent fb6ad03 commit f0cbc6a
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"typescript": "3.7.3"
},
"dependencies": {
"@types/react-helmet": "^6.1.0"
"@types/react-helmet": "^6.1.0",
"gatsby-plugin-image": "^1.5.0"
}
}
8 changes: 6 additions & 2 deletions src/Seo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { FC, useContext } from 'react';
import { Helmet } from 'react-helmet';
import { getSrc } from 'gatsby-plugin-image';

import SEOContext from './SeoContext';

Expand Down Expand Up @@ -92,6 +93,9 @@ const SEO: FC<SeoProps> = ({ post = {}, meta = [], title, postSchema }) => {

const getReadingTime = time => (time === 1 ? '1 minute' : `${time} minutes`);

const ogImage = getSrc(seo?.opengraphImage?.localFile);
const twitterImage = getSrc(seo?.twitterImage?.localFile);

return (
<Helmet
htmlAttributes={{
Expand Down Expand Up @@ -129,7 +133,7 @@ const SEO: FC<SeoProps> = ({ post = {}, meta = [], title, postSchema }) => {
},
{
property: 'og:image',
content: seo?.opengraphImage?.sourceUrl || social?.facebook?.defaultImage?.mediaItemUrl,
content: ogImage || seo?.opengraphImage?.sourceUrl || social?.facebook?.defaultImage?.mediaItemUrl,
},
{
property: 'og:image:alt',
Expand All @@ -153,7 +157,7 @@ const SEO: FC<SeoProps> = ({ post = {}, meta = [], title, postSchema }) => {
},
{
name: 'twitter:image',
content: seo?.twitterImage?.sourceUrl,
content: twitterImage || seo?.twitterImage?.sourceUrl,
},
{
name: 'twitter:image:alt',
Expand Down
5 changes: 3 additions & 2 deletions src/SeoContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createContext } from 'react';
import { IGatsbyImageData } from 'gatsby-plugin-image';

interface IPost {
title: string;
Expand All @@ -23,9 +24,9 @@ interface IFixed {
width: number;
height: number;
}

interface IChildImageSharp {
fixed: IFixed;
fixed?: IFixed;
gatsbyImageData?: IGatsbyImageData;
}

interface ILocalFile {
Expand Down
Loading

0 comments on commit f0cbc6a

Please sign in to comment.