Nuxt4 patterns

From aaione/everything-claude-code-zh · View interactive version

Nuxt4 patterns is an AI agent skill from the GitHub repository aaione/everything-claude-code-zh. Nuxt 4 应用模式,涵盖水合安全、性能、路由规则、懒加载和使用 useFetch 和 useAsyncData 的 SSR 安全数据获取。

How to use this skill

Skill file

Nuxt 4 模式

在构建或调试具有 SSR、混合渲染、路由规则或页面级数据获取的 Nuxt 4 应用时使用。

何时使用

水合安全

数据获取

const route = useRoute()

const { data: article, status, error, refresh } = await useAsyncData(
  () => `article:${route.params.slug}`,
  () => $fetch(`/api/articles/${route.params.slug}`),
)

const { data: comments } = await useFetch(`/api/articles/${route.params.slug}/comments`, {
  lazy: true,
  server: false,
})

路由规则

优先在 nuxt.config.ts 中使用 routeRules 进行渲染和缓存策略:

export default defineNuxtConfig({
  routeRules: {
    '/': { prerender: true },
    '/products/**': { swr: 3600 },
    '/blog/**': { isr: true },
    '/admin/**': { ssr: false },
    '/api/**': { cache: { maxAge: 60 * 60 } },
  },
})

按路由组选择路由规则,而不是全局。营销页面、目录、仪表板和 API 通常需要不同的策略。

懒加载和性能

<template>
  <LazyRecommendations v-if="showRecommendations" />
  <LazyProductGallery hydrate-on-visible />
</template>

审查清单

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.