with one click
nuxt4-patterns
Nuxt 4 应用模式,涵盖水合安全、性能优化、路由规则、懒加载,以及使用 useFetch 和 useAsyncData 进行 SSR 安全的数据获取。
Menu
Nuxt 4 应用模式,涵盖水合安全、性能优化、路由规则、懒加载,以及使用 useFetch 和 useAsyncData 进行 SSR 安全的数据获取。
| name | nuxt4-patterns |
| description | Nuxt 4 应用模式,涵盖水合安全、性能优化、路由规则、懒加载,以及使用 useFetch 和 useAsyncData 进行 SSR 安全的数据获取。 |
| origin | ECC |
在构建或调试具有 SSR、混合渲染、路由规则或页面级数据获取的 Nuxt 4 应用时使用。
useFetch、useAsyncData 或 $fetch 进行页面或组件数据获取Date.now()、Math.random()、仅限浏览器的 API 或存储读取直接放入 SSR 渲染的模板状态中。onMounted()、import.meta.client、ClientOnly 或 .client.vue 组件后面。useRoute() 组合式函数,而不是来自 vue-router 的那个。route.fullPath 来驱动 SSR 渲染的标记。URL 片段是仅客户端的,这可能导致水合不匹配。ssr: false 视为真正仅限浏览器区域的逃生舱口,而不是解决不匹配的默认修复方法。await useFetch() 进行 SSR 安全的 API 读取。它将服务器获取的数据转发到 Nuxt 有效负载中,并避免在水合时进行第二次获取。$fetch() 调用,或者需要自定义键,或者正在组合多个异步源时,使用 useAsyncData()。useAsyncData() 提供一个稳定的键以重用缓存并实现可预测的刷新行为。useAsyncData() 处理程序无副作用。它们可能在 SSR 和水合期间运行。$fetch() 用于用户触发的写入或仅客户端操作,而不是应该从 SSR 水合而来的顶级页面数据。lazy: true、useLazyFetch() 或 useLazyAsyncData()。在 UI 中处理 status === 'pending'。server: false。pick 修剪有效负载大小,并在不需要深层响应性时优先使用较浅的有效负载。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 } },
},
})
prerender:在构建时生成静态 HTMLswr:提供缓存内容并在后台重新验证isr:在支持的平台上进行增量静态再生ssr: false:客户端渲染的路由cache 或 redirect:Nitro 级别的响应行为按路由组选择路由规则,而非全局设置。营销页面、产品目录、仪表板和 API 通常需要不同的策略。
Lazy 前缀来动态导入非关键组件。v-if 有条件地渲染懒加载组件,以便在 UI 实际需要时才加载该代码块。<template>
<LazyRecommendations v-if="showRecommendations" />
<LazyProductGallery hydrate-on-visible />
</template>
defineLazyHydrationComponent() 配合可见性或空闲策略。NuxtLink,以便 Nuxt 可以预取路由组件和生成的有效负载。useFetch 或 useAsyncData,而非顶层的 $fetchTrack and report Claude Code token usage, spending, and budgets from the local ECC cost-tracker metrics log. Use when the user asks about costs, spending, usage, tokens, budgets, or cost breakdowns by model, session, or date.
Instinct-based learning system that observes sessions via hooks, creates atomic instincts with confidence scoring, and evolves them into skills/commands/agents. v2.1 adds project-scoped instincts to prevent cross-project contamination.
Create reproducible, cross-platform (macOS/Linux) development environments with Flox, a declarative Nix-based environment manager. Use when setting up project toolchains for any language, installing system-level dependencies (compilers, databases, native libs like openssl/BLAS), pinning exact package versions for a team, running local services (PostgreSQL, Redis, Kafka), onboarding developers with one command, or solving 'works on my machine' problems — including agent/vibe-coding setups that need project-scoped tools without sudo. Also use when the user mentions .flox/, manifest.toml, flox activate, or FloxHub.
Commercial-grade Python installer expert for Windows: Nuitka extreme compilation, dist slimming, DLL footprint analysis, and Inno Setup packaging to ship the smallest, fastest installers. Use only for advanced packaging/optimization (minimal size, fast startup), not basic script-to-exe conversion. 中文触发:Nuitka 极限优化、Python 商业打包、极限编译 Python、dist 瘦身、DLL 分析、最小安装包、最快启动、商业级打包风格
Use when a brand needs to discover or articulate its identity through structured multi-session interviews. Covers purpose, positioning, audience, personality, voice, narrative, and founder-brand tension across 8 modules using laddering, 5 Whys, and projective techniques. Produces a resumable session with disk-persisted state and a master brandbook (90_SYNTHESIS.md).
Use when a brand needs to discover or articulate its identity through structured multi-session interviews. Covers purpose, positioning, audience, personality, voice, narrative, and founder-brand tension across 8 modules using laddering, 5 Whys, and projective techniques. Produces a resumable session with disk-persisted state and a master brandbook (90_SYNTHESIS.md).