New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

next-api-og-image

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-api-og-image

Easy way to generate open-graph images dynamically using Next.js API Routes.

  • 1.1.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
208
decreased by-15.79%
Maintainers
1
Weekly downloads
 
Created
Source

Next.js API OG Image · version types license

Simple library with purpose of providing easy way to dynamically
generate open-graph images using Next.js API routes.

Features

  • 🐄 Super easy usage
  • 🌐 Suitable for serverless environment
  • 🥷 TypeScript compatible

Installing

npm i next-api-og-image -S
# or
yarn add next-api-og-image

Basic usage and explaination

import { withOGImage } from 'next-api-og-image'

export default withOGImage({ html: ({ myQueryParam }) => `<h1>${myQueryParam}</h1>` })

The Next.js API route where this code exists will open headless browser on each request sent, put HTML content in the web page, screenshot it and return as binary data. The parameter of the function bound to html in the configuration object is nothing else but request query parameters. This allows you to create HTML templates.

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'

Configuration

Apart from html configuration property (which is required), you can specify Content-Type and Cache-Control headers and much more.

Example configuration with default values (apart from required html prop):

const nextApiOgImageConfig = {
  /* Remember to specify `html` manually !!! */
  // ------------------------------------------
  // 'Content-Type' HTTP header
  contentType: 'image/png',
  // 'Cache-Control' HTTP header
  cacheControl: 'max-age 3600, must-revalidate',
  // NOTE: These options works only when process.env.NODE_ENV === 'development'
  dev: {
    // Whether to display HTML in place of binary data (image/screenshot)
    inspectHtml: true,
  },
}

Loading custom local fonts

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

Examples

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 examples && npm i && npm run dev then navigate to http://localhost:3000/

License

This project is licensed under the MIT license.
All contributions are welcome.

Keywords

FAQs

Package last updated on 05 Oct 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc