All skills
onmax avatar

/pinia

@de09c7b official
by Maxonmax/nuxt-skills715 stars
38

Pinia official Vue state management library, type-safe and extensible. Use when defining stores, working with state/getters/actions, or implementing store patterns in Vue apps.

Use this Skill: https://skilld.dev/gh/onmax/nuxt-skills/pinia

This session only. Nothing lands on disk.

referencesadvanced-ssr.md

≈607 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Server Side Rendering (SSR)

Pinia works with SSR when stores are called at the top of setup, getters, or actions.

Using Nuxt? See the Nuxt integration instead.

Basic Usage

<script setup>
// ✅ Works - pinia knows the app context in setup
const main = useMainStore()
</script>

Using Store Outside setup()

Pass the pinia instance explicitly:

const pinia = createPinia()
const app = createApp(App)
app.use(router)
app.use(pinia)

router.beforeEach((to) => {
  // ✅ Pass pinia for correct SSR context
  const main = useMainStore(pinia)

  if (to.meta.requiresAuth && !main.isLoggedIn) {
    return '/login'
  }
})

serverPrefetch()

Access pinia via this.$pinia:

export default {
  serverPrefetch() {
    const store = useStore(this.$pinia)
    return store.fetchData()
  },
}

onServerPrefetch()

Works normally:

<script setup>
const store = useStore()

onServerPrefetch(async () => {
  await store.fetchData()
})
</script>

State Hydration

Serialize state on server and hydrate on client.

Server Side

Use devalue for XSS-safe serialization:

import devalue from 'devalue'
import { createPinia } from 'pinia'

const pinia = createPinia()
const app = createApp(App)
app.use(router)
app.use(pinia)

// After rendering, state is available
const serializedState = devalue(pinia.state.value)
// Inject into HTML as global variable

Client Side

Hydrate before any useStore() call:

const pinia = createPinia()
const app = createApp(App)
app.use(pinia)

// Hydrate from serialized state (e.g., from window.__pinia)
if (typeof window !== 'undefined') {
  pinia.state.value = JSON.parse(window.__pinia)
}

SSR Examples

Key Points

  1. Call stores inside functions, not at module scope
  2. Pass pinia instance when using stores outside components in SSR
  3. Hydrate state before calling any useStore()
  4. Use devalue or similar for safe serialization
  5. Avoid cross-request state pollution by creating fresh pinia per request
<!-- Source references: - https://pinia.vuejs.org/ssr/ -->

Source: SKILL.md on GitHub

No alerts9d3 checks · Risk SAFE
  • Gen Agent Trust Hub9d

    The skill provides comprehensive documentation and best practices for the Pinia state management library. It covers defining stores, using plugins, SSR hydration, and unit testing. No malicious patterns or security risks were identified; the skill includes security-conscious recommendations like using 'devalue' for XSS-safe serialization during server-side rendering.

  • Socket9d

    No alerts

  • Snyk9d

    Risk: LOW · No issues

Signed by skilld at de09c7b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated last week
Other metadata
metadata
{
  "author": "Anthony Fu",
  "version": "2026.1.28",
  "source": "Generated from https://github.com/vuejs/pinia, scripts located at https://github.com/antfu/skills"
}

README badge

README badge for onmax/nuxt-skills/pinia