[data-reveal]{opacity:1!important;transform:none!important}
AntelopeJS DMS
DocumentationData loading

Data loading

useDmsAsyncData, useDmsFetch, useDmsPagedData and useDmsState, how a server render waits for and transfers their data, and how the browser revalidates it.

useDmsAsyncData loads the data a component reads from a route. The server waits for the result and passes it to the browser, so hydration does not repeat the request. Components that use the same key share the result.

In this example, a Vue component's <script setup> reads an existing /api/orders route. useDmsAsyncData and $fetch are auto-imports of the renderer, so a module's source files use them without an import. The declare lines only give this example the types that the renderer exports:

components/Orders.vue (script setup) ts
declare const useDmsAsyncData: typeof import("@antelopejs/dms-frontend/templates/vue/frontend-module").useDmsAsyncData;
declare const $fetch: typeof import("@antelopejs/dms-frontend/templates/vue/frontend-module").$fetch;

const { data, error, status, refresh } = useDmsAsyncData(
  "orders",
  ({ signal }) => $fetch("/api/orders", { signal }),
);

data.value holds the route's response, and error.value holds a failure. Calling refresh() after an action that changes the orders reads them again. The composable does not create the endpoint, which must already exist. The key must include every filter that tells one result apart from another.

useDmsFetch(url, options) is useDmsAsyncData around a request, and useDmsPagedData reads a list page by page. In the DMS, useApiData and useApiPagedData wrap them. See the frontend composables of the DMS guide.

useDmsAsyncData follows the semantics of Nuxt's useAsyncData. A reader who knows that composable should note three behaviours of this one:

  • Calls with one key join the run in flight instead of starting another.
  • A failed run keeps the data of the last success next to the failure.
  • The first lazy run starts once the caller's turn is over, so an explicit refresh in that turn replaces it.

The first request needs no configuration of the server rendering, caching and revalidation that the sections below describe.

On the server

  • A server render waits for the data. The renderer awaits a component's data before the component renders, whether or not its setup awaits the call. The browser takes the result over while it hydrates, instead of fetching it again. Keys that a setup does not await load in parallel. The page renders after the data that a plugin starts.
  • A page renders once its middleware is done. The page that calls defineDmsPageMeta waits for the middleware it starts. The HTML therefore shows what a middleware loads or sets with transfer: true, and the browser hydrates with it. A middleware's redirect still answers the request. A middleware that throws fails the page. The error page shows in its place, and a server render answers with the error's status code. Middlewares run in order. Adjacent middlewares registered with { parallel: true } start together, and their results apply in registration order.
  • A page that completes its query renders at that query. When a page asks for a navigation while the server renders it (router.replace, navigateDms, a middleware), the render starts no more data reads, and the request answers with a redirect. A navigation that changes only the query of the same path renders in place instead. One case is a page that adds the ?workspace= it remembers. The server renders the page again at that query in the same request, and reuses the reads that the first render completed. It answers with 200 and a Content-Location header that names the URL. The browser shows that URL in the address bar without a new history entry. A page that keeps changing its query gets a redirect after two such renders.
  • The browser hydrates with what the HTML shows. The server leaves to the browser any data that settles after the HTML is complete. A value that a setup derives from data it did not await, in an immediate watcher for example, then keeps the value that the server rendered.
  • The payload carries a failure too, so hydration does not run the handler again. An HTTP failure keeps its statusCode, statusMessage and message, and a 4xx failure also keeps its data. The payload leaves out the body of a 5xx failure. Any other failure becomes a 500, and only the server log shows its cause. On a server, the message of a fetch error names the backend's address, so the status text replaces that message.
  • A new key is read where the first one would have been. On a server, server: false and immediate: false read nothing. A key that the setup sets gets its own read, and the component's render waits for that read and transfers it. If a key changes after that wait has started, the server does not read the new key. The HTML shows the default, and the browser reads the key once the component is mounted. To render a read that depends on the answer of another read, await the first read in the setup.
  • A call without a key cannot be transferred. This covers useDmsAsyncData(handler) and a useDmsFetch call whose body it cannot key, such as a FormData. The server leaves the call to the browser, as server: false does. A development render warns about it. It also warns about a backend read made outside any handler on the server, such as a setup's own await $fetch, which the browser then makes again.
  • A middleware or a plugin can load with useDmsAsyncData too. The page waits for the read and carries the answer, with no warning. A read made outside a handler and handed over with useDmsState(key, init, { transfer: true }) also reaches the browser. The development warning cannot tell that case apart from a read that is not handed over, so the warning names it.

The payload

One <script id="dms-payload" type="application/json"> element carries the data, the failures and the transferred state. It uses the format of devalue, which keeps a Set, a Map or a Date, and is escaped like Inertia's page object. The server leaves out a value that devalue cannot write, such as a class instance, and logs it.

useDmsState(key, init, { transfer: true }) hands the server's value to the browser, which then starts from that value instead of calling init. The server transfers only the state whose key opts in. Transferred state must never hold a token or a secret, because the value is written into the page.

In the browser

  • Stale while revalidate. Data loaded before shows at once. The browser reads it again in the background each time a component that shows it mounts, and each time a reactive key returns to it. Awaiting the call does not wait for that read. The first mount after a server render does not read again. Of the data that no component shows any more, the browser keeps the 50 most recently shown across navigations, so a return to a page shows its data at once.
  • error stays until the next answer. A run in flight keeps the last failure. A success clears it, and a failure replaces it. After a failed refresh, the data of the last success stays on screen.
  • refreshInterval: ms reads again on an interval while a component shows the data and the page is visible. The interval may be a ref or a getter. The polling follows it, and 0, null or undefined stops it.
  • A call outside a component belongs to the application. This covers a plugin, past its first await too, and a callback. Its watchers and its polling run for as long as the application does.

Options and results

Option or memberBehaviour
await or notThe result's refs are there at once. Awaiting the result waits for the data
lazy: trueA client navigation does not wait. A server render still waits
server: falseThe server renders the default. The browser fetches once the page is mounted
immediate: falseThe first run waits for execute(), for a dialog or a drawer
dedupe"cancel" (the default) aborts the run in flight when a newer one replaces it. "defer" waits for the run in flight
The handler's { signal }Aborted when a newer run replaces this one, when clear() resets the data, or when the last component that shows the key unmounts
deepdata is a shallow ref by default. Replace the value, or pass deep: true to track changes inside it
transform(value, { key })Receives the key. useDmsFetch's transform(value, { key, url }) also receives the URL that the response answers
key, placeholderkey is the key that the data is loaded under. placeholder is true while a reactive key shows the previous key's data
refreshOnPageRefresh: falseLeaves the call out of refreshDmsPageData()
A null or undefined keyNothing to load. The caller holds its default and runs nothing until the key has a value

Requests

useDmsFetch(url, options) keys a request on its method, base URL, URL, query and body, and not on its headers. The callers of one request share it, and a server render transfers it. The URL, method, query, params and body may each be a ref or a getter, read again for each request, as in Nuxt's useFetch. A null or undefined URL requests nothing until it has a value. The key option names a key in place of the request's own, and may be a ref or a getter too. The $fetch option replaces the fetch function, which receives the signal.

dmsRequestKey(url, { method, baseURL, query, params, body }) returns the key that useDmsFetch gives a request, without the scope. Pass it to refreshDmsData(keys), or use it in a test that looks for a response's entry.

Lists, page by page

useDmsPagedData(key, handler, { nextCursor, initialCursor }) reads a list page by page under one key, for "load more" buttons and infinite lists. The handler receives { cursor, index, signal } and returns a page. nextCursor(page, pages) returns the next page's cursor, or null at the end.

The first page loads like any data. A server render waits for it and transfers it. loadMore() appends the next page, and pages, hasMore and loadingMore follow it. A failure lands in error, and the loaded pages stay. A revalidation reads every page loaded so far again, and replaces them once all are in. Put the list's filters in the key, because a new key starts again from its first page.

Updating and refreshing

  • useDmsData(key) returns the data that another caller loads under key, like Nuxt's useNuxtData. Assigning it updates every component that shows the key, without reading it again. Changing it in place does the same when the key is deep: true. Keys are shallow by default, so replace the value unless the reader opts into deep reactivity. Use it to apply a write's answer, an optimistic update or a realtime event. It loads nothing and keeps nothing alive. A write to a key that nobody loaded is ignored. A run in flight still writes its answer over an update, so a writer refreshes the key once the write is done.
  • refreshDmsPageData() reads again the data that the components on screen show, after a save or on a realtime event. It leaves alone the data that waits for execute().
  • refreshDmsData(keys?) reads the given keys again.
  • setDmsAsyncDataScope(scope) puts the keys in a scope, such as a digest of the account and the workspace. The same key in another scope is another entry, and moving to another scope drops the data of every key. The data a component on screen shows is read again in the new scope, and the data it waits to execute() stays unread. Set the scope on both sides before the page renders, with the same value. Set it from a plugin registered with { enforce: "pre" }, so that it runs before another module's plugin reads data.