Skip to content
SprecherEast
Issue 927

chore: use Payload's and Next.js's native query and caching options, and add a check that fails when a public page stops being cached or asks the database for more

Back to our plan
Open
cifrontendcmstech-debtpriority: high

No ready-made tool was found that catches slow queries or broken caching in a Next.js and Payload site. Both frameworks have native options for this, though, and some of them already fail the build by themselves. This works through those options first, applies the ones that help, and adds a mechanical check only where nothing native exists.

Problem

  • Public pages use the previous caching model of Next (route revalidate, unstable_cache in src/modules/site/data/, revalidatePath in each module's afterChange hooks). Cache Components (cacheComponents, use cache, cacheLife, cacheTag) is the current model in Next 16 and isn't enabled.
  • A public page can become dynamic through something it imports (a cookies or draft-mode read deep in a component), and then it renders for every visitor. docs/reference/routes.md notes this blind spot: it reads only the route file.
  • Nothing measures how many database queries a page makes, so an extra query in a loop goes unseen.

Proposed fix

  1. Native caching options (Next 16 docs in node_modules/next/dist/docs/, read first). Evaluate, record on this issue:
    • In the current model, export const dynamic = 'error' makes the build fail when a page uses request-time APIs or uncached data. That is a native, mechanical guard for pages that must stay static. Check which public pages can take it; pages that read draft mode for previews need testing first.
    • Under Cache Components, ensureStatic does the same per page or layout, and only works with cacheComponents on. Moving to Cache Components is its own decision: list what it would change (the migrating-to-cache-components guide), and propose it as a separate issue; don't migrate here.
    • Self-hosting facts that matter on our one server: the page cache lives on local disk plus an in-memory cache (cacheMaxMemorySize, 50 MB by default); optimized images are cached on disk (minimumCacheTTL, maximumDiskCacheSize); dynamically rendered pages are sent as private, no-store.
    • A build-based check as the fallback if dynamic = 'error' doesn't fit a page: compare the routes the production build prerendered (its prerender manifest) with the list of public routes that must be cached, and fail on a route that dropped out.
  2. Native query options (Payload docs, https://payloadcms.com/llms-full.txt, opens in a new tab and the Performance page). Check the public data reads in src/modules/*/data/ against: select and a small depth (and the config's defaultDepth and maxDepth), limit and pagination: false where a count isn't needed, index: true on fields used in where and sort, joins and populate instead of follow-up reads, one cached Payload instance (getPayload), and blockReferences for blocks shared across fields (smaller admin payloads). Apply what helps, each change with its test, in the module's own data/ or cms/ layer.
  3. The query check. Nothing native fails on query count. Use the adapter's Drizzle logger from the baseline issue (off unless its environment variable is set) to count queries per page render in CI over the page list, and fail when a page goes over its budget from the baseline. This is the custom code allowed by the ladder; record each rung's finding on this issue first. Its log ends with the summary line format the gate log issue counts. It runs when the shared perf gate says query is due, and always on main.
  4. SQLite settings that matter under load (the adapter's wal and busyTimeout) are noted for the capacity issue, not changed here without its numbers.

Acceptance criteria

  • Not done: Findings on this issue for each native option, one line each with a link: used, not used and why
  • Not done: Every public page that can stay static fails the build if it stops being static (natively where possible, the manifest check otherwise)
  • Not done: A query-count check over the page list that fails a test PR adding a query in a loop, and passes once it's reverted
  • Not done: Each query change has a test written first and lives in its module's layer
  • Not done: A separate proposal issue for Cache Components, if the findings support it
  • Not done: The module notes and docs/project.md describe any change in caching behavior in the same PR

Files likely touched

src/modules/*/data/**, src/modules/*/cms/** (indexes), src/app/(frontend)/**/page.tsx (segment config only), src/payload.config.ts (logger switch), .github/workflows/ci.yml, the module READMEs, docs/project.md.

Must not touch

src/app/(payload)/ (generated). The revalidate hooks' behavior, except to fix a bug found here with its test. next.config.* caching settings without numbers from the baseline or capacity issue.

Open questions

  • Where the native guard and the manifest check overlap, keep only one; record which.
See it on GitHub, opens in a new tab