Skip to main content

Endpoint Expiry Policy

By default, Reactive Data Client uses stale-while-revalidate. When data is available, components render it immediately. If the data is old enough, Data Client refetches it in the background.

Expiry status​

within dataExpiryLengthHow long (in milliseconds) a response stays fresh before it becomes stale. Learn more
useSuspense()GET

Fresh​

The data is new enough. Data Client does not fetch it.

When on, data skips Stale and becomes invalid as soon as it expires, so it must refetch before it can render. Learn more
after dataExpiryLengthOnce dataExpiryLength (in milliseconds) has passed, the cached response is stale: still shown, but refetched. Learn more
useSuspense()GET

Stale​

Components can show the data. Data Client fetches it again when a component needs it.

after ctrl.invalidate()Marks the data invalid right away, even while fresh, so components that need it fetch again. Learn more
useSuspense()GET

Invalid​

Components cannot show the data. A component that needs it fetches it (mounted components keep their data until it resolves). If no component needs the data, nothing happens.

useSuspense() checks the data on mount and when its parameters change. It fetches if the data is stale.

Expiry Time​

Endpoint.dataExpiryLength​

Endpoint.dataExpiryLength sets how many milliseconds data stays 'fresh' before it becomes 'stale'. Set it to a low number like 50 to make data stale almost immediately, or to a large number to keep it fresh for a long time.

Toggling between 'first' and 'second' changes the parameters. While the data is fresh, the old time stays without a refetch.

import { lastUpdated } from './api/lastUpdated';

export const getUpdated = lastUpdated.extend({ dataExpiryLength: 10000 });
@data-client/rest

Long cache lifetime

LongLivingResource.ts
import {
RestEndpoint,
RestGenerics,
resource,
} from '@data-client/rest';

// We can now use LongLivingEndpoint to create endpoints that will be cached for one hour
class LongLivingEndpoint<
O extends RestGenerics,
> extends RestEndpoint<O> {
dataExpiryLength = 60 * 60 * 1000; // one hour
}

const LongLivingResource = resource({
path: '/:id',
Endpoint: LongLivingEndpoint,
});

Never retry on error

NoRetryResource.ts
import {
RestEndpoint,
RestGenerics,
resource,
} from '@data-client/rest';

// We can now use NoRetryEndpoint to create endpoints that will be cached for one hour
class NoRetryEndpoint<
O extends RestGenerics,
> extends RestEndpoint<O> {
errorExpiryLength = Infinity;
}

const NoRetryResource = resource({
path: '/:id',
Endpoint: NoRetryEndpoint,
});

Endpoint.invalidIfStale​

Endpoint.invalidIfStale eliminates the 'stale' status, making data that expires immediately be considered 'invalid'.

This is demonstrated by the page suspending once its data goes stale. While the data is fresh, the page shows it.

import { lastUpdated } from './api/lastUpdated';

export const getUpdated = lastUpdated.extend({
  invalidIfStale: true,
  dataExpiryLength: 5000,
});

Force refresh​

To fetch new data while you show the old data:

A specific endpoint​

Controller.fetch fetches while components show the previous data. It fetches even when the data is 'fresh'.

<script setup lang="ts">
  import { useController, useSuspense } from '@data-client/vue';
  import { lastUpdated } from './api/lastUpdated';

  const time = await useSuspense(lastUpdated, { id: '1' });
  const ctrl = useController();
</script>

<template>
  <div class="listItem">
    <span class="meta">API time</span>
    <time>{{ time.updatedAt.toLocaleString('en-US', { timeStyle: 'long' }) }}</time>
  </div>
  <button @click="ctrl.fetch(lastUpdated, { id: '1' })">Refresh</button>
</template>

Refresh visible endpoints​

Controller.expireAll() sets all responses' expiry status matching testKey to Stale.

<script setup lang="ts">
  import { useController } from '@data-client/vue';
  import { lastUpdated } from './api/lastUpdated';
  import TimeList from './TimeList.vue';

  const ctrl = useController();
</script>

<template>
  <div>
    <TimeList />
    <div class="buttonGroup">
      <button @click="ctrl.expireAll(lastUpdated)">Expire All</button>
      <button @click="ctrl.fetch(lastUpdated, { id: '1' })">
        Force Refresh First
      </button>
    </div>
  </div>
</template>

Invalidate​

Both endpoints and entities can be invalidated.

Invalidated data always refetches, even when it is fresh. Vue can't suspend a component again once its setup has run, so mounted components keep showing their previous data until the refetch resolves. Meanwhile useCache() returns undefined and useDLE()'s loading is true. Components mounted after invalidation suspend until the new data arrives.

A specific endpoint​

In this example invalidating the endpoint refetches it, even though its data is still fresh.

<script setup lang="ts">
  import { useController } from '@data-client/vue';
  import { lastUpdated } from './api/lastUpdated';
  import TimeList from './TimeList.vue';

  const ctrl = useController();
</script>

<template>
  <div>
    <TimeList />
    <div class="buttonGroup">
      <button @click="ctrl.invalidateAll(lastUpdated)">
        Invalidate All
      </button>
      <button @click="ctrl.invalidate(lastUpdated, { id: '1' })">
        Invalidate First
      </button>
    </div>
  </div>
</template>

Any endpoint with an entity​

The Invalidate schema invalidates every endpoint whose response includes that entity. If the endpoint has the entity in an Array, Data Client removes it from that Array.

<script setup lang="ts">
  import { useController, useLoading } from '@data-client/vue';
  import {
    InvalidateTimedEntity,
    deleteLastUpdated,
  } from './api/lastUpdated';
  import ServerTime from './ServerTime.vue';

  const ctrl = useController();
  const [handleDelete, loadingDelete] = useLoading(() =>
    ctrl.fetch(deleteLastUpdated, { id: '1' }),
  );
</script>

<template>
  <div>
    <ServerTime id="1" />
    <div class="buttonGroup">
      <button @click="handleDelete" :aria-busy="loadingDelete">
        Invalidate
      </button>
      <button
        @click="ctrl.setResponse(deleteLastUpdated, { id: '1' }, { id: '1' })"
      >
        Invalidate (without fetching DELETE)
      </button>
      <button @click="ctrl.set([InvalidateTimedEntity], [{ id: '1' }])">
        Invalidate Entity with ctrl.set
      </button>
    </div>
  </div>
</template>

Controller.fetch() updates the server and the store. To change only the store, use Controller.setResponse() or Controller.set().

Conditional Invalidation based on data​

To invalidate only for some responses, return undefined from Entity.process.

class PriceLevel extends Entity {
price = 0;
amount = 0;

pk() {
return this.price;
}

static process(
input: [number, number],
parent: any,
key: string | undefined,
): any {
const [price, amount] = input;
if (amount === 0) return undefined;
return { price, amount };
}
}