A fun project demonstrating dynamic OGP (Open Graph Protocol) image generation using Cloudflare Workers, Hono, and Satori.
This project generates various types of OGP images on the fly based on the request URL. Instead of serving static images, it creates them dynamically, allowing for real-time information to be embedded within the image shared on social media or messaging platforms.
Currently, the following dynamic OGP image generators are implemented:
- /img/timestamp: Displays the current date and time in JST (Asia/Tokyo).
- /img/uuid: Displays a newly generated UUID (v4).
- /img/ulid: Displays a newly generated ULID.
- /img/yojijukugo?n={number}: Displays a random Japanese four-character idiom (Yojijukugo), its reading, and meaning. The idiom is selected based on the
nquery parameter. - /img/wiki?t={title}: Displays a random Wikipedia article title fetched from the Japanese Wikipedia API. The title is passed via the
tquery parameter. - /fortune: (Redirects) Provides a link to a page displaying a random fortune (redirects to an external static image URL based on the fortune).
Each feature also has a corresponding HTML endpoint (e.g., /timestamp, /uuid, /yojijukugo, /wiki) that serves an HTML page with the appropriate meta tags pointing to the dynamic OGP image URL.
- Runtime: Cloudflare Workers
- Framework: Hono
- SVG Generation: Satori (JSX to SVG)
- PNG Conversion: @resvg/resvg-wasm (SVG to PNG using WASM)
- Layout Engine: yoga-wasm-web (Flexbox layout for Satori, WASM)
- Language: TypeScript
- Package Manager: npm (or pnpm/yarn)
-
Clone the repository:
git clone https://github.com/YOUR_USERNAME/ogp-playground.git # Replace with your repo URL cd ogp-playground
-
Install dependencies:
npm install # or # pnpm install # or # yarn install
To run the development server locally (using wrangler):
npm run devThis will typically start a server at http://localhost:8787.
-
HTML Pages (for sharing):
https://<your-worker-url>/timestamphttps://<your-worker-url>/uuidhttps://<your-worker-url>/ulidhttps://<your-worker-url>/fortunehttps://<your-worker-url>/yojijukugohttps://<your-worker-url>/wiki
-
Direct Image Generation:
https://<your-worker-url>/img/timestamphttps://<your-worker-url>/img/uuidhttps://<your-worker-url>/img/ulidhttps://<your-worker-url>/img/yojijukugo?n=<number>(e.g.,n=10)https://<your-worker-url>/img/wiki?t=<title>(e.g.,t=Example%20Title)
Replace <your-worker-url> with the actual URL of your deployed Cloudflare Worker. The base endpoint https://ogp-playground.ponyo877.workers.dev is used within the code for generating image URLs in the HTML pages. You might need to update this in src/index.tsx if you deploy to a different domain.
Deploy the application to Cloudflare Workers using Wrangler:
npm run deployMake sure you have wrangler installed and configured with your Cloudflare account.
Contributions are welcome! Please feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.