How to generate dynamic OG images in Nuxt
In Nuxt, sign a Snapsbrew image URL in a server route and set it with useSeoMeta. The signing key stays in the private runtime config, so the browser never sees it. The server route takes the slug of a page, not free text, so nobody can use it to render other images.
By Alejandro Medina, maintainer of Snapsbrew. Published . Last updated .
This guide gives each page of a Nuxt 3 or Nuxt 4 site its own og:image. A Snapsbrew template makes the image. Your Nuxt server only signs a URL. The first crawler that opens the URL makes the instance render the image, and later requests get the stored file.
The Nuxt part has one trap. Code in a page component runs on the server and also in the browser. A secret in that code ends up in the browser bundle. So the signing happens in a server route, and the page reads the finished URL.
What you need
- A Snapsbrew instance that the public internet can reach. Read Install with Docker.
- A published template with the
open_graphsize preset (1200x630) and two text layers:titleandsubtitle. The Next.js guide shows the steps. - A signed-URL key from Account > Developers > Signed URL API keys.
Step 1: Add the private runtime config
Keys at the top level of runtimeConfig are private. Only server code can read them. Keys under public go to the browser.
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
snapsbrewUrl: '',
snapsbrewSigningKey: '',
snapsbrewOgTemplate: '',
},
})
Set the values with environment variables. Nuxt maps NUXT_ plus the key in upper snake case to each key:
NUXT_SNAPSBREW_URL=https://snapsbrew.example
NUXT_SNAPSBREW_SIGNING_KEY=<signed-url-key>
NUXT_SNAPSBREW_OG_TEMPLATE=<template-uuid>
Step 2: Install the SDK
The JavaScript SDK is on GitHub, not on npm yet. It has no runtime dependencies and runs on Node.js 20 or later, Bun, Deno and Cloudflare Workers, so it works with each Nitro preset.
npm install github:amedina068/snapsbrew-js
Step 3: Sign the URL in a server route
The route takes a slug, finds the page, and returns the signed URL. It does not accept free text. If it did, anybody could call it to render any text on your instance.
// server/api/og/[slug].get.ts
import { signedUrl } from 'snapsbrew'
import { getPost } from '~~/server/utils/posts'
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, 'slug')!
const post = await getPost(slug)
if (!post) throw createError({ statusCode: 404, statusMessage: 'No such post' })
const config = useRuntimeConfig(event)
const url = await signedUrl({
baseUrl: config.snapsbrewUrl,
key: config.snapsbrewSigningKey,
templateId: config.snapsbrewOgTemplate,
variants: [
{ layer: 'title', text: post.title },
{ layer: 'subtitle', text: `${post.author} · ${post.date}` },
],
})
return { url }
})
getPost is your own data function. It can read Nuxt Content, a CMS or a database.
Step 4: Set the meta tags in the page
<!-- pages/blog/[slug].vue -->
<script setup lang="ts">
const route = useRoute()
const { data: post } = await useFetch(`/api/posts/${route.params.slug}`)
const { data: og } = await useFetch(`/api/og/${route.params.slug}`)
useSeoMeta({
title: () => post.value?.title,
ogTitle: () => post.value?.title,
ogImage: () => og.value?.url,
ogImageWidth: 1200,
ogImageHeight: 630,
twitterCard: 'summary_large_image',
twitterImage: () => og.value?.url,
})
</script>
On the first server render, useFetch calls the route on the server. The result goes to the browser in the payload, so the browser does not call the route again for that page. The key never leaves the server.
Step 5: Test it
Request the page and read the tag:
curl -s http://localhost:3000/blog/hello-world | grep -o '<meta property="og:image"[^>]*>'
Then request the image URL with curl -sI. A 302 with a Location header is a good render. A 200 with a small gray PNG is the placeholder: the key, the signature or the template ID is wrong. The Next.js guide lists each response.
A prerendered site
With nuxt generate or routeRules with prerender: true, the server route runs at build time. The signed URL is in the static HTML. This site, snapsbrew.com, is a prerendered Nuxt site.
The first request for each image still renders it on your instance. To render the images before anyone shares them, request each og:image URL one time after the deploy:
for page in $(grep -o '<loc>[^<]*' .output/public/sitemap.xml | cut -c6-); do
img=$(curl -s "$page" | grep -o 'og:image" content="[^"]*' | cut -d'"' -f3 | sed 's/&/\&/g')
[ -n "$img" ] && curl -s -o /dev/null "$img"
done
When the design changes
Edit the template in the editor and publish it. The publish changes the cache key of every signed URL of that template. The next request for each URL renders the new design. You do not deploy the Nuxt site again.
Other frameworks
The method is the same in each framework: sign on the server, put the URL in the og:image tag.
- Next.js: How to generate dynamic OG images in Next.js
- Laravel and Rails: How to generate OG images in Laravel
- Python: the Python SDK has
signed_url(). Read SDKs.
Questions
- Why not sign the URL directly in useSeoMeta?
- Code in a page runs on the server and also in the browser after a client-side navigation. If the page signs the URL, the signing key must be in the browser bundle. A server route keeps the key on the server.
- Does this work with a fully prerendered Nuxt site?
- Yes. During
nuxt generate,useFetchcalls the server route at build time. The signed URL goes into the HTML. The site needs no server after the build. - Can I use the nuxt-og-image module instead?
- Yes. That module renders the image inside your Nuxt app. A signed URL moves the render to your Snapsbrew instance and lets a designer change the template with no deploy. Read Dynamic OG images compared.