Create a new Next.js Project
Open your terminal and write this following command:
Next.js generator will be show like this:
Change your directory with this following command:
and open with your code editor or IDE.eg:
or
Project Structure
I created api folder to store my api routes and also created fonts folder to store my fonts. You can use any font you want.
Implement the dynamic opengraph route
Output
Code Explanation
- Import the necessary modules and constants.
- Set Runtime: Declare runtime as 'edge'.
- Define an asynchronous function to handle GET requests.
- Use the fetch API to get the Outfit font data and convert it to an array buffer.
- Generate an ImageResponse object with dynamic JSX content.
- Specify options for the ImageResponse, including width, height, and font details.
Test your dynamic opengraph route
- pnpm dev
- open your browser and enter your dynamic open graph route in your browser address bar.
- example: http://localhost:3000/og?title=hello
For more information, check out the official documentation on Open Graph.