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 planOpen
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_cacheinsrc/modules/site/data/,revalidatePathin 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.mdnotes 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
- 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,
ensureStaticdoes the same per page or layout, and only works withcacheComponentson. Moving to Cache Components is its own decision: list what it would change (themigrating-to-cache-componentsguide), 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 asprivate, 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.
- In the current model,
- 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:selectand a smalldepth(and the config'sdefaultDepthandmaxDepth),limitandpagination: falsewhere a count isn't needed,index: trueon fields used inwhereandsort,joinsandpopulateinstead of follow-up reads, one cached Payload instance (getPayload), andblockReferencesfor blocks shared across fields (smaller admin payloads). Apply what helps, each change with its test, in the module's owndata/orcms/layer. - The query check. Nothing native fails on query count. Use the adapter's Drizzle
loggerfrom 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 saysqueryis due, and always onmain. - SQLite settings that matter under load (the adapter's
walandbusyTimeout) 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.mddescribe 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.