All skills
onmax avatar

/nuxt

@de09c7b official
by Maxonmax/nuxt-skills715 stars
38

Nuxt full-stack Vue framework with SSR, auto-imports, and file-based routing. Use when working with Nuxt apps, server routes, useFetch, middleware, or hybrid rendering.

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

This session only. Nothing lands on disk.

referencesbest-practices-ssr.md

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

SSR Best Practices

Patterns for avoiding common SSR pitfalls: context leaks, hydration mismatches, and composable errors.

The "Nuxt Instance Unavailable" Error

This error occurs when calling Nuxt composables outside the proper context.

❌ Wrong: Composable Outside Setup

// composables/bad.ts
// Called at module level - no Nuxt context!
const config = useRuntimeConfig()

export function useMyComposable() {
  return config.public.apiBase
}

✅ Correct: Composable Inside Function

// composables/good.ts
export function useMyComposable() {
  // Called inside the composable - has context
  const config = useRuntimeConfig()
  return config.public.apiBase
}

Valid Contexts for Composables

Nuxt composables work in:

  • <script setup> blocks
  • setup() function
  • defineNuxtPlugin() callbacks
  • defineNuxtRouteMiddleware() callbacks
// ✅ Plugin
export default defineNuxtPlugin(() => {
  const config = useRuntimeConfig() // Works
})

// ✅ Middleware
export default defineNuxtRouteMiddleware(() => {
  const route = useRoute() // Works
})

Avoid State Leaks Between Requests

❌ Wrong: Module-level State

// composables/bad.ts
// This state is SHARED between all requests on server!
const globalState = ref({ user: null })

export function useUser() {
  return globalState
}

✅ Correct: Use useState

// composables/good.ts
export function useUser() {
  // useState creates request-isolated state
  return useState('user', () => ({ user: null }))
}

Why This Matters

On the server, module-level state persists across requests, causing:

  • Data leaking between users
  • Security vulnerabilities
  • Memory leaks

Hydration Mismatch Prevention

Hydration mismatches occur when server HTML differs from client render.

❌ Wrong: Browser APIs in Setup

<script setup>
// localStorage doesn't exist on server!
const theme = localStorage.getItem('theme') || 'light'
</script>

✅ Correct: Use SSR-safe Alternatives

<script setup>
// useCookie works on both server and client
const theme = useCookie('theme', { default: () => 'light' })
</script>

❌ Wrong: Random/Time-based Values

<template>
  <div>{{ Math.random() }}</div>
  <div>{{ new Date().toLocaleTimeString() }}</div>
</template>

✅ Correct: Use useState for Consistency

<script setup>
// Value is generated once on server, hydrated on client
const randomValue = useState('random', () => Math.random())
</script>

<template>
  <div>{{ randomValue }}</div>
</template>

❌ Wrong: Conditional Rendering on Client State

<template>
  <!-- window doesn't exist on server -->
  <div v-if="window?.innerWidth > 768">Desktop</div>
</template>

✅ Correct: Use CSS or ClientOnly

<template>
  <!-- CSS media queries work on both -->
  <div class="hidden md:block">Desktop</div>
  <div class="md:hidden">Mobile</div>

  <!-- Or use ClientOnly for JS-dependent rendering -->
  <ClientOnly>
    <ResponsiveComponent />
    <template #fallback>Loading...</template>
  </ClientOnly>
</template>

Browser-only Code

Use import.meta.client

<script setup>
if (import.meta.client) {
  // Only runs in browser
  window.addEventListener('scroll', handleScroll)
}
</script>

Use onMounted for DOM Access

<script setup>
const el = ref<HTMLElement>()

onMounted(() => {
  // Safe - only runs on client after hydration
  el.value?.focus()
  initThirdPartyLib()
})
</script>

Dynamic Imports for Browser Libraries

<script setup>
onMounted(async () => {
  const { Chart } = await import('chart.js')
  new Chart(canvas.value, config)
})
</script>

Server-only Code

Use import.meta.server

<script setup>
if (import.meta.server) {
  // Only runs on server
  const secrets = useRuntimeConfig().apiSecret
}
</script>

Server Components

<!-- components/ServerData.server.vue -->
<script setup>
// This entire component only runs on server
const data = await fetchSensitiveData()
</script>

<template>
  <div>{{ data }}</div>
</template>

Async Composable Patterns

❌ Wrong: Await Before Composable

<script setup>
await someAsyncOperation()
const route = useRoute() // May fail - context lost after await
</script>

✅ Correct: Get Context First

<script setup>
// Get all composables before any await
const route = useRoute()
const config = useRuntimeConfig()

await someAsyncOperation()
// Now safe to use route and config
</script>

Plugin Best Practices

Client-only Plugins

// plugins/analytics.client.ts
export default defineNuxtPlugin(() => {
  // Only runs on client
  initAnalytics()
})

Server-only Plugins

// plugins/server-init.server.ts
export default defineNuxtPlugin(() => {
  // Only runs on server
  initServerConnections()
})

Provide/Inject Pattern

// plugins/api.ts
export default defineNuxtPlugin(() => {
  const api = createApiClient()

  return {
    provide: {
      api,
    },
  }
})
<script setup>
const { $api } = useNuxtApp()
const data = await $api.get('/users')
</script>

Third-party Library Integration

❌ Wrong: Import at Top Level

<script setup>
import SomeLibrary from 'browser-only-lib' // Breaks SSR
</script>

✅ Correct: Dynamic Import

<script setup>
let library: typeof import('browser-only-lib')

onMounted(async () => {
  library = await import('browser-only-lib')
  library.init()
})
</script>

Use ClientOnly Component

<template>
  <ClientOnly>
    <BrowserOnlyComponent />
    <template #fallback>
      <div class="skeleton">Loading...</div>
    </template>
  </ClientOnly>
</template>

Debugging SSR Issues

Check Rendering Context

<script setup>
console.log('Server:', import.meta.server)
console.log('Client:', import.meta.client)
</script>

Use Nuxt DevTools

DevTools shows payload data and hydration state.

Common Error Messages

Error Cause
"Nuxt instance unavailable" Composable called outside setup context
"Hydration mismatch" Server/client HTML differs
"window is not defined" Browser API used during SSR
"document is not defined" DOM access during SSR
<!-- Source references: - https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables - https://nuxt.com/docs/guide/best-practices/hydration - https://nuxt.com/docs/getting-started/state-management#best-practices -->

Source: SKILL.md on GitHub

No alerts9d5 checks · Risk SAFE
  • Gen Agent Trust Hub9d

    This skill provides a comprehensive technical reference for the Nuxt full-stack framework, including documentation on directory structure, routing, data fetching, and deployment. No security issues or malicious patterns were detected.

  • Socket9d

    No alerts

  • Snyk9d

    Risk: LOW · No issues

  • Runlayer7mo

    8 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

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.6.22",
  "source": "Generated from https://github.com/nuxt/nuxt, scripts located at https://github.com/antfu/skills"
}

README badge

README badge for onmax/nuxt-skills/nuxt

Provides patterns and reference files for Nuxt 4.3+ projects, covering server routes with h3 validation, file-based routing, middleware, plugins, and Nuxt composables. Use this when building API endpoints, setting up pages and layouts, or working with Nuxt-specific features like auto-imports and route guards.

Generated from the current SKILL.md.

Does this skill cover Nuxt 3 or only Nuxt 4+?
This skill targets Nuxt 4.3+ only. It includes patterns specific to Nuxt 4, such as typed router and getRouterParam, and does not cover Nuxt 2 or 3 conventions.
Does this skill include guidance for h3 and Nitro?
Yes. The skill covers h3 v1 helpers (validation, WebSocket, SSE) and nitropack v2 patterns for server routes and middleware.
Does this cover UI components like buttons or forms?
No. The skill covers Nuxt-specific components (NuxtLink, NuxtImg, NuxtTime) but delegates UI component libraries to the separate nuxt-ui skill.
Can I use this skill for creating Nuxt modules?
No. For module development, use the nuxt-modules skill instead. This skill focuses on building applications with Nuxt 4+.
Does this skill include database or content management guidance?
No. For databases, use the nuxthub skill; for content-driven sites, use the nuxt-content skill.

Generated from the current SKILL.md. These answers refresh after source changes.