All Projects → alvarlagerlof → next-banner

alvarlagerlof / next-banner

Licence: MIT license
🖼️ Generate Open Graph images for Next.js on build

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
CSS
56736 projects

Projects that are alternatives of or similar to next-banner

personal-website
Personal website – made with Next.js, Preact, MDX, RMWC, & Vercel
Stars: ✭ 16 (-64.44%)
Mutual labels:  next, vercel
next-api-og-image
Easy way to generate open-graph images dynamically in HTML or React using Next.js API Routes. Suitable for serverless environment.
Stars: ✭ 179 (+297.78%)
Mutual labels:  next, vercel
personal-website
My personal website, statically generated by Next.js
Stars: ✭ 16 (-64.44%)
Mutual labels:  next, vercel
bradgarropy.com
🏠 my home on the web
Stars: ✭ 58 (+28.89%)
Mutual labels:  next, vercel
next-api-decorators
Collection of decorators to create typed Next.js API routes, with easy request validation and transformation.
Stars: ✭ 187 (+315.56%)
Mutual labels:  next, vercel
Seotools
SEO Tools for Laravel
Stars: ✭ 2,406 (+5246.67%)
Mutual labels:  opengraph
blog
My personal blog.
Stars: ✭ 13 (-71.11%)
Mutual labels:  vercel
Embed
Get info from any web service or page
Stars: ✭ 1,808 (+3917.78%)
Mutual labels:  opengraph
Open Graph
Library that assists in building Open Graph meta tags
Stars: ✭ 112 (+148.89%)
Mutual labels:  opengraph
datasette-publish-vercel
Datasette plugin for publishing data using Vercel
Stars: ✭ 30 (-33.33%)
Mutual labels:  vercel
next-all-in
🗃⚛️ Next starter for creating any type of site
Stars: ✭ 29 (-35.56%)
Mutual labels:  next
go-opengraph
Golang package for parsing OpenGraph data from HTML into regular structures
Stars: ✭ 66 (+46.67%)
Mutual labels:  opengraph
Meta Tags
Search Engine Optimization (SEO) for Ruby on Rails applications.
Stars: ✭ 2,464 (+5375.56%)
Mutual labels:  opengraph
opendaylight-sample-apps
Sample applications for use with OpenDaylight (https://www.opendaylight.org/)
Stars: ✭ 56 (+24.44%)
Mutual labels:  next
Ngmeta
Dynamic meta tags in your AngularJS single page application
Stars: ✭ 152 (+237.78%)
Mutual labels:  opengraph
website
My personal website.
Stars: ✭ 29 (-35.56%)
Mutual labels:  vercel
Puppeteer Social Image
Create dynamic social share images using HTML + CSS via puppeteer 🎁
Stars: ✭ 141 (+213.33%)
Mutual labels:  opengraph
bifrost
🌉 The rainbow bridge. URL shortener for Vercel.
Stars: ✭ 28 (-37.78%)
Mutual labels:  vercel
react-ogp
🌐 The ultimate React OpenGraph component
Stars: ✭ 27 (-40%)
Mutual labels:  opengraph
next-ts-starter
A Next.JS powered Typescript starter with styled components, styled-system, framer motion, jest and cypress
Stars: ✭ 27 (-40%)
Mutual labels:  next

⚠️ Archived ⚠️

You should probably use https://github.com/vercel/satori instead. It is much faster, and renders without needing a browser, making it plausible to run dynmically. But you can of course also be usied statically if you want to.

Logo

Next Banner

Generate Open Graph images (og:image) at build using Puppeteer.

What is an Open Graph image?

Open Graph is a protocol for structured metadata for websites. Part of that is a specification for preview images referred to as "og:image". When using that, your website gets a nice preview in social media and messaging apps. For an example, check out ogimage.gallery.

Why this library?

You might just design images in Figma. This is doable, but quickly becomes tedious if you have a lot of pages (like blog posts) or want to change the design. Generating images is much more effective.

Most currently existing solutions run on-demand either in a serverless function or in a service. This is wasteful and could be expensive if demand is high. For example, cold starting Puppeteer to take a screenshot of the page can take 8s per visitor. To counteract this, a CDN can be used, which further increases the amount of things needing setup.

With next-banner, none of that is needed. In true Jamstack fashion, this library generates images at build, using existing infrastructure that you already have.

Features

  • Speed. It uses Puppeteer to render pages, but only on instance, meaning there is only one cold start. On an M1, 100 pages are rendered and captured in 18s.
  • Simple setup. Does not require you to touch Puppeteer, CDNs, or serverless functions.
  • Render using React. Your images are captured pages that you code in React just like you are used to. No SVGs or special template languages.
  • Multiple layouts. You could have one layout for a start page and another for blog posts.
  • Pass any data. Page title and meta description is passed to the layout pages by default, but you can include any data in any structure you want.

Usage

Installation

Use npm or yarn

npm install next-banner
yarn add next-banner

Add this to your scripts in package.json

"postbuild": "next-banner",

Configuration

Edit next.config.js to wrap wrap the config with withNextBanner. The domain property is needed for some social media sites to render the images.

const { withNextBanner } = require("next-banner");

module.exports = withNextBanner({
  nextBanner: {
    domain: "example.com",
  },
  // ... normal Next.js config here.
});

Meta provider

You also need to wrap your _app with <NextBannerMeta> like below. This ads og:image tags to every page and automatically points them to the generated images.

import { NextBannerMeta } from "next-banner";

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <NextBannerMeta>
      <Component {...pageProps} />
    </NextBannerMeta>
  );
}

Layout files

Create a folder called next-banner-layouts/ in your pages/ folder. Then create a file called default.js there and add the following code:

import { Template } from "next-banner";

export default Template;

Custom layouts

To use a custom layout you first need to declare that a page should render another layout using hte setBannerData hook.

pages/post.jsx

import { setBannerData } from "next-banner";

function PostPage() {
  setBannerData({
    layout: "post" // This is the name of the layout file.
  })

  return (
    ...
  )
}

Then you need a layout file. Notice the default (=) parameters in the destructuring. This helps during local development. In production, the hook will return the real data. But locally, data has not been extracted from the pages.

pages/next-banner-layouts/post.jsx

export default function PostLayout() {
  const {
    meta: {
      title = "Placeholder title",
      description = "Placeholder description"
    }
  } = useBannerData();

  return (
    <ScreenshotCanvas>
      <div
        style={{
          display: "flex",
          flexDirection: "row",
          alignItems: "center",
          gap: "20px",
          marginBlockEnd: "30px",
        }}
      >
        <img src="/favicon.ico" alt="" style={{ marginBottom: "70px" }} />

        <div>
          <h1 style={{ fontSize: "5em" }}>{title}</h1>
          <h2 style={{ fontSize: "2em" }}>{description}</h2>
        </div>
    </div>
  </ScreenshotCanvas>
  );
}

Custom data

If you want to add any extra data besides the meta title and description, you can do so with a the setBannerData on the normal (non-layout) page.

import { setBannerData } from "next-banner";

function ImagePage() {
  setBannerData({
    custom: {
      image: "https://example.com/image.jpg"
    }
  })
}

It can then be accessed in layout files using useBannerData.

import { useBannerData } from "next-banner"

function ImageLayout() {
  const {
    custom: {
      image
    }
  } = useBannerData()

  return (
    ... // Your layout here.
  )
}

Example

There is an example showcasing usage here

License

MIT

Contributing

Contributions are always welcome!

See CONTRIBUTING.md for ways to get started.

Feedback

If you have any feedback, please create an issue reach me on twitter.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].