1.18.0 performance items: reference 404 placeholder and heading font without preload, lazy form and Turnstile code (eager entry /client/eager), Turnstile space kept, background video paused for reduced motion, card hover on opacity, metadata stripped on upload, MongoDB TTL indexes for the consent log and statistics, unused-media notice, version limit for the plugin's entities
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
076b3ef707
commit
d628839235
@@ -0,0 +1,63 @@
|
||||
import type { BasePayload, Config, DateField } from 'payload'
|
||||
|
||||
/**
|
||||
* MongoDB TTL index for a plugin log (1.18.0; docs/performance.md §6): the database itself deletes records once
|
||||
* they are older than the retention — also on a quiet site, where the purge after a new record never runs.
|
||||
*
|
||||
* Why a field of its own: Payload already keeps a plain index on `createdAt` (and the statistics on `lastAt`), and
|
||||
* Mongoose recreates its indexes at every start — turning that index into a TTL one makes the start fail with
|
||||
* IndexOptionsConflict. So the TTL counts from `iaplTtlFrom` (hidden; the record's `createdAt`, the visit's `lastAt`),
|
||||
* with an index named `iapl_ttl` that only the plugin manages. At every start its time is set to the current
|
||||
* `retentionDays` (collMod), so a changed retention applies at once; records written before 1.18.0 get the field
|
||||
* from their own date once (a single updateMany). Postgres and SQLite: no field, no index — the purge does it.
|
||||
* Opt-out: `ttlIndex: false` on the option; the index is then dropped at the next start (nothing more is deleted
|
||||
* by it), the purge stays.
|
||||
*/
|
||||
export const TTL_FIELD = 'iaplTtlFrom'
|
||||
export const TTL_INDEX = 'iapl_ttl'
|
||||
|
||||
export const isMongo = (config: Pick<Config, 'db'>) => (config.db as { name?: string } | undefined)?.name === 'mongoose'
|
||||
|
||||
/** The hidden date the TTL counts from. Not indexed by Payload (the plugin's own index is the TTL one). */
|
||||
export const ttlField = (): DateField => ({
|
||||
name: TTL_FIELD,
|
||||
type: 'date',
|
||||
admin: { disableBulkEdit: true, hidden: true },
|
||||
})
|
||||
|
||||
type NativeCollection = {
|
||||
collectionName: string
|
||||
createIndex: (key: object, options: object) => Promise<string>
|
||||
dropIndex: (name: string) => Promise<unknown>
|
||||
indexes: () => Promise<Array<{ expireAfterSeconds?: number; name?: string }>>
|
||||
updateMany: (filter: object, update: object[]) => Promise<{ modifiedCount?: number }>
|
||||
}
|
||||
|
||||
export type TtlSyncResult = 'created' | 'dropped' | 'none' | 'updated'
|
||||
|
||||
/** Creates, updates or drops the TTL index of one collection (MongoDB only; anything else does nothing). */
|
||||
export async function syncTtlIndex(
|
||||
payload: BasePayload,
|
||||
slug: string,
|
||||
{ days, enabled, from }: { days: number; enabled: boolean; from: string },
|
||||
): Promise<TtlSyncResult> {
|
||||
const db = payload.db as unknown as { collections?: Record<string, { collection?: NativeCollection }>; connection?: { db?: { command: (c: object) => Promise<unknown> } }; name?: string }
|
||||
const coll = db.collections?.[slug]?.collection
|
||||
if (db.name !== 'mongoose' || !coll) {return 'none'}
|
||||
const existing = (await coll.indexes()).find((i) => i.name === TTL_INDEX)
|
||||
if (!enabled) {
|
||||
if (!existing) {return 'none'}
|
||||
await coll.dropIndex(TTL_INDEX)
|
||||
return 'dropped'
|
||||
}
|
||||
// records from before 1.18.0 (or written by another path): the TTL counts from their own date
|
||||
await coll.updateMany({ [TTL_FIELD]: { $exists: false }, [from]: { $type: 'date' } }, [{ $set: { [TTL_FIELD]: `$${from}` } }])
|
||||
const seconds = Math.round(days * 86_400)
|
||||
if (!existing) {
|
||||
await coll.createIndex({ [TTL_FIELD]: 1 }, { expireAfterSeconds: seconds, name: TTL_INDEX })
|
||||
return 'created'
|
||||
}
|
||||
if (existing.expireAfterSeconds === seconds) {return 'none'}
|
||||
await db.connection!.db!.command({ collMod: coll.collectionName, index: { expireAfterSeconds: seconds, name: TTL_INDEX } })
|
||||
return 'updated'
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* The plugin's own collections and globals turn versions on nowhere. When a project turns them on (globalOverrides,
|
||||
* collectionOverrides, formOverrides…) without a limit, each keeps at most 20 versions per document (1.18.0;
|
||||
* docs/performance.md §6) instead of Payload's 100 — a global like Site integrations is saved by the site itself on
|
||||
* every status change. A limit the project writes wins (`max` for globals, `maxPerDoc` for collections, 0 =
|
||||
* unlimited). Payload trims the oldest versions of a document on its next save.
|
||||
*/
|
||||
export const PLUGIN_VERSION_LIMIT = 20
|
||||
|
||||
type Versioned = { slug: string; versions?: unknown }
|
||||
|
||||
export function withVersionLimit<T extends Versioned>(entity: T, kind: 'collection' | 'global', limit = PLUGIN_VERSION_LIMIT): T {
|
||||
const v = entity.versions
|
||||
if (!v) {return entity}
|
||||
const key = kind === 'global' ? 'max' : 'maxPerDoc'
|
||||
if (v === true) {return { ...entity, versions: { [key]: limit } }}
|
||||
if (typeof v === 'object' && typeof (v as Record<string, unknown>)[key] !== 'number') {return { ...entity, versions: { ...(v as object), [key]: limit } }}
|
||||
return entity
|
||||
}
|
||||
Reference in New Issue
Block a user