vercel/og
The @vercel/og
Pages Plugin is a middleware which renders social images for webpages. It also includes an API to create arbitrary images.
As the name suggests, it is powered by @vercel/og
↗. This plugin and its underlying Satori ↗ library was created by the Vercel team.
To install the @vercel/og
Pages Plugin, run:
The Plugin takes an object with six properties:
-
imagePathSuffix
: the path suffix to make the generate image available at. For example, if you mount this Plugin atfunctions/blog/_middleware.ts
, set theimagePathSuffix
as/social-image.png
and have a/blog/hello-world
page, the image will be available at/blog/hello-world/social-image.png
. -
component
: the React component that will be used to render the image. By default, the React component is given apathname
property equal to the pathname of the underlying webpage (for example,/blog/hello-world
), but more dynamic properties can be provided with theextractors
option. -
extractors
: an optional object with two optional properties:on
andonDocument
. These properties can be set to a function which takes an object and returns aHTMLRewriter
element handler or document handler respectively. The object parameter can be mutated in order to provide the React component with additional properties. In the example above, you will use an element handler to extract theog:title
meta tag from the webpage and pass that to the React component as theogTitle
property. This is the primary mechanism you will use to create dynamic images which use values from the underlying webpage. -
options
: an optional object which is given directly to the@vercel/og
library ↗. -
onError
: an optional function which returns aResponse
or a promise of aResponse
. This function is called when a request is made to theimagePathSuffix
andextractors
are provided but the underlying webpage is not valid HTML. Defaults to returning a404
response. -
autoInject
: an optional object with an optional property:openGraph
. If set totrue
, the Plugin will automatically set theog:image
,og:image:height
andog:image:width
meta tags on the underlying webpage.
Use this Plugin’s API to generate arbitrary images, not just as middleware.
For example, the below code will generate an image saying “Hello, world!” which is available at /greet
.
This is the same API that the underlying @vercel/og
library ↗ offers.