pinia
Goal: pinia in a Vue app, with a value the server set already in the store when the browser hydrates, and no request to fetch it. The runnable sample is examples/data-vue/; pinia is a devDependency of that example, and Hamur has no module for it.
When Hamur's own state is enough: useState(key, init) is state shared by key that travels with the page, and useCookie keeps a value in a cookie (State). Reach for pinia when the app already has stores, or you want its actions, getters and devtools. The two work together; pinia's state is carried the way useState's is, through the page payload.
- Make the store as pinia documents it. The file is plain TypeScript, so a page, a plugin and a test import the same one:
import { defineStore } from "pinia"
export const useVisitor = defineStore("visitor", {
state: () => ({ servedAt: "", name: "guest" }),
actions: {
rename(name: string) {
this.name = name
},
},
})
- Install pinia and carry its state in an app plugin.
app:adapterruns before the first render on the server and in the browser, and gives Vue'sApp(App setup per adapter).useHydration(app, key, get, set)stores whatget()returns in the page payload after the render, and callsset(value)in the browser when the plugin runs, beforeapp:adapterand the first render (Handing a value from the server to the browser). Heregetreturns pinia's root state andsetputs it back:
import { definePlugin, useHydration } from "hamur/app"
import { createPinia } from "pinia"
import type { App } from "vue"
import { useVisitor } from "../../shared/visitor.ts"
export default definePlugin((app) => {
const pinia = createPinia()
useHydration(
app,
"pinia",
() => pinia.state.value,
(value) => {
pinia.state.value = value
},
)
app.hooks.hook("app:adapter", (instance) => {
;(instance as App).use(pinia)
if (app.event) useVisitor(pinia).servedAt = new Date().toISOString()
})
})
The server sets servedAt in the hook, after app.use(pinia); app.event is the request's event on the server and is never there in the browser. The plugin runs for each request, so each one makes its own createPinia(). Nothing is shared between requests.
- Use the store in a page:
<script setup lang="ts">
import { useVisitor } from "../../shared/visitor.ts"
const visitor = useVisitor()
</script>
<template>
<main>
<h1>pinia</h1>
<p>Served at {{ visitor.servedAt }}</p>
<p>Hello, {{ visitor.name }}</p>
<button @click="visitor.rename('Ada')">Rename</button>
</main>
</template>
In the browser the plugin restores the state first, so the store starts with the servedAt the server wrote. The state is plain data; it must be serializable, with the rules of Data. A store with a class instance in its state needs a transport entry.
Check it. Open
/piniaand view the page source:Served atshows the server's time. In the browser's network panel there is no request for it after hydration, and the time on screen does not change.Renamechangesnameonly in the browser; a reload starts from the server's state again.Checked on 2026-12-01 against
examples/data-vueat commit713acc0, built and previewed with headless Chrome 158 on macOS: the page source and the hydrated store showed the sameservedAt, the browser asked only for the three_hamur/*.jschunks after load and nothing for the data, andRenamechangedHello, guesttoHello, Adawith no request andservedAtkept.
See also: State, Plugins and hooks, Data, Hydration.