Eleventy Plugin Baseline

A magic carpet ride

Documentation — Table of Contents

Custom Social Previews

Add custom Open Graph and Twitter meta using Baseline. You’ll set site-wide defaults and override per page, then verify the rendered <head>. Uses npm, Node 20.15.0+, and the same Baseline setup as earlier tutorials.

What you’ll build

Prerequisites

1) Ensure Baseline is loaded

eleventy.config.js:

import baseline, { config as baselineConfig } from "@apleasantview/eleventy-plugin-baseline";

/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
export default function (eleventyConfig) {
  eleventyConfig.addPlugin(baseline());
}

export const config = baselineConfig;

2) Set site-wide social defaults

In src/_data/head.js, use openGraph and twitter objects:

export default {
  link: [{ rel: "stylesheet", href: "/assets/css/index.css" }],
  script: [{ src: "/assets/js/index.js", defer: true }],
  openGraph: {
    "og:title": "Baseline Starter",
    "og:description": "An Eleventy site using Baseline",
    "og:type": "website",
    "og:image": "https://example.com/assets/social/default-og.jpg"
  },
  twitter: {
    "twitter:card": "summary_large_image",
    "twitter:title": "Baseline Starter",
    "twitter:description": "An Eleventy site using Baseline",
    "twitter:image": "",
    "twitter:site": "@yoursite",
    "twitter:creator": "@yoursite"
  },
};

3) Create a page with overrides

Create src/content/pages/social-demo.md:

---
title: "Custom Social Preview"
description: "This page overrides social meta."
permalink: "/social-demo/"
layout: "layouts/base.njk"
head:
  openGraph:
    "og:title": "Custom OG Title"
    "og:description": "Custom OG description for this page."
    "og:image": "https://example.com/assets/social/custom-og.jpg"
  twitter:
    "twitter:card": "summary_large_image"
    "twitter:title": "Custom Twitter Title"
    "twitter:description": "Custom Twitter description."
    "twitter:image": "https://example.com/assets/social/custom-twitter.jpg"
---

This page sets its own OG/Twitter meta instead of using the defaults.

4) Run and verify

npx rimraf dist/ && npm run dev
npx rimraf dist/ && npm run build

5) Tips

Previous: Sitemaps & Drafts

Next: Deployment URL Checks