Generate Images Programmatically on the Edge

Beyond Fireship · Beginner ·🌐 Frontend Engineering ·3y ago

Key Takeaways

The video demonstrates how to use Vercel's OG library to generate images programmatically on the edge for Open Graph, improving SEO and clickability of links shared on social media, using JavaScript and Next.js.

Full Transcript

one of the most frustrating things about building a content-driven site is that if you plan to share it on social media you'll want to have a unique and clickable image that'll show up in link previews for every URL that means someone might have to go into Photoshop or figma and design a unique image luckily now things don't have to be that manual and painful because versel just dropped a new library called OG which stands for Original Gangster and allows you to generate open graph images programmatically on the edge the way it works is very simple you use HTML and CSS to design an image then put that code on a back-end API route that will convert the HTML and CSS to an SVG image on the Fly I'm really excited about this feature because it's a perfect use case for my next JS course in that course we build a website like dev2 where users can create their own blog posts search engine optimization is really important and now thanks to OG we can dynamically create a unique image for every single post without ever touching a design tool or uploading an image to a server somewhere here's how a basic implementation looks I've got an xjs app that uses server-side rendering for a dynamic route called blog this route is pretty typical if you know next JS it grabs the ID query parameter from the URL and then fetches data from somewhere to render the page in this case I'm just simulating a database as a local variable here in the code I'm grabbing data like title and Emoji then passing those props down to the front end what we want to do is use this data in the head of the document with meta tags like OG image the problem is that we don't have an image to use here yet the cool thing about this library is that we can now generate one dynamically I'm creating a URL here that goes to an API route called OG that also contains parameters for title and emoji and now we can move on to the fun part where we implement this API route by creating a file called og.jsx you'll first want to install the package with npm then import image response from versel OG if you run it on the edge you should get an extremely fast response anywhere in the world and versel will automatically cache it after the initial response now inside the Handler function we'll First grab our Dynamic data which in this case will be the title and emoji and then provide default values if they're undefined from there we return an image response which itself returns jsx to define the appearance of the image inside this code you can use inline Styles and it also supports Tailwind as well most importantly you'll notice it has a fixed width and height and the goal here is to create something very simple and clickable that will catch your attention when a link is shared on Twitter Discord or whatever they also created a playground where you can actually edit this code in the browser if you prefer and that's pretty much all there is to it we now have an extremely fast way to generate Dynamic images on the Fly thanks for watching and I will see you in the next one

Original Description

A new tool from Vercel called OG can generate images on the edge for Open Graph with some basic JavaScript. This can be a great way to improve SEO and the clickability of links shared on social media. Try out my Next.js Course https://fireship.io/courses/react-next-firebase/ Vercel OG https://vercel.com/blog/introducing-vercel-og-image-generation-fast-dynamic-social-card-images
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Beyond Fireship · Beyond Fireship · 11 of 42

1 Social Media Style Number Formatting in JS
Social Media Style Number Formatting in JS
Beyond Fireship
2 I used 3 different File System APIs in Node.js
I used 3 different File System APIs in Node.js
Beyond Fireship
3 Time is Relative, even in JavaScript
Time is Relative, even in JavaScript
Beyond Fireship
4 How to NOT Screw Up Firebase Environment Variables
How to NOT Screw Up Firebase Environment Variables
Beyond Fireship
5 How to make your JavaScript Bundle Smaller
How to make your JavaScript Bundle Smaller
Beyond Fireship
6 Subtle, yet Beautiful Scroll Animations
Subtle, yet Beautiful Scroll Animations
Beyond Fireship
7 Beyond Surreal? A closer look at NewSQL Relational Data
Beyond Surreal? A closer look at NewSQL Relational Data
Beyond Fireship
8 How to Build a Discord Bot
How to Build a Discord Bot
Beyond Fireship
9 How to make Eyeballs that Follow You Around
How to make Eyeballs that Follow You Around
Beyond Fireship
10 Reverse Engineer Google’s NASA Dart Easter Egg with CSS
Reverse Engineer Google’s NASA Dart Easter Egg with CSS
Beyond Fireship
Generate Images Programmatically on the Edge
Generate Images Programmatically on the Edge
Beyond Fireship
12 WTF are all these config files for?
WTF are all these config files for?
Beyond Fireship
13 NEW Firebase Features Just Dropped
NEW Firebase Features Just Dropped
Beyond Fireship
14 Next.js 13 - The Basics
Next.js 13 - The Basics
Beyond Fireship
15 Make Crazy Art with the NEW OpenAI Dall-e API
Make Crazy Art with the NEW OpenAI Dall-e API
Beyond Fireship
16 How to Setup Node.js with TypeScript in 2023
How to Setup Node.js with TypeScript in 2023
Beyond Fireship
17 Dramatically improve website speed with Partytown
Dramatically improve website speed with Partytown
Beyond Fireship
18 The easiest realtime app I’ve ever built
The easiest realtime app I’ve ever built
Beyond Fireship
19 10 Rendering Patterns for Web Apps
10 Rendering Patterns for Web Apps
Beyond Fireship
20 You don't need Node to use NPM packages
You don't need Node to use NPM packages
Beyond Fireship
21 Sorting Algorithms Explained Visually
Sorting Algorithms Explained Visually
Beyond Fireship
22 ChatGPT Official API First Look
ChatGPT Official API First Look
Beyond Fireship
23 I built an image search engine
I built an image search engine
Beyond Fireship
24 Industrial-scale Web Scraping with AI & Proxy Networks
Industrial-scale Web Scraping with AI & Proxy Networks
Beyond Fireship
25 Next.js Server Actions...  5 awesome things you can do
Next.js Server Actions... 5 awesome things you can do
Beyond Fireship
26 The ultimate guide to web performance
The ultimate guide to web performance
Beyond Fireship
27 I built a fullstack PaLM AI app in just 2 minutes
I built a fullstack PaLM AI app in just 2 minutes
Beyond Fireship
28 I tried 8 different Postgres ORMs
I tried 8 different Postgres ORMs
Fireship
29 I built a *streaming* AI chat app
I built a *streaming* AI chat app
Fireship
30 React VS Svelte...10 Examples
React VS Svelte...10 Examples
Fireship
31 How GitHub Actions 10x my productivity
How GitHub Actions 10x my productivity
Fireship
32 PROOF JavaScript is a Multi-Threaded language
PROOF JavaScript is a Multi-Threaded language
Fireship
33 Mind-blowing page animations are easy now... View Transitions API first look
Mind-blowing page animations are easy now... View Transitions API first look
Fireship
34 I built an Apple Vision Pro app... visionOS tutorial
I built an Apple Vision Pro app... visionOS tutorial
Fireship
35 This UI component library is mind-blowing
This UI component library is mind-blowing
Fireship
36 How I deploy serverless containers for free
How I deploy serverless containers for free
Fireship
37 GitHub Copilot now controls your command line...
GitHub Copilot now controls your command line...
Fireship
38 Build better payment forms using new “embedded” Stripe Checkout
Build better payment forms using new “embedded” Stripe Checkout
Fireship
39 Does Deno 2 really uncomplicate JavaScript?
Does Deno 2 really uncomplicate JavaScript?
Fireship
40 JavaScript performance is weird... Write scientifically faster code with benchmarking
JavaScript performance is weird... Write scientifically faster code with benchmarking
Fireship
41 Is Next.js 15 any good? "use cache" API first look
Is Next.js 15 any good? "use cache" API first look
Beyond Fireship
42 I built a DeepSeek R1 powered VS Code extension…
I built a DeepSeek R1 powered VS Code extension…
Fireship

The video teaches how to use Vercel's OG library to generate images programmatically on the edge for Open Graph, improving SEO and clickability of links shared on social media. This is achieved using JavaScript and Next.js, with a focus on server-side rendering and meta tags.

Key Takeaways
  1. Install Vercel OG library
  2. Create a Next.js app with server-side rendering
  3. Define a dynamic route for blog posts
  4. Use OG library to generate images programmatically
  5. Implement API route for image generation
  6. Use inline styles and Tailwind for image customization
💡 The OG library allows for fast and dynamic image generation on the edge, improving SEO and clickability of links shared on social media.

Related Reads

📰
Web Development training and Placement in Electronic City — Full Stack (HTML, CSS, JS, React, Node…
Learn full-stack web development with HTML, CSS, JS, React, and Node.js and get placement assistance in Electronic City
Medium · JavaScript
📰
Document Object Model [DOM] CRUD Operations
Learn to perform CRUD operations on the Document Object Model (DOM) to dynamically manipulate web page content
Dev.to · Madhan Raj
📰
I Found a Surprisingly Fun Way to Practice Frontend Development
Practice frontend development in a fun way by building football-themed applications on VibeCode Arena
Dev.to AI
📰
The Enter key that submits your form while a Japanese user is still typing
Learn how to prevent the Enter key from submitting a form while a Japanese user is still typing, and why it matters for user experience
Dev.to · greymoth
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →