Repository navigation
feat(sdk): serialize/hydrate LruMemoCache for SSR - #1007
Merged
Jaydbrown merged 2 commits intoOct 20, 2026
Merged
Conversation
The memo cache loses everything when a Next.js server component finishes, so the next request repeats the same contract queries. serialize() writes a versioned snapshot (expired entries dropped, LRU order preserved) and hydrate() restores it with the original absolute deadlines instead of restarting every TTL. Malformed input throws instead of leaving a half-filled cache, and metrics stay untouched so measuredSpeedupPercent is not fabricated.
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.
Closes #977.
Problem
LruMemoCachelives in memory. In a Next.js server component the instance dieswith the request, so every request repeats the same contract-metadata queries
that the cache was supposed to absorb.
What this adds
serialize()writes a versioned snapshot;hydrate()restores it.Also exported as free functions —
serializeCache(cache)/hydrateCache(cache, json)— for callers holding the cache through aninterface.
Details that matter
expiresAtinstead of restarting every TTL, so a snapshot taken at T+9s of a 10s entry
expires 1s later, not 10s later.
coldest entry.
maxSize: 10cannotoverflow a cache built with
maxSize: 3.SyntaxError/TypeError, including a versionmismatch) rather than leaving a half-filled cache.
and
measuredSpeedupPercent: null, because it has measured nothing.Tests
src/tests/lru-memo-cache-serialize.test.ts— 10 cases: round-trip, deadlinepreservation with fake timers, expired-entry filtering, LRU order after
hydration, capacity clamping, metrics untouched, malformed input rejected,
non-primitive keys rejected, and the exported helpers.
Rebased on
mainafter #1004.npx tsc --noEmitand this PR's test suite are green on the current head.