feat: render CMS SEO metadata and refresh published page caches
This commit is contained in:
@@ -27,7 +27,12 @@ export async function fetchAPI<T>(
|
||||
{ revalidate = 60, tags }: FetchOptions = {},
|
||||
): Promise<T> {
|
||||
const res = await fetch(`${API_BASE_URL}${path}`, {
|
||||
next: { revalidate, tags },
|
||||
next: {
|
||||
revalidate,
|
||||
tags: path.startsWith("/api/v2/pages/")
|
||||
? [...new Set(["wagtail-pages", ...(tags ?? [])])]
|
||||
: tags,
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { Metadata } from "next";
|
||||
import { fetchAPI } from "./api-client";
|
||||
import type { WagtailListResponse, WagtailPageSummary } from "@/types/wagtail";
|
||||
|
||||
export const SEO_FIELDS = "seo_title,search_description,seo_title_override,seo_description_override,og_image,canonical_url,schema_json";
|
||||
|
||||
export async function getPageSEO(type: string, slug?: string): Promise<WagtailPageSummary | null> {
|
||||
const query = new URLSearchParams({ type, fields: SEO_FIELDS, limit: "1" });
|
||||
if (slug) query.set("slug", slug);
|
||||
const response = await fetchAPI<WagtailListResponse<WagtailPageSummary>>(
|
||||
`/api/v2/pages/?${query}`,
|
||||
);
|
||||
return response.items[0] ?? null;
|
||||
}
|
||||
|
||||
export function createPageMetadata(page: WagtailPageSummary | null, fallback: string): Metadata {
|
||||
const title = page?.seo_title_override || page?.meta.seo_title;
|
||||
const description = page?.seo_description_override || page?.meta.search_description || undefined;
|
||||
const images = page?.og_image?.url
|
||||
? [{ url: page.og_image.url, alt: page.og_image.title }]
|
||||
: undefined;
|
||||
return {
|
||||
title: title ? { absolute: title } : page?.title || fallback,
|
||||
description,
|
||||
alternates: page?.canonical_url ? { canonical: page.canonical_url } : undefined,
|
||||
openGraph: {
|
||||
title: title || page?.title || fallback,
|
||||
description,
|
||||
url: page?.canonical_url || undefined,
|
||||
images,
|
||||
type: "website",
|
||||
},
|
||||
twitter: { card: images ? "summary_large_image" : "summary", title: title || page?.title || fallback, description, images },
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user