Giter VIP home page Giter VIP logo

battlebase-og-image-screenshot's Introduction

Netlify Screenshot

Screenshot webpages to render social media cards on-the-fly using Puppeteer; largely based on how Pieter generates shareable pictures for Nomad List, and how I did them for Coworkations with cardserver.

Deploy to Netlify

Coworkations ๐Ÿ“„ HTML ๐Ÿ–ผ๏ธ PNG Hacker Paradise: Cape Town South Africa ๐Ÿ“„ HTML ๐Ÿ–ผ๏ธ PNG
Nomad Cruise VI: Spain To Greece ๐Ÿ“„ HTML ๐Ÿ–ผ๏ธ PNG PACK: Ubud Bali ๐Ÿ“„ HTML ๐Ÿ–ผ๏ธ PNG

Setup

After deploying to Netlify and setting BASE_URL the site should be good to go, try visiting https://{site-name}.netlify.app/screenshot for a capture of your homepage.

Any additional path that comes after /screenshot is used in the request to your webserver:

https://{site-name}.netlify.app/screenshot/**/* -> {BASE_URL}/**/*

If you want to change the BASE_URL edit the site's environment variables from Site Settings > Build & deploy > Environment, the value should be the root domain of your website e.g. https://example.com.

Usage

Netlify Screenshot performs HTML requests based on PNG requests like so:

๐Ÿ–ผ PNG (Netlify request) ๐Ÿ“„ HTML (webserver request)
https://stevelacey.netlify.app/screenshot/cards/steve.png https://steve.ly/cards/steve
https://coworkations.netlify.app/screenshot/cards/coworkations.png https://coworkations.com/cards/coworkations
https://coworkations.netlify.app/screenshot/cards/hacker-paradise.png https://coworkations.com/cards/hacker-paradise
https://coworkations.netlify.app/screenshot/cards/pack/ubud-bali-2.png https://coworkations.com/cards/pack/ubud-bali-2

If you want to serve your cards on the same domain as your app, you can route PNG traffic to Netlify via NGINX:

location ~ ^/(cards/.*\.png)$ {
  proxy_pass http://{site-name}.netlify.app/screenshot/$1;
}

Then your URLs can look something like this:

๐Ÿ–ผ PNG (Netlify request) ๐Ÿ“„ HTML (webserver request)
https://steve.ly/cards/steve.png https://steve.ly/cards/steve
https://coworkations.com/cards/coworkations.png https://coworkations.com/cards/coworkations
https://coworkations.com/cards/hacker-paradise.png https://coworkations.com/cards/hacker-paradise
https://coworkations.com/cards/pack/ubud-bali-2.png https://coworkations.com/cards/pack/ubud-bali-2

Alternatively, you can route all /screenshots/ traffic to Netlify, then prefix any path on your site with /screenshots/ to capture a screenshot.

location /screenshots/ {
  proxy_pass http://{site-name}.netlify.app/screenshot/;
}

Note that query params are passed through to your backend, which can be useful if you want to toggle features like Cookiebot or Intercom on/off.

Caching

Netlify Screenshot serves sensible cache headers, and allows you to suffix requests with .png which helps reverse proxies like Cloudflare recognize the content as cacheable.

In addition to that, you might want to cache results locally so they don't hit Netlify every time.

To do that, add something like this to your nginx.conf:

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=static:10m max_size=10g inactive=7d use_temp_path=off;
proxy_cache_valid 7d;

And update your location block like so:

location /screenshots/ {
  proxy_cache static;
  proxy_pass http://{site-name}.netlify.app/screenshot/;
}

Markup

Youโ€™ll want meta tags something like these:

<meta itemprop="image" content="https://coworkations.com/cards/coworkations.png">
<meta property="og:image" content="https://coworkations.com/cards/coworkations.png">
<meta name="twitter:image" content="https://coworkations.com/cards/coworkations.png">

Debugging

battlebase-og-image-screenshot's People

Contributors

stevelacey avatar

Watchers

Mark McFarlane avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.