AbsoluteJS

Server Data

useServerData loads data while a Vue page renders on the server and sends it to the browser with the page. Hydration reads that value instead of fetching again.

#useServerData

Call useServerData(key, loader) in <script setup> or setup(). Import it from @absolutejs/absolute/vue. The loader receives an AbortSignal and returns a promise. On the server it runs in Vue's onServerPrefetch, so the page renders with the data in place.

VUE
<!-- frontend/vue/pages/Profile.vue -->
<script setup lang="ts">
import { useServerData } from '@absolutejs/absolute/vue';

type Profile = { id: number; name: string };

const loadProfile = async (signal: AbortSignal): Promise<Profile> => {
  const response = await fetch('https://api.example.com/me', { signal });

  return response.json();
};

const { data, error, pending, refresh } = useServerData('profile', loadProfile);
</script>

<template>
  <p v-if="pending">Loading…</p>
  <p v-else-if="error">Could not load your profile: {{ error.message }}</p>
  <h1 v-else-if="data">{{ data.name }}</h1>
  <button type="button" @click="refresh">Reload</button>
</template>

The route handler doesn't change. Register the page as usual.

TS
// backend/server.ts
import { asset } from '@absolutejs/absolute';
import { handleVuePageRequest } from '@absolutejs/absolute/vue';
import type Profile from '../frontend/vue/pages/Profile.vue';

new Elysia().get('/profile', () =>
  handleVuePageRequest<typeof Profile>({
    indexPath: asset(manifest, 'ProfileIndex'),
    pagePath: asset(manifest, 'Profile'),
    props: {}
  })
);

#Return Values

NameTypeMeaning
dataRef<T | null>The loaded value. T is the loader’s resolved type.
errorRef<Error | null>The last failure, on the server or in the browser.
pendingRef<boolean>True while a load is in flight.
refresh() => Promise<void>Loads again in the browser, aborting a load in flight.

In the browser the signal aborts when the component unmounts or refresh() starts a new load. Pass it to fetch so stale requests are cancelled.

#Hydration

After rendering, the page handler writes every value it loaded into one inline script before the page's module script. A page that loaded no server data gets no script.

HTML
<script>window.__ABSOLUTE_SERVER_DATA__={"profile":{"data":{"id":1,"name":"Ada"}}}</script>

The JSON is escaped so a value can't close the script element. When the page hydrates, useServerData reads its key from the payload and doesn't call the loader. Each entry is used once, so later mounts of the component load in the browser.

SituationServerBrowser
First load of a hydrated pageRuns onceReads the page, no fetch
Client-side route change to the componentNoRuns
The same component mounting againNoRuns
client: 'none'Runs onceNo client code is sent
server: falseSkipped, renders pendingRuns
Page that exports routes (SPA shell)SkippedRuns
collectStreamingSlots: trueRuns onceReads the page, no fetch

Pass { server: false } to always load in the browser. The server renders the pending state.

TS
// The server renders the pending state; the browser loads the data.
const { data, pending } = useServerData('recommendations', loadRecommendations, {
  server: false
});

#Keys and Values

A key must be unique among the useServerData calls of one request. Outside production, two calls with one key fail the page with an error that names the key. In production the second call logs a warning and loads in the browser. When a component appears more than once, put its identity in the key.

VUE
<script setup lang="ts">
import { useServerData } from '@absolutejs/absolute/vue';

const props = defineProps<{ id: number }>();

// One key per request: put the component's identity in it when the
// component appears more than once on a page.
const { data } = useServerData(`post:${props.id}`, (signal) =>
  loadPost(props.id, signal)
);
</script>

Results travel as JSON. Dates arrive as strings, undefined becomes null, and a value JSON can't encode, such as a cycle or a BigInt, becomes that key's error.

#Errors

A loader that throws on the server doesn't fail the page. The component renders with error set, and the browser hydrates with an Error of the same name. Outside production the message is the original one. In production it is replaced with Loading server data failed. so server details don't reach the page. The original error is logged on the server.

#client: 'none'

A page registered with client: 'none' is rendered on the server only. It ships no script: no props script, no module script and no server data payload. indexPath is optional, and the build skips that page's client bundle. useServerData still runs on the server, so the HTML includes the data.

TS
// backend/server.ts
import type Pricing from '../frontend/vue/pages/Pricing.vue';

new Elysia().get('/pricing', () =>
  handleVuePageRequest<typeof Pricing>({
    client: 'none',
    pagePath: asset(manifest, 'Pricing'),
    props: { plans }
  })
);

The build finds the client value in the handler source. A string literal or a const in the same file works. A value it can't know at build time keeps the client bundle; the page still renders without scripts.