Next.js integration tutorial

Add Mosaicora to Next.js

Follow a complete App Router setup for Mosaicora Open Graph images, route metadata, and typed JSON-LD render overrides.

What you will build

A Next.js route that publishes a deterministic Mosaicora image URL and optional JSON-LD values that tell the Mosaicora generator exactly what content to render in the Open Graph image.

Package: @mosaicora/plugin-nextjs 1.0.0Framework: Next.js 16 and React 19Runtime: Node.js 20+License: MIT

Before you begin

  • A Next.js 16 App Router project using React 19 and Node.js 20 or newer.
  • The public canonical URL for the route you want to connect.
  • Your public Mosaicora site ID. A site ID identifies the site configuration; it is not a secret API credential.
  • A route that can export static metadata or generateMetadata from a Server Component.
1

Step 1

Install the Next.js package

Add the native App Router helpers to the application that publishes your metadata.

Run the installation command from the root of your Next.js project. The package includes the framework-agnostic Mosaicora core as a dependency.

Install with pnpm

pnpm add @mosaicora/plugin-nextjs

Expected result

  • The dependency appears in package.json.
  • Your package manager completes without peer dependency errors.
2

Step 2

Prepare the canonical page data

Use the same absolute canonical URL for the page, metadata, and Mosaicora image path.

Resolve the canonical URL from trusted application data. Dynamic route query parameters are preserved in deterministic order, while hash fragments are ignored.

Use fallbackHref when a route can be reached through an alternate URL or when your publishing data needs a stable fallback.

Route values

const siteId = "321cac22d2103fb1660c50bd";
const canonicalUrl =
  "https://example.com/products/view?sku=123&campaign=launch";
3

Step 3

Create the Open Graph metadata

Generate matching Open Graph and X/Twitter image metadata with createMosaicoraMetadata.

Spread the helper output into the metadata returned by your route. You can add the page title, description, canonical alternate, and other standard Next.js metadata alongside it.

For dynamic routes, resolve the route data inside generateMetadata and pass the final canonical URL to the helper.

Static App Router metadata

import type { Metadata } from "next";
import { createMosaicoraMetadata } from "@mosaicora/plugin-nextjs";

const canonicalUrl = "https://example.com/products/view?sku=123";

export const metadata: Metadata = {
  title: "Example product",
  alternates: { canonical: canonicalUrl },
  ...createMosaicoraMetadata({
    siteId: "321cac22d2103fb1660c50bd",
    canonicalHref: canonicalUrl,
    fallbackHref: canonicalUrl,
    alt: "Example product social preview",
  }),
};

Dynamic route metadata

export async function generateMetadata({ params }): Promise<Metadata> {
  const { slug } = await params;
  const product = await getProduct(slug);

  return {
    title: product.title,
    description: product.description,
    ...createMosaicoraMetadata({
      siteId: "321cac22d2103fb1660c50bd",
      canonicalHref: product.canonicalUrl,
      fallbackHref: product.canonicalUrl,
      alt: product.title,
    }),
  };
}

Expected result

  • The rendered page contains og:image and twitter:image tags.
  • Both tags point to the same cdn.mosaicora.io image URL.
4

Step 4

Add JSON-LD render overrides

Tell the Mosaicora generator which exact page values to render in the image.

MosaicoraOgJsonLd preserves normal Schema.org fields and adds the typed mosaicora:og v3 block. The override is not a replacement for SEO metadata: it gives the image generator precise content such as the title, description, price, and feature list.

Place the component in the page or a route-specific server component so its values describe the same canonical page as the metadata.

Product JSON-LD override

import { MosaicoraOgJsonLd } from "@mosaicora/plugin-nextjs";

export default function ProductJsonLd() {
  return (
    <MosaicoraOgJsonLd
      schemaType="Product"
      name="Example product"
      description="A polished preview for every product page."
      url="https://example.com/products/view?sku=123"
      offers={{
        "@type": "Offer",
        price: "49",
        priceCurrency: "USD",
      }}
      mosaicoraOg={{
        schemaVersion: 3,
        semanticValues: {
          "content.title": "Example product",
          "content.description": "A polished product preview.",
          "product.price": "$49",
          "product.features": ["Fast setup", "Consistent previews"],
        },
      }}
    />
  );
}
5

Step 5

Verify the deployed route

Confirm the final HTML exposes the image URL and render instructions to crawlers.

Deploy or expose the route at its public canonical URL. Inspect the rendered HTML, then run that URL through the Mosaicora Open Graph Checker.

Check that the canonical URL, og:image, twitter:image, image dimensions, alt text, and mosaicora:og semantic values all describe the same page.

Expected result

  • The route returns a successful public response.
  • The social preview uses the expected Mosaicora URL.
  • The JSON-LD script parses and contains schemaVersion 3.

Reference

Advanced usage

Deterministic URL behavior

The helper creates stable image addresses from the page identity.

  • Every query parameter is preserved and sorted by key and value.
  • The .jpg suffix appears before the query string.
  • Hash fragments are ignored.
  • UTF-8 path segments remain readable.

Public exports

Use the high-level Next.js helpers or the re-exported core primitives.

  • getMosaicoraOgImageUrl
  • createMosaicoraMetadata
  • MosaicoraOgJsonLd
  • Core URL helpers, JSON-LD builders, semantic values, and role-specific types

Help

Troubleshooting

The metadata is missing from the initial HTML

Keep metadata exports in a Server Component route. Do not move metadata generation into a client component or a browser-only effect.

Different URLs generate unexpected image paths

Normalize one absolute canonical URL and reuse it for alternates, canonicalHref, fallbackHref, and the JSON-LD url field.

The image renders, but uses the wrong text

Inspect the mosaicora:og semanticValues. Add explicit title, description, price, or feature overrides for the values the generator should render exactly.

Build Better Open Graph Images

Join the waitlist and tell us what your publishing workflow needs.