Repository navigation
fix(core): cache Content Platform template list across page generations - #3542
Merged
Merged
Conversation
…ns (#3526) ## What's the purpose of this pull request? On stores using the Content Platform (CP), every cold PLP/PDP generation scans **all** entries of the content type to pick a template, and nothing is reused between page generations. `getPlpContent` first tries `plp/entries/slug/:slug`. With the default `plp` schema this always 404s, because it declares `identifierKeys: ["slug"]` but has no `slug` property. It then calls `getMultipleContent`, which: 1. paginates `listEntries` (20 per page); 2. fetches the full content of every entry, because `settings.template.value` is not in the listing. Each cold page therefore costs `ceil(N / 20) + N` CP calls. Since `listEntries` started paginating (#3384, `v3.100.2` / v4 `61fcbdfde`), this grows with the number of templates. On a store with 333 PLP entries, that is about 350 CP calls per collection. The calls arrive in bursts right after every build, which matched the 502/504 bursts seen in production. Unknown slugs pay the same scan, because the content lookup runs in parallel with the collection query. ## How it works? `ContentService.getMultipleContent` (CP branch) now memoizes the result of "list entries + fetch each entry" per process: - **Key:** `(accountName, storeId, contentType, locale)`. These are the only fields `listEntries` uses. The page slug stays out of the key, so every collection shares one list. - **In-flight deduplication:** concurrent page generations wait on the same promise. - **TTL of 5 minutes:** a CMS publish already triggers a new build. The TTL only bounds staleness for long-lived processes (ISR with `revalidate`). - **Preview and branch preview bypass the cache:** they neither read nor write it. - **Failures are not cached:** the entry is evicted (only if it is still the failed promise) and the error is re-thrown unchanged. The existing scroll loop and `fillEntriesWithData` moved unchanged into a private `listAndFillEntries`. Template selection (`findBestPLPTemplate` / `findBestPDPTemplate`) and the hCMS path are untouched. Nothing public changes: no signatures, exports or config. The shared objects are safe to reuse. Downstream code only reads the chosen template: `[...slug].tsx` reads `settings.productGallery`, and `[slug]/p.tsx` spreads it into props. ## How to test it? - `cd packages/core && pnpm vitest run --project node test/server/content/service.test.ts` - The new suite fakes `ClientCP` and covers: - concurrent PLPs with different slugs share one list (with correct templates); - reuse within the TTL; - the direct slug match still wins; - scroll pagination; - separate keys per content type and per locale; - preview bypass; - failures not cached; - a late failure not evicting a newer fetch; - TTL expiry (fake timers). - **Local reproduction** against a real CP store (333 PLP entries, 20 cold collections fired concurrently): | | CP calls | p50 per page | static request max during burst | |---|---|---|---| | before | 7,140 | 11.9s | 1.6s | | with this cache | 509 | 8.4–9.6s | 39–158 ms | The local run patched the same memoization logic into a store's `node_modules`. The production code here is equivalent, with the cache held on the `contentService` singleton instead of at module level. `test/pages/api/preview.test.ts` already fails on `dev` (2 tests); this PR doesn't touch it. ### Starters Deploy Preview N/A (server-side content fetching only) ## References - Internal follow-up: [SO-698](https://vtex-dev.atlassian.net/browse/SO-698) - Pagination change that made the cost scale with N: #3384 - Possible follow-ups, out of scope here: - backport to `3.x`, which has the same code since `v3.100.2`; - a configurable TTL or kill switch; - CP data-plane search by `template.value`. ## Checklist - [x] PR title and commit messages follow the Conventional Commits specification - [x] PR description - [ ] Label: `bug` / `performance` [SO-698]: https://vtex-dev.atlassian.net/browse/SO-698?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Content listings are reused for up to five minutes, helping repeat visits and simultaneous requests load faster. - Preview requests continue to fetch fresh content, and failed requests can be retried. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Cursor <[email protected]> (cherry picked from commit b03ed33)
lariciamota
requested review from
hellofanny and
renatamottam
and removed request for
a team
October 23, 2026 18:47
lemagnetic
approved these changes
Oct 23, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@faststore/api
@faststore/cli
@faststore/components
@faststore/core
@faststore/diagnostics
@faststore/lighthouse
@faststore/sdk
@faststore/ui
commit: |
DK Review — Deferred (Sonar gate)⏭️ LLM review not run: this PR's Sonar quality gate is currently failing. DK Review holds the AI review until Sonar passes, so review budget is not spent on a PR that already needs changes. Fix the issues Sonar reported and push again — the full review runs automatically once Sonar is green. This PR is not blocked by DK Review; a human engineer can still approve and merge manually. DK Review v1.0.0 |
Merged
2 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What's the purpose of this pull request?
Hotfix to
mainof #3526, which was merged intodev. The content is the same commit (b03ed332b), cherry-picked ontomainwithout changes.On stores using the Content Platform (CP), every cold PLP/PDP generation scans all entries of the content type to pick a template, and nothing is reused between page generations.
getPlpContentfirst triesplp/entries/slug/:slug. With the defaultplpschema this always 404s, because it declaresidentifierKeys: ["slug"]but has noslugproperty. It then callsgetMultipleContent, which:listEntries(20 per page);settings.template.valueis not in the listing.Each cold page therefore costs
ceil(N / 20) + NCP calls. SincelistEntriesstarted paginating (#3384), this grows with the number of templates. On a store with 333 PLP entries, that is about 350 CP calls per collection. The calls arrive in bursts right after every build, which matched the 502/504 bursts seen in production.How it works?
ContentService.getMultipleContent(CP branch) now memoizes the result of "list entries + fetch each entry" per process:(accountName, storeId, contentType, locale). The page slug stays out of the key, so every collection shares one list.revalidate).Template selection and the hCMS path are untouched. Nothing public changes.
See #3526 for the full description and benchmark.
How to test it?
cd packages/core && pnpm vitest run --project node test/server/content/service.test.ts(15 tests passing on this branch).Starters Deploy Preview
N/A (server-side content fetching only)
References
dev): fix(core): cache Content Platform template list across page generations #3526🤖 Generated with Claude Code