Skip to content

tanstack: logged-in requests also disable the data caches (loader/fetch/section), not only the HTML cache #715

Open

Description

@aline-pereira

Summary

In @decocms/tanstack 7.80.0, a request that the site's buildSegment marks as loggedIn turns off the response cache and also every data cache: memory and shared storage, for loaders, fetch and section caches. So every SSR and every ?renderJson for a logged-in shopper reruns all Intelligent Search, catalog and CMS calls from scratch.

Fresh (deco runtime) caches public loaders the same way whether or not the shopper is logged in. Whether a loader is cached is decided by its own cacheKey, and the segment token is part of the key.

Where

packages/tanstack/src/sdk/workerEntry.ts (published src/sdk/workerEntry.ts:2333-2372 in 7.80.0):

const privateRequest =
  requestCarriesDraft(request, url) ||
  segment?.loggedIn === true ||          // <- logged-in shopper
  request.headers.has("authorization") || ...;
storage = privateRequest || isDevMode() ? null : options.cacheStorage?.(env, request);
...
variant = [..., privateRequest ? crypto.randomUUID() : null];   // unique scope
bindCacheStorage({ storage, disabled: privateRequest || isDevMode(), ... });

With disabled: true, createCacheStore get/read/set return early. That covers createCachedLoader, createFetchCache and the section/layout caches, not only the HTML cache.

Fresh reference

The deco runtime (blocks/loader.ts at oficina-dev/deco-runtime@c30a0f6, lines 229-238) bypasses a loader's cache only for no-store/no-cache or when cacheKey returns null. Being logged in does not enter that decision.

For example, VTEX intelligentSearch/productListingPage.ts (deco-apps@60695e7, cacheKey at line 458) keys by getSegmentFromBag(ctx).token. A logged-in shopper whose segment equals the anonymous one shares those entries; a member whose priceTables differ gets their own.

Impact (oficina-tanstack)

  • PLP SSR while logged in: 6.3–9.1 s, against ~0.1–1.9 s for the same build when anonymous (cache MISS) and 0.6–1.7 s on the Fresh production site, logged in or not.
  • The PLP's in-place pill navigation fetches ?renderJson with a 5 s timeout, the same as Fresh. Above that it falls back to a full page load. For logged-in shoppers that fallback is the common case.

Proposal

Split "don't cache the response" from "don't use the data caches":

  1. Keep the HTML/response cache bypass for every private request, as today.
  2. For segment.loggedIn (and only that case: not drafts, previews, authorization or matcher overrides), keep the data caches on, with the scope built from the segment hash. The hash already distinguishes priceTables, campaigns, sales channel and region, so a member never reads an anonymous price.
  3. Loaders that read the session (user, orders, wishlist, orderForm) must stay outside createCachedLoader. That is the existing contract, the same one Fresh's cacheKey → null expresses. This could be opt-in at first, e.g. dataCacheForLoggedIn: true in createDecoWorkerEntry options.

Found while investigating QA-049 / QA-165 on deco-sites/oficina-tanstack (#491, #501).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions