Skip to content

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.

  1. Make the store as pinia documents it. The file is plain TypeScript, so a page, a plugin and a test import the same one:
ts
import { defineStore } from "pinia"

export const useVisitor = defineStore("visitor", {
  state: () => ({ servedAt: "", name: "guest" }),
  actions: {
    rename(name: string) {
      this.name = name
    },
  },
})
  1. Install pinia and carry its state in an app plugin. app:adapter runs before the first render on the server and in the browser, and gives Vue's App (App setup per adapter). useHydration(app, key, get, set) stores what get() returns in the page payload after the render, and calls set(value) in the browser when the plugin runs, before app:adapter and the first render (Handing a value from the server to the browser). Here get returns pinia's root state and set puts it back:
ts
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.

  1. Use the store in a page:
vue
<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.

  1. Check it. Open /pinia and view the page source: Served at shows 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. Rename changes name only in the browser; a reload starts from the server's state again.

    Checked on 2026-12-01 against examples/data-vue at commit 713acc0, built and previewed with headless Chrome 158 on macOS: the page source and the hydrated store showed the same servedAt, the browser asked only for the three _hamur/*.js chunks after load and nothing for the data, and Rename changed Hello, guest to Hello, Ada with no request and servedAt kept.

See also: State, Plugins and hooks, Data, Hydration.