Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@snelsi/next-api-og-image
Advanced tools
Easy way to generate open-graph images dynamically using Next.js API Routes.
Simple library with purpose of providing easy way to dynamically
generate open-graph images using Next.js API routes.
If you're not familiar with dynamic open-graph images concept - please see vercel/og-image repository's README for very detailed explaination.
you can treat this project as simpler and configurable version of mentioned earlier vercel repository
npm i next-api-og-image -S
# or
yarn add next-api-og-image
You can find more examples here:
the example/
directory contains simple Next.js application implementing next-api-og-image
. To fully explore examples implemented in it by yourself - simply do npm link && cd example && npm i && npm run dev
then navigate to http://localhost:3000/
import { withOGImage } from 'next-api-og-image'
export default withOGImage({ template: { html: ({ myQueryParam }) => `<h1>${myQueryParam}</h1>` } })
import { withOGImage } from 'next-api-og-image'
export default withOGImage({ template: { react: ({ myQueryParam }) => <h1>{myQueryParam}</h1> } })
You've may noticed the html
and react
properties in configuration. Their responsibility is to provide HTML document to image creator (browser screenshot), filled with your values.
⚠️ NOTE
Template cannot be ambigious. You must either
definereact
orhtml
. Never both at once
The html
and react
properties are template providers functions. Each function's first (and only) parameter is nothing else but HTTP request's query params converted to object notation.
This allows you to create fully customized HTML templates by simply accessing these parameters. The preferred way to do that is object destructuring.
⚠️ NOTE
html
andreact
template provider functions
can be defined as asynchronous
import { withOGImage } from 'next-api-og-image'
export default withOGImage({ template: { html: ({ myQueryParam }) => `<h1>${myQueryParam}</h1>` } })
import { withOGImage } from 'next-api-og-image'
export default withOGImage({ template: { react: ({ myQueryParam }) => <h1>{myQueryParam}</h1> } })
if you send GET HTTP request to api route with code presented above e.g. localhost:3000/api/foo?myQueryParam=hello
- it will render heading with content equal to 'hello'
next-api-og-image
allows you to choose strategy for providing values to the template. The available strategies are:
query
(default) - values are passed by query params and GET HTTP request.
These values ⛔️ cannot be nested nor accessed by nested destructuring in template provider function.
body
- values are passed by POST HTTP request and JSON body.
These values ✅ can be nested and accessed by nested destructuring in template provider function.
The strategies are determined by strategy
prop in the configuration. Default strategy is query
.
⚠️ NOTE
Regardless of the strategy - all properties (every single one)
are implicitly casted to string, even very long JSON's nested values
If you're using TypeScript, you probably want to have these things
typed. Well... its actually super easy! Simply add generic types to withOGImage
function.
query
strategy with query params ?foo=hello&bar=friend
will look like this:
export default withOGImage<'query', 'foo' | 'bar'>(/* ... */)
body
strategy with JSON payload { "foo": "hello", "imNested": { "bar": "friend" }}
will look like this:
export default withOGImage<'body', { foo: string, imNested: { bar: string } }>({ strategy: 'body', /* ... */ })
When strategy is set to query
and you're sending POST HTTP request with JSON body or when strategy is set to body
and you're sending GET HTTP request with query params - next-api-og-image
will:
dev: { errorsInResponse: false }
in the configurationKeeping all the templates inline within Next.js API route should not be problematic, but if you prefer keeping things in separate files you can follow the common pattern of creating files like my-template.html.js
or my-template.js
when you define template as react (naming convention is fully up to you) with code e.g.
export default function myTemplate({ myQueryParam }) {
return `<h1>${myQueryParam}</h1>`
}
...or in TypeScript
import type { NextApiOgImageQuery } from 'next-api-og-image'
type QueryParams = 'myQueryParam'
export default function myTemplate({ myQueryParam }: Record<QueryParams, string>) {
return `<h1>${myQueryParam}</h1>`
}
then importing it and embedding in the withOGImage
.
In order to load custom fonts from the project source, you need to create source file with your font in base64 format or simply bind the font file content to the variable in your Next.js API route
Apart from html
and react
configuration property (in template
) (whose are required), you can specify additional info about how next-api-og-image
should behave.
Example configuration with default values (apart from template.html or template.react prop):
const nextApiOgImageConfig = {
// Values passing strategy
strategy: 'query',
// Response's 'Content-Type' HTTP header and browser screenshot type.
type: 'png',
// Screenshot's quality. WORKS ONLY IF 'type' IS SET TO 'jpeg'
quality: 90,
// Width of the image in pixels
width: 1200,
// Height of the image in pixels
height: 630,
// 'Cache-Control' HTTP header
cacheControl: 'max-age 3600, must-revalidate',
// NOTE: Options within 'dev' object works only when process.env.NODE_ENV === 'development'
dev: {
// Whether to replace binary data (image/screenshot) with HTML
// that can be debugged in Developer Tools
inspectHtml: true,
// Whether to set error message in response
// if there are strategy related errors
errorsInResponse: true,
},
}
This project is licensed under the MIT license.
All contributions are welcome.
FAQs
Easy way to generate open-graph images dynamically using Next.js API Routes.
The npm package @snelsi/next-api-og-image receives a total of 0 weekly downloads. As such, @snelsi/next-api-og-image popularity was classified as not popular.
We found that @snelsi/next-api-og-image demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.