Skip to content

fix(core): cache Content Platform template list across page generations - #3542

Merged
lariciamota merged 2 commits into
mainfrom
fix/cp-template-list-cache-hotfix
Oct 23, 2026
Merged

lariciamota merged 2 commits into
mainfrom
fix/cp-template-list-cache-hotfix

Conversation

@lariciamota

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

Hotfix to main of #3526, which was merged into dev. The content is the same commit (b03ed332b), cherry-picked onto main without 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.

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), 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:

  • Key: (accountName, storeId, contentType, locale). 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: bounds staleness for long-lived processes (ISR with revalidate).
  • Preview and branch preview bypass the cache.
  • Failures are not cached: the entry is evicted and the error is re-thrown unchanged.

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

🤖 Generated with Claude Code

…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 lariciamota added bug Something isn't working performance Pull requests related to perfomance issues labels Oct 23, 2026
@lariciamota
lariciamota requested a review from a team as a code owner October 23, 2026 18:47
@lariciamota
lariciamota requested review from hellofanny and renatamottam and removed request for a team October 23, 2026 18:47
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pkg-pr-new

pkg-pr-new Bot commented Oct 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

@faststore/api

npm i https://pkg.pr.new/vtex/faststore/@faststore/api@16472aa

@faststore/cli

npm i https://pkg.pr.new/vtex/faststore/@faststore/cli@16472aa

@faststore/components

npm i https://pkg.pr.new/vtex/faststore/@faststore/components@16472aa

@faststore/core

npm i https://pkg.pr.new/vtex/faststore/@faststore/core@16472aa

@faststore/diagnostics

npm i https://pkg.pr.new/vtex/faststore/@faststore/diagnostics@16472aa

@faststore/lighthouse

npm i https://pkg.pr.new/vtex/faststore/@faststore/lighthouse@16472aa

@faststore/sdk

npm i https://pkg.pr.new/vtex/faststore/@faststore/sdk@16472aa

@faststore/ui

npm i https://pkg.pr.new/vtex/faststore/@faststore/ui@16472aa

commit: 16472aa

@sonar-workflows

Copy link
Copy Markdown

Failed Quality Gate failed

  • 1 New Issues (is greater than 0)

Project ID: vtex_faststore_f0a862d5-9557-49f9-8d09-de40caa76622

View in SonarQube

@dk-pr-review

dk-pr-review Bot commented Oct 23, 2026

Copy link
Copy Markdown

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

@lariciamota
lariciamota merged commit ab44199 into main Oct 23, 2026
15 of 17 checks passed
@eduardoformiga eduardoformiga mentioned this pull request Oct 29, 2026
2 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working performance Pull requests related to perfomance issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants