提供 Nuxt 4 的 SSR 安全、性能优化与数据获取最佳实践模式
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "nuxt4-patterns" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/nuxt4-patterns/SKILL.md 2. 保存为 ~/.claude/skills/nuxt4-patterns/SKILL.md 3. 装好后重载技能,告诉我可以用了
请给我一套 Nuxt 4 模式,排查首页在 SSR 和客户端渲染结果不一致的问题。重点说明哪些状态应放在 useState、哪些逻辑只能在客户端执行,并提供安全示例代码。
输出一套避免水合报错的模式说明,并附带可直接参考的 Nuxt 4 示例代码。
请为 Nuxt 4 应用整理性能优化方案,涵盖组件懒加载、路由规则配置、关键内容优先渲染,以及如何减少首屏 JavaScript 负担。
得到一份面向 Nuxt 4 的性能优化清单,包含配置建议、实现模式和代码片段。
请比较 Nuxt 4 中 useFetch 和 useAsyncData 的适用场景,并给出一个 SSR 安全的数据获取方案,避免重复请求、空数据闪烁和客户端状态不一致。
输出 useFetch 与 useAsyncData 的选择建议,以及稳定的数据获取模式示例。
Use when building or debugging Nuxt 4 apps with SSR, hybrid rendering, route rules, or page-level data fetching.
useFetch, useAsyncData, or $fetchDate.now(), Math.random(), browser-only APIs, or storage reads directly into SSR-rendered template state.onMounted(), import.meta.client, ClientOnly, or a .client.vue component when the server cannot produce the same markup.useRoute()vue-routerroute.fullPath to drive SSR-rendered markup. URL fragments are client-only, which can create hydration mismatches.ssr: false as an escape hatch for truly browser-only areas, not a default fix for mismatches.await useFetch() for SSR-safe API reads in pages and components. It forwards server-fetched data into the Nuxt payload and avoids a second fetch on hydration.useAsyncData() when the fetcher is not a simple $fetch() call, when you need a custom key, or when you are composing multiple async sources.useAsyncData() a stable key for cache reuse and predictable refresh behavior.useAsyncData() handlers side-effect free. They can run during SSR and hydration.$fetch() for user-triggered writes or client-only actions, not top-level page data that should be hydrated from SSR.lazy: true, useLazyFetch(), or useLazyAsyncData() for non-critical data that should not block navigation. Handle status === 'pending' in the UI.server: false only for data that is not needed for SEO or the first paint.pick and prefer shallower payloads when deep reactivity is unnecessary.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,
})
Prefer routeRules in nuxt.config.ts for rendering and caching strategy:
export default defineNuxtConfig({
routeRules: {
'/': { prerender: true },
'/products/**': { swr: 3600 },
'/blog/**': { isr: true },
'/admin/**': { ssr: false },
'/api/**': { cache: { maxAge: 60 * 60 } },
},
})
prerender: static HTML at build timeswr: serve cached content and revalidate in the backgroundisr: incremental static regeneration on supported platformsssr: false: client-rendered routecache or redirect: Nitro-level response behaviorPick route rules per route group, not globally. Marketing pages, catalogs, dashboards, and APIs usually need different strategies.
Lazy prefix to dynamically import non-critical components.v-if so the chunk is not loaded until the UI actually needs it.<template>
<LazyRecommendations v-if="showRecommendations" />
<LazyProductGallery hydrate-on-visible />
</template>
defineLazyHydrationComponent() with a visibility or idle strategy.…
为 Quarkus 项目执行发布前验证闭环,涵盖构建、测试、扫描与差异审查。
帮助你编写或审查符合 React 18/19 最佳实践的组件与架构。