Giter Site home page Giter Site logo

ogimage's Introduction

๐Ÿง‘โ€๐ŸŽจ Social Share Images

Social share images are a must to grab the attention of readers when sharing your content on social media.

๐Ÿคจ What is an Open Graph image?

The Open Graph protocol is extra metadata about your site social media sites can use. One of the meta tags you can include is og:image which is a link to a 1200x630 pixels image that's going to be used as the preview image for your content.

<head>
  <!-- ... -->
  <meta property="og:image" content="og-image.png" />
  <!-- ... -->
</head>

If you have a blog creating a social share image for every post is tedious so I created this to automate it.

๐Ÿค” How does it work?

  • I use SvelteKit because it's easy to create a HTML template and API endpoints but any backend could work if you apply the same principles
  • The HTML template for the social share image is at src/template/index.svelte and accepts any text we pass it
  • Using Puppeteer and visiting the API endpoint at http://localhost:3000/images/screenshot.png navigates to the HTML template and takes a screenshot which is served from the endpoint and cached on Vercel's CDN forever using serverless

Because serverless functions are limited to 50 MB the Chromium binary has to be small enough to not exceed that limit so I use chrome-aws-lambda and puppeteer-core.

In development you can uncomment the process.env.AWS_LAMBDA_FUNCTION_NAME = true line so chrome-aws-lambda thinks it's in a serverless environment and uses the Chromium binary.

If you go over the 50 MB size limit because of the size of the Chromium binary you can use an older version of chrome-aws-lambda to reduce the size.

๐Ÿ“œ Setup

The project uses ๐Ÿ“ฆ๏ธ pnpm but you can use any package manager.

๐Ÿ‘ฌ Clone the project.

git clone https://github.com/mattcroat/og-image.git

๐Ÿ“ฆ๏ธ Install the dependencies.

pnpm i

๐Ÿ“œ Run the development server with pnpm run dev or build and preview the project with pnpm run build && pnpm run preview.

pnpm run dev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.