1.2.188 • Published 1 year ago

next-banner v1.2.188

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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.

1.2.134

2 years ago

1.2.133

2 years ago

1.2.136

2 years ago

1.2.135

2 years ago

1.2.138

2 years ago

1.2.137

2 years ago

1.2.139

2 years ago

1.2.145

2 years ago

1.2.144

2 years ago

1.2.147

2 years ago

1.2.146

2 years ago

1.2.149

2 years ago

1.2.148

2 years ago

1.2.141

2 years ago

1.2.140

2 years ago

1.2.143

2 years ago

1.2.142

2 years ago

1.2.156

2 years ago

1.2.155

2 years ago

1.2.158

2 years ago

1.2.157

2 years ago

1.2.159

2 years ago

1.2.150

2 years ago

1.2.152

2 years ago

1.2.151

2 years ago

1.2.154

2 years ago

1.2.153

2 years ago

1.2.167

2 years ago

1.2.166

2 years ago

1.2.169

2 years ago

1.2.168

2 years ago

1.2.161

2 years ago

1.2.160

2 years ago

1.2.163

2 years ago

1.2.162

2 years ago

1.2.165

2 years ago

1.2.164

2 years ago

1.2.178

2 years ago

1.2.177

2 years ago

1.2.179

2 years ago

1.2.170

2 years ago

1.2.172

2 years ago

1.2.171

2 years ago

1.2.174

2 years ago

1.2.173

2 years ago

1.2.176

2 years ago

1.2.175

2 years ago

1.2.188

1 year ago

1.2.181

2 years ago

1.2.180

2 years ago

1.2.183

2 years ago

1.2.182

2 years ago

1.2.185

1 year ago

1.2.184

1 year ago

1.2.187

1 year ago

1.2.186

1 year ago

1.2.130

2 years ago

1.2.132

2 years ago

1.2.131

2 years ago

1.2.112

2 years ago

1.2.111

2 years ago

1.2.114

2 years ago

1.2.113

2 years ago

1.2.116

2 years ago

1.2.115

2 years ago

1.2.118

2 years ago

1.2.117

2 years ago

1.2.110

2 years ago

1.2.109

2 years ago

1.2.123

2 years ago

1.2.122

2 years ago

1.2.125

2 years ago

1.2.124

2 years ago

1.2.127

2 years ago

1.2.126

2 years ago

1.2.129

2 years ago

1.2.128

2 years ago

1.2.121

2 years ago

1.2.120

2 years ago

1.2.119

2 years ago

1.2.0

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.81

2 years ago

1.2.82

2 years ago

1.2.80

2 years ago

1.2.85

2 years ago

1.2.86

2 years ago

1.2.83

2 years ago

1.2.84

2 years ago

1.2.89

2 years ago

1.2.87

2 years ago

1.2.88

2 years ago

1.2.92

2 years ago

1.2.93

2 years ago

1.2.90

2 years ago

1.2.91

2 years ago

1.2.96

2 years ago

1.2.97

2 years ago

1.2.94

2 years ago

1.2.95

2 years ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.98

2 years ago

1.2.10

2 years ago

1.2.99

2 years ago

1.2.11

2 years ago

1.2.16

2 years ago

1.2.17

2 years ago

1.2.14

2 years ago

1.2.15

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.2.60

2 years ago

1.2.63

2 years ago

1.2.64

2 years ago

1.2.61

2 years ago

1.2.62

2 years ago

1.2.67

2 years ago

1.2.68

2 years ago

1.2.65

2 years ago

1.2.66

2 years ago

1.2.69

2 years ago

1.2.70

2 years ago

1.2.71

2 years ago

1.2.74

2 years ago

1.2.75

2 years ago

1.2.72

2 years ago

1.2.73

2 years ago

1.2.9

2 years ago

1.2.78

2 years ago

1.2.79

2 years ago

1.2.76

2 years ago

1.2.77

2 years ago

1.2.41

2 years ago

1.2.42

2 years ago

1.2.40

2 years ago

1.2.45

2 years ago

1.2.46

2 years ago

1.2.43

2 years ago

1.2.44

2 years ago

1.2.49

2 years ago

1.2.47

2 years ago

1.2.48

2 years ago

1.2.101

2 years ago

1.2.100

2 years ago

1.2.103

2 years ago

1.2.102

2 years ago

1.2.105

2 years ago

1.2.104

2 years ago

1.2.107

2 years ago

1.2.106

2 years ago

1.2.52

2 years ago

1.2.53

2 years ago

1.2.50

2 years ago

1.2.51

2 years ago

1.2.56

2 years ago

1.2.57

2 years ago

1.2.54

2 years ago

1.2.55

2 years ago

1.2.58

2 years ago

1.2.59

2 years ago

1.2.18

2 years ago

1.2.19

2 years ago

1.2.20

2 years ago

1.2.23

2 years ago

1.2.108

2 years ago

1.2.24

2 years ago

1.2.21

2 years ago

1.2.22

2 years ago

1.2.27

2 years ago

1.2.28

2 years ago

1.2.25

2 years ago

1.2.26

2 years ago

1.2.29

2 years ago

1.2.30

2 years ago

1.2.31

2 years ago

1.2.34

2 years ago

1.2.35

2 years ago

1.2.32

2 years ago

1.2.33

2 years ago

1.2.38

2 years ago

1.2.39

2 years ago

1.2.36

2 years ago

1.2.37

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

0.0.1

3 years ago