# Device

Show the last server answer at once, from the device, for the signed-in account. Then ask the server again.

```ts [src/main.ts]
import { createApp } from 'vue'
import { openDevice } from 'vite-hub/device'
import App from './App.vue'

// Read the stored answers before the first render
await openDevice({ version: '1' })
createApp(App).mount('#app')
```

```vue [src/components/ProjectList.vue]
<script setup lang="ts">
import { useStoredFetch } from 'vite-hub/device/vue'

// The last answer at once, then the new one
const { data: projects } = useStoredFetch<{ id: string, name: string }[]>('/api/projects', { default: () => [] })
</script>

<template>
  <li v-for="project in projects" :key="project.id">{{ project.name }}</li>
</template>
```

## What you get

| Part | What it does |
| --- | --- |
| Stored reads | `useStoredData()` shows the stored answer in the first render, then calls your loader. It has the shape of Nuxt `useAsyncData()`. `useStoredFetch()` does the same for a GET of JSON, and never stores an error answer. |
| Nuxt | The ViteHub module opens the device, auto-imports the reads, and with ViteHub Auth, names the account and waits for the copies to go on sign-out. |
| One account | The device holds the copies of the account that your session read names with its `account` option, or that you name with `setDeviceAccount()`. Another account, or a sign-out, deletes them in every tab. |
| No account, no disk | The device stores only the answers of loader calls that start after the page names an account, and the answer of the session read that names it. An app that names no account stores nothing. |
| Caps | A copy older than `maxAge` (7 days by default) does not show. The device keeps at most 5 MB, and deletes the oldest copies first. |
| Versions | `openDevice({ version })` deletes the copies of another version, so a new build does not show data of an old shape. |
| Fallback | In a private window or a sandboxed frame without IndexedDB, the copies stay in memory. |
| Inspection | `inspectDevice()` gives the account, the storage, and each copy with its key, time, and size. |

A stored copy only changes what the screen shows first. The server still checks every request.

## Next

- [Get started](https://vitehub.dev/docs/device/get-started): a project list that shows at once on a reload.
- [Browser API](https://vitehub.dev/docs/device/browser-api): every function and option.
- [Limits and errors](https://vitehub.dev/docs/device/limits-and-errors): what the device does not do, and the `DEVICE_*` codes.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/device.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/device/get-started.md)
- [Browser API](https://vitehub.dev/raw/docs/device/browser-api.md)
- [Limits and errors](https://vitehub.dev/raw/docs/device/limits-and-errors.md)

Next: [DEVICE_ALREADY_OPEN](https://vitehub.dev/raw/docs/device/diagnostics/device_already_open.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
