crelte / ssr / SsrCache
Class: SsrCache
A simple cache for server side rendering
You can use this to store data to pass to the client or to cache data generally. Storing data and retrieving it will also work on the client.
Constructors
Constructor
new SsrCache(): SsrCache;Returns
SsrCache
Methods
get()
get<T>(key): T | null;Get a value from the cache
Type Parameters
T
T
Parameters
key
string
Returns
T | null
getOrInsertComputed()
getOrInsertComputed<T>(key, fn): T;check if the value is in the cache else calls the fn
See also getOrInsertLoaded
Type Parameters
T
T
Parameters
key
string
fn
() => T
Returns
T
getOrInsertLoaded()
getOrInsertLoaded<T>(key, fn): Promise<T>;check if the value is in the cache else calls the fn
See also getOrInsertComputed
Type Parameters
T
T
Parameters
key
string
fn
() => Promise<T>
Returns
Promise<T>
has()
has(key): boolean;Check if a key exists in the cache
Parameters
key
string
Returns
boolean
load()
load<T>(key, fn): Promise<T>;check if the value is in the cache else calls the fn
See also getOrInsertComputed
Type Parameters
T
T
Parameters
key
string
fn
() => Promise<T>
Returns
Promise<T>
Deprecated
use getOrInsertLoaded instead
remove()
remove<T>(key): T | null;Remove a value from the cache and return it
Type Parameters
T
T
Parameters
key
string
Returns
T | null
set()
set<T>(key, val): T;Set a value in the cache
Type Parameters
T
T
Parameters
key
string
val
T
Returns
T
takeOnce()
takeOnce<T>(key, fn): T;One-shot SSR handoff value.
Intended use: call this once per request (per key) during SSR to generate a value that must match between server render and client hydration.
On the server, the value is generated once per key and stored for hydration. Subsequent calls with the same key during SSR currently return the same value, but this behaviour is an implementation detail and may change in the future. Consumers should rely on calling this at most once per key during SSR.
On the client, the stored value is returned exactly once and removed. Subsequent calls return a fresh value and are not cached.
Warning: this function is designed to be called once per key during SSR. Calling it multiple times may lead to unexpected behaviour if the server-side implementation changes.
See also getOrInsertComputed
Type Parameters
T
T
Parameters
key
string
fn
() => T
Returns
T