Built-in statistics (cookieless, consent-gated visitor history); per-page meta descriptions, NoSnippet
Stats: ipalKit({ stats: true }) adds the stats-visits collection, POST /api/ipal/hit, the
/admin/stats view (30-day visits chart, top/entry pages, referrers, devices), a dashboard card
and a nav link. <PageStats> (or <SiteProviders stats>) sends one beacon per page view. Without
consent nothing is stored on the device and no IP is kept: page views are joined by a hash with
an in-memory salt replaced daily. With analytics consent an ipal_vid cookie links visits across
days (server re-checks the consent cookie and policy version; withdrawal deletes the cookie).
Bots, logged-in panel users and prefetches aren't counted. Retention 90 days.
@payloadcms/next added to peerDependencies (DefaultTemplate). Docs: stats.md with privacy text.
Also in this release: meta.description auto-filled from page blocks, <NoSnippet>,
MetaDescriptionCheck in the SEO tab, data-nosnippet on banner/language switcher, GA4 CSP
wildcards, consent version passed to Analytics' default consent.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
305c564d13
commit
4ec27d9ef0
@@ -1,12 +1,22 @@
|
||||
import type { CollectionBeforeChangeHook } from 'payload'
|
||||
|
||||
import { blocksToMarkdown } from '../content/blocksToMarkdown.js'
|
||||
import { describeFromMarkdown } from './plainText.js'
|
||||
|
||||
/**
|
||||
* Maps document fields to SEO meta fields for auto-fill.
|
||||
* Defaults match the Payload website template (title → meta.title).
|
||||
*/
|
||||
export type AutoFillMapping = {
|
||||
/** Document field used to fill meta.description when empty. */
|
||||
/**
|
||||
* Block layout field to derive meta.description from when it's empty and the
|
||||
* `description` field gives nothing. Default 'layout'; false turns it off.
|
||||
*/
|
||||
content?: false | string
|
||||
/** Document field used to fill meta.description when empty. Tried before `content`. */
|
||||
description?: string
|
||||
/** Target length of a description derived from `content`. Default 155. */
|
||||
descriptionLength?: number
|
||||
/** Document field used to fill meta.title when empty. Default: 'title'. */
|
||||
title?: string
|
||||
}
|
||||
@@ -27,32 +37,49 @@ function readString(value: unknown): string | undefined {
|
||||
* editor typed. This runs server-side on every create/update, so editors get
|
||||
* sensible meta without clicking "auto-generate".
|
||||
*
|
||||
* Every page gets its own description: from the `description` field when one
|
||||
* is mapped, otherwise from the page's own blocks. Without it, pages fall back
|
||||
* to the one site-wide description, and Google — seeing the same text on every
|
||||
* page — ignores it and quotes the footer instead.
|
||||
*
|
||||
* The plugin doesn't know the client's collection shape, so the field mapping
|
||||
* is configurable; defaults follow the website template.
|
||||
*/
|
||||
export function buildAutoFillMetaHook(mapping: AutoFillMapping = {}): CollectionBeforeChangeHook {
|
||||
const titleField = mapping.title ?? 'title'
|
||||
const descriptionField = mapping.description
|
||||
const contentField = mapping.content === undefined ? 'layout' : mapping.content
|
||||
const descriptionLength = mapping.descriptionLength ?? 155
|
||||
|
||||
return ({ data }) => {
|
||||
return ({ data, originalDoc }) => {
|
||||
if (!data) {return data}
|
||||
|
||||
// plugin-seo stores meta as a group under `meta`
|
||||
const meta = (data.meta as Record<string, unknown> | undefined) ?? {}
|
||||
// A partial update (REST PATCH, payload.update with a few fields) carries
|
||||
// only what changed — read the rest from the stored document, so a saved
|
||||
// description isn't treated as empty and replaced.
|
||||
const stored = (originalDoc?.meta as Record<string, unknown> | undefined) ?? {}
|
||||
const current = (key: string) => (key in meta ? meta[key] : stored[key])
|
||||
const source = (field: string) => (field in data ? data[field] : originalDoc?.[field])
|
||||
|
||||
// Fill meta.title from the document title when empty
|
||||
if (!readString(meta.title)) {
|
||||
const sourceTitle = readString(data[titleField])
|
||||
if (!readString(current('title'))) {
|
||||
const sourceTitle = readString(source(titleField))
|
||||
if (sourceTitle) {meta.title = sourceTitle}
|
||||
}
|
||||
|
||||
// Fill meta.description from a configured field when empty
|
||||
if (descriptionField && !readString(meta.description)) {
|
||||
const sourceDescription = readString(data[descriptionField])
|
||||
// Fill meta.description from a configured field, else from the page content
|
||||
if (!readString(current('description'))) {
|
||||
const sourceDescription =
|
||||
(descriptionField && readString(source(descriptionField))) ||
|
||||
(contentField &&
|
||||
describeFromMarkdown(blocksToMarkdown(source(contentField)), descriptionLength)) ||
|
||||
undefined
|
||||
if (sourceDescription) {meta.description = sourceDescription}
|
||||
}
|
||||
|
||||
data.meta = meta
|
||||
if (data.meta !== undefined || Object.keys(meta).length) {data.meta = meta}
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user