Skip to content

Commit

Permalink
fix(seo): Add OG image for home (#2471)
Browse files Browse the repository at this point in the history
Add special case for generating homepage og image
  • Loading branch information
AlexandreGaubert authored Oct 10, 2023
1 parent 01ba132 commit dd09e7a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion gatsby-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export const createPages = async ({actions, graphql}) => {
await Promise.all(data.pages.nodes.map(async ({id, children, absolutePath}) => {
const [{fields, frontmatter}] = children;

await generateImage(frontmatter.title, fields.slug)
if (fields.slug === '/')
await generateImage('Home', fields.slug)
else
await generateImage(frontmatter.title, fields.slug)

await actions.createPage({
path: fields.slug,
Expand Down
4 changes: 2 additions & 2 deletions generate-og-images.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const saveImage = (path, content) => {
}

export const generateImage = async (title, path) => {
if (path === '/' || title == null) return; // Skip Home page
if (title == null) return;
registerFont('./static/fonts/Poppins-Light.ttf', {family: 'Poppins', weight: 300})

const canvas = createCanvas(1200, 630)
Expand Down Expand Up @@ -69,5 +69,5 @@ export const generateImage = async (title, path) => {
}

const buffer = canvas.toBuffer('image/png')
saveImage(resolve(`./static/og-images/${removeTrailingSlash(path)}.png`), buffer)
saveImage(resolve(`./static/og-images/${path === '/' ? 'home' : removeTrailingSlash(path)}.png`), buffer)
};
8 changes: 7 additions & 1 deletion src/components/SEO/SEO.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function SEO({
image: `${siteUrl}${image}`,
url: `${siteUrl}${pathname || ''}`,
};
const isHomepage = pathname === '/';

return (
<Helmet>
Expand All @@ -40,7 +41,12 @@ export default function SEO({
<meta property="og:title" content={seo.longTitle} />
<meta property="og:description" content={seo.description} />
<meta property="og:url" content={seo.url} />
<meta property="og:image" content={`${siteUrl}/og-images${pathname || ''}.png`} />
<meta
property="og:image"
content={isHomepage
? `${siteUrl}/og-images/home.png`
: `${siteUrl}/og-images${pathname || ''}.png`}
/>
<meta property="og:type" content="article" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
Expand Down

0 comments on commit dd09e7a

Please sign in to comment.