Skip to content

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

ts
new SsrCache(): SsrCache;

Returns

SsrCache

Methods

get()

ts
get<T>(key): T | null;

Get a value from the cache

Type Parameters

T

T

Parameters

key

string

Returns

T | null


getOrInsertComputed()

ts
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()

ts
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()

ts
has(key): boolean;

Check if a key exists in the cache

Parameters

key

string

Returns

boolean


load()

ts
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()

ts
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()

ts
set<T>(key, val): T;

Set a value in the cache

Type Parameters

T

T

Parameters

key

string

val

T

Returns

T


takeOnce()

ts
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