惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
W
WeLiveSecurity
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
K
Kaspersky official blog
NISL@THU
NISL@THU
Recent Commits to openclaw:main
Recent Commits to openclaw:main
M
Microsoft Research Blog - Microsoft Research
S
Secure Thoughts
罗磊的独立博客
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
Project Zero
Project Zero
Latest news
Latest news
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
L
LangChain Blog
PCI Perspectives
PCI Perspectives
博客园 - Franky
P
Palo Alto Networks Blog
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
量子位
L
LINUX DO - 热门话题
人人都是产品经理
人人都是产品经理
T
Tor Project blog
博客园 - 叶小钗
C
CERT Recently Published Vulnerability Notes
李成银的技术随笔
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 三生石上(FineUI控件)
Scott Helme
Scott Helme
雷峰网
雷峰网

DEV Community

Class and Pseudo Class Git & GitLab Basics 고객은 우리를 사기꾼으로 봤다: 아무도 믿지 않는 신사업을 단 둘이서 검증한 3개월 Cron Not Working on Mac? How to Fix the macOS Sleep Trap with launchd Deploy a Node.js App to STACKIT Kubernetes Engine With Managed Redis & PostgreSQL Slopsquatting & Remote Prompts: Why I Built a 38,000 Ticker Engine with Zero NPM Dependencies 05/20: TCP/IP vs OSI Model: The Ultimate Comparison My New Adventures in IT # Mitigating Market Inefficiency in eSports: A Stochastic Approach to EA Sports FC25 Modeling Don't let a billion RAG docs drown your 25-result pipeline Experienced devs are slower with AI tools. Nobody wants to admit it. I built an MCP-native OSINT framework that lets AI agents investigate from your terminal AWS Nitro Enclaves vs Intel TDX: Why Attestation Root Matters for Regulated Workloads Vibe Coding: Revolution or Risk in Software Development? - SmarterArticles S1E6 JSON Schema Explained: Validate Your API Data Before It Breaks Production Harness Tells Your Agent What to Do. GUI Agents Let It Actually Do It. Is AI actually replacing developers? Customizing Docker Images: Write Your First Dockerfile (2026) €40 n8n vs 28% weekly Anthropic quota. Which /goal layer should you actually run? Reviving glyph-v8: From a Forgotten Prototype to STRIDE - a Field-Aware Integer Coder 04/20: Data Encapsulation: How a Message Becomes Bits on the Wire Hướng Dẫn Thiết Lập Reasoning Proxy DeepSeek V4-Pro với Cursor (2026) Sofi Log #012: Agentic GDP — Solana Pay.sh & x402 Protocol Spec Input Types, Attributes, Self-Closing Tags, Hover Effect Absolute vs Relative Paths File Types (Regular, Directory, Link, Device, Socket, Pipe) From Arduino IDE to AVR GCC | AVR Bare Metal #1 Using Bitcoin as collateral without wrapping it: the design of a BTC collateral vault Unreal Engine 5 Skill System Architecture using GAS and GameplayTags 5 Things I Wish I Knew Before Building with Hermes Agent Thoughts on Codingame 2026 Spring challenge OUT WITH THE OLD IN WITH THE NEW Why are simple 1099 tax calculators online so horribly bloated? So I built my own "Why You're Not Getting Callbacks (It's Not Your Skills)" # How I Built a Retail Demand Forecasting App with Python and Streamlit Why We Deliberately Crush Lithium Batteries (UN38.3 Crush Testing Explained) Command History & Completion The Three-Body Problem: AI Code, Supply Chain Attacks, and the Talent Exodus 로컬 LLM 셋업 가이드 (v27) Building Better .NET Worker Services with Cursor Rules Generate Professional PDF Invoices via REST API — JSON In, PDF Out Redis: Big Keys Destroem o Desempenho Compartilhado Agentic AI for Cybersecurity: Autonomous Threat Detection and Response How to Automate Android Without Appium Cron vs systemd daemon: which one for Node.js? Designing XSLT transforms with parameters and multiple inputs I Downloaded Gemma4:e2b On My Macbook in 2 steps Building an Autonomous SRE Agent: From Raw Telemetry to Safe, AI-Driven Remediation The EU AI Act in 2026: Reading the Law After the Omnibus I had zero coding knowledge. Here is "RetroTube", a 2010 YouTube sandbox prototype I built using AI! How to Validate Environment Variables in TypeScript (and Why You Should) I Built a CLI Tool That Writes Better Git Commits Than I Do Transfer Fees, Metadata, and Soulbound Tokens: My First Real Token Experiments on Solana Stop Using Fetch() in React: A Better Way To Call Your Backend Creando un Tetris con JavaScript VI: Complicando el juego. DeepSeek's API Price Cut Changed My Claude Code and ChatGPT Math [Boost] Perl 🐪 Weekly #774 - Perl is too HOT How to Track AI Usage Without Losing Revenue (Complete Guide) 77 Rules Later: What Graduating Our First Stack Actually Looked Like RAG 시스템 실전 구축 (v26) When Premature Scaling Leads to Operator Burnout Multi-Repo Microservice Changes Are a Coordination Problem. I Solved It With AI Agent Teams. The Next Frontier: How Multi-Agent Systems are Redefining Productivity The Kimwolf Bust Just Outed Android Webcams as Botnet Fodder — Here's the Question Every Repurposed-Phone Camera Setup Has to Answer I'm an autonomous AI agent. I shipped 18 fixes to myself in one session. Building a Secure Future with Zero Trust Security Architecture Asynchronous Functions in Dart How I migrated magic-link login from Resend to AWS SES + Lambda five days before launch Edge Computing He creado una empresa ficticia IT/OT para poder encontrar sus vulnerabilidades y reforzar su seguridad en sus activos críticos Why I Built @editora/react I built a tiny UGC script generator because hooks are the hardest part The Phone Is Becoming the New Terminal Why Most AI Music Tools Feel Wrong to Developers Goroutines vs. Promises: Why Go and JavaScript Look at Concurrency Completely Differently How I Use Antigravity 2.0 to Navigate Open-Source Codebases and Make Better Technical Decisions Understanding Basic HTML & CSS Concepts for Beginners Go Error Handling: Annoying or Awesome? Your To-Do List Doesn't Know You — So I Gave Mine Three Brains Shell Basics (Bash, Zsh, Sh) Free MongoDB GUI Tool for Developers, Students, and Teams Designing High-Performance Blockchain Indexers Choosing Models for an Agentic Chat App on Amazon Bedrock How Smart Growth Teams Automate Their Marketing Stack in 2026 (Without Hiring More People) What I Learned About Memory-Augmented AI Agents Seven Docker Tips Every Engineer Should Know (from Docker Captains) Welcome to the Fast-Food Era of Testing: Over-Weight by Tests How to use Claude in vscode? Prompt Engineering for Automated Evaluation: Making LLMs the Judge in AI Builder Solutions Full Stack Projects Are Not Enough Anymore Virtualization & Cloud Basics Orakle: Turning Raw Blockchain Data into Intelligence with Gemma 4 Building an Autoposting Pipeline with Hermes Agent: Why Waterfall Beats Parallel, and the Edge Cases Nobody Talks About OpenShift Virtualization Migration Advisor — Local-First, Powered by Gemma 4 26B MoE WebMCP is coming — so I’m building webmcp.js I Disappeared for 4 Months After Launch - Here's What Brought Me Back Jira Is Turing-Complete (And You've Been Coding in It) NyayAI: Building an AI Legal Assistant for 1.4 Billion People — A Technical Deep Dive E-commerce Order Automation: Stripe + Invoice + Shipping Workflow
Cache Everything: Advanced Caching Strategies in Vue 3 & Nuxt 4
Parsa Jirava · 2026-05-25 · via DEV Community

Table of Contents

  1. Layer 1 — Reactive Variable Caching (Vue Reactivity System)
  2. Layer 2 — Computed Property Caching & Memoization
  3. Layer 3 — Composable-Level Data Caching
  4. Layer 4 — Nuxt Data Fetching Cache (useFetch / useAsyncData)
  5. Layer 5 — Server Route Caching with Nitro
  6. Layer 6 — Route-Level Cache Rules (routeRules)
  7. Layer 7 — HTTP & Browser Caching Headers
  8. Layer 8 — Pinia as a Client-Side Cache Store
  9. Quick Reference Cheat Sheet

Layer 1 — Reactive Variable Caching {#layer-1}

Before we talk about network caches, let's talk about reactivity overhead — because creating unnecessary deep reactive proxies is a silent performance killer in large Vue apps.

shallowRef and shallowReactive for large data

By default, ref() and reactive() deeply proxy every nested property. For large API responses, table rows, or chart datasets this is wasteful — you're paying to track changes you'll never make.

// ❌ Deep reactive — tracks every nested field on every item
const rows = ref<Row[]>([])

// ✅ Shallow reactive — triggers only when the array reference changes
const rows = shallowRef<Row[]>([])

async function loadPage(page: number) {
  rows.value = await $fetch(`/api/rows?page=${page}`)
  // Replacing the whole reference triggers reactivity cheaply
}

Enter fullscreen mode Exit fullscreen mode

Rule: Use shallowRef when you replace the whole object wholesale (pagination, API responses). Use deep ref only when you mutate individual nested properties.

Object.freeze for truly static data

If your data never changes after fetch — config objects, lookup tables, static lists — freeze it entirely to opt out of Vue's proxy system:

const countries = shallowRef(Object.freeze(await $fetch('/api/countries')))
// Vue won't bother tracking any of this — zero reactivity overhead

Enter fullscreen mode Exit fullscreen mode

v-once and v-memo in templates

<!-- Renders once and never re-renders, even if parent does -->
<HeavyStaticChart v-once :data="staticConfig" />

<!-- Re-renders only when userId changes -->
<UserCard
  v-memo="[userId]"
  :user="user"
  :metadata="metadata"
/>

Enter fullscreen mode Exit fullscreen mode

v-memo is especially powerful inside v-for loops with large stable lists.


Layer 2 — Computed Property Caching & Memoization {#layer-2}

Vue's computed() is itself a cache — it stores its last result and only recomputes when a tracked dependency changes. But there are patterns that break this guarantee.

Keep computed pure

// ❌ Breaks caching — Math.random() makes it non-deterministic
const discountedPrice = computed(() => price.value * Math.random())

// ✅ Pure — Vue can cache this safely
const discountedPrice = computed(() => price.value * 0.9)

Enter fullscreen mode Exit fullscreen mode

Chain computed for granular invalidation

Instead of one giant computed with many dependencies, chain smaller ones. Each step caches independently:

const rawOrders = shallowRef<Order[]>([])

// Each computed caches its own slice of the pipeline
const paidOrders = computed(() =>
  rawOrders.value.filter(o => o.status === 'paid')
)

const sortedOrders = computed(() =>
  [...paidOrders.value].sort((a, b) => b.total - a.total)
)

const topTenOrders = computed(() => sortedOrders.value.slice(0, 10))

Enter fullscreen mode Exit fullscreen mode

Changing rawOrders recomputes all three. But if only sortedOrders's sort key changes, only sortedOrders and topTenOrders invalidate.

Manual memoization for argument-based caching

computed doesn't accept arguments. For functions that take parameters, build a Map-based memo:

function useMemoized<A, R>(fn: (arg: A) => R) {
  const cache = new Map<A, R>()
  return (arg: A): R => {
    if (cache.has(arg)) return cache.get(arg)!
    const result = fn(arg)
    cache.set(arg, result)
    return result
  }
}

// Usage in a composable
const getProductTax = useMemoized((productId: string) => {
  return expensiveTaxCalculation(productId)
})

Enter fullscreen mode Exit fullscreen mode


Layer 3 — Composable-Level Data Caching {#layer-3}

A composable defined outside the component instance acts as a singleton module-level cache — data persists across component mounts and unmounts.

// composables/useProductCatalog.ts

// Defined at module scope — shared across all consumers
const catalog = shallowRef<Product[]>([])
const lastFetched = ref<number | null>(null)
const TTL = 5 * 60 * 1000 // 5 minutes

export function useProductCatalog() {
  async function fetchIfStale() {
    const now = Date.now()
    if (lastFetched.value && now - lastFetched.value < TTL) {
      return // Still fresh, use the cache
    }
    catalog.value = await $fetch('/api/products')
    lastFetched.value = now
  }

  return { catalog, fetchIfStale }
}

Enter fullscreen mode Exit fullscreen mode

Any component calling useProductCatalog() shares the same catalog ref. The network request fires once and the result is reused until TTL expires.

Nuxt note: Be aware that module-level state is shared across all SSR requests on the server. Use useState() or useNuxtApp() for server-safe singleton state in Nuxt.


Layer 4 — Nuxt Data Fetching Cache (useFetch / useAsyncData) {#layer-4}

This is where Nuxt 4 brings the most significant improvements over Nuxt 3.

How the built-in cache works

useFetch and useAsyncData key-deduplicate requests. Multiple components calling the same key share one data, error, and status ref:

// components/UserProfile.vue AND components/UserBadge.vue
// Both call this — only ONE network request fires
const { data: user } = await useAsyncData('user-123', () =>
  $fetch('/api/users/123')
)

Enter fullscreen mode Exit fullscreen mode

In Nuxt 4, data fetched on the server is forwarded to the client in the payload, eliminating the double-fetch (server + hydration) problem entirely.

Reactive keys for dynamic data

// pages/users/[id].vue
const route = useRoute()

// computed key — Nuxt refetches when route.params.id changes
const userId = computed(() => `user-${route.params.id}`)

const { data: user } = await useAsyncData(
  userId,
  () => $fetch(`/api/users/${route.params.id}`)
)

Enter fullscreen mode Exit fullscreen mode

In Nuxt 4, changing userId automatically purges the old cached data and triggers a fresh fetch.

getCachedData — custom client-side cache logic

This is the most underused feature. getCachedData lets you intercept the cache lookup and decide whether to return stale data or trigger a fresh request:

const nuxtApp = useNuxtApp()

const { data: posts } = await useAsyncData(
  'blog-posts',
  () => $fetch('/api/posts'),
  {
    getCachedData(key) {
      // Return payload data if it already exists — skip the fetch
      return nuxtApp.payload.data[key] ?? nuxtApp.static.data[key]
    }
  }
)

Enter fullscreen mode Exit fullscreen mode

For a TTL-aware variant:

const CACHE_TTL = 60_000 // 1 minute
const cache = new Map<string, { data: unknown; ts: number }>()

const { data } = await useAsyncData('dashboard-stats', () =>
  $fetch('/api/stats'), {
    getCachedData(key) {
      const entry = cache.get(key)
      if (!entry) return undefined
      if (Date.now() - entry.ts > CACHE_TTL) {
        cache.delete(key)
        return undefined
      }
      return entry.data
    }
  }
)

Enter fullscreen mode Exit fullscreen mode

staleTime — the declarative TTL option

Nuxt 4 introduced staleTime as a first-class option, eliminating the need for manual getCachedData in most cases:

const { data } = await useFetch('/api/products', {
  key: 'products-list',
  staleTime: 5 * 60 * 1000 // Data is considered fresh for 5 minutes
})

Enter fullscreen mode Exit fullscreen mode

Lazy loading and deferred fetching

// useLazyFetch — doesn't block navigation; shows loading state immediately
const { data, pending } = useLazyFetch('/api/recommendations', {
  key: 'recommendations',
  server: false // Only fetch on client — excludes from SSR payload
})

Enter fullscreen mode Exit fullscreen mode

Abort signals for cancelled requests

Nuxt 4 exposes an AbortSignal in useAsyncData's handler, letting you cancel in-flight requests:

const { data, refresh } = await useAsyncData(
  'live-feed',
  (_nuxtApp, { signal }) => $fetch('/api/feed', { signal })
)

Enter fullscreen mode Exit fullscreen mode


Layer 5 — Server Route Caching with Nitro {#layer-5}

Nuxt's server engine (Nitro) exposes two powerful caching primitives for API routes.

cachedEventHandler — cache entire route responses

// server/api/products.get.ts
import { cachedEventHandler } from 'nitropack/runtime'
import type { H3Event } from 'h3'

export default cachedEventHandler(
  async (event) => {
    const products = await db.product.findMany({ where: { active: true } })
    return products
  },
  {
    maxAge: 60 * 10,           // Cache for 10 minutes
    staleMaxAge: 60 * 60,      // Serve stale for up to 1 hour while revalidating
    swr: true,                 // Stale-While-Revalidate behaviour
    getKey: (event: H3Event) => event.path, // Cache key per URL
    varies: ['accept-language'] // Vary by header
  }
)

Enter fullscreen mode Exit fullscreen mode

cachedFunction — cache individual server functions

For shared logic called across multiple routes:

// server/utils/catalog.ts
import { defineCachedFunction } from 'nitropack/runtime'

export const getCatalog = defineCachedFunction(
  async (category: string) => {
    return db.product.findMany({ where: { category } })
  },
  {
    maxAge: 60 * 30, // 30 minutes
    name: 'catalog',
    getKey: (category: string) => category
  }
)

// server/api/electronics.get.ts
export default defineEventHandler(() => getCatalog('electronics'))

// server/api/apparel.get.ts
export default defineEventHandler(() => getCatalog('apparel'))

Enter fullscreen mode Exit fullscreen mode

Cache storage backends

Configure the Nitro cache driver in nuxt.config.ts:

// nuxt.config.ts
export default defineNuxtConfig({
  nitro: {
    storage: {
      cache: {
        driver: 'redis',
        url: process.env.REDIS_URL
      }
    }
  }
})

Enter fullscreen mode Exit fullscreen mode

Nitro's unstorage layer supports redis, fs, memory, cloudflare-kv, vercel-kv, and more — no code change required when switching backends.


Layer 6 — Route-Level Cache Rules (routeRules) {#layer-6}

routeRules lets you set rendering strategy and caching behaviour per route pattern directly in config — no middleware, no per-page logic.

// nuxt.config.ts
export default defineNuxtConfig({
  routeRules: {
    // Marketing pages — full static, cached forever at CDN
    '/': { prerender: true },
    '/about': { prerender: true },

    // Product pages — ISR: regenerate every 60 seconds
    '/products/**': { isr: 60 },

    // API responses — cached at CDN edge for 10 minutes
    '/api/catalog/**': { cache: { maxAge: 60 * 10 } },

    // Dashboard — always SSR, never cached
    '/dashboard/**': { ssr: true, cache: false },

    // User-specific pages — no caching, always fresh
    '/account/**': { cache: false },

    // Admin — redirect to login
    '/admin/**': { redirect: '/login' }
  }
})

Enter fullscreen mode Exit fullscreen mode

Pro tip: Start with conservative TTLs and increase them over time. Stale pages are almost always better than slow pages, but you need to understand your data freshness requirements first.


Layer 7 — HTTP & Browser Caching Headers {#layer-7}

Nuxt and Nitro let you set fine-grained HTTP cache headers on any response.

// server/api/static-config.get.ts
export default defineEventHandler((event) => {
  setResponseHeaders(event, {
    'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
    'CDN-Cache-Control': 'max-age=86400',
    'Vary': 'Accept-Encoding'
  })

  return getStaticConfig()
})

Enter fullscreen mode Exit fullscreen mode

Cache-Control directive cheat sheet

Directive Meaning
public CDNs and proxies can cache this
private Only the end browser should cache this
max-age=N Fresh for N seconds
s-maxage=N CDN-specific TTL (overrides max-age for proxies)
stale-while-revalidate=N Serve stale for N seconds while refreshing in the background
no-store Never cache (auth, sensitive data)
immutable Asset will never change — skip revalidation entirely

For Nuxt's static assets (JS, CSS, images), Nitro automatically appends content-hash fingerprints and sets immutable headers, so these are cached forever at the browser and CDN level.


Layer 8 — Pinia as a Client-Side Cache Store {#layer-8}

Pinia is more than state management — it's your application's runtime cache layer for data that needs to be shared and reused across many routes and components.

// stores/products.ts
import { defineStore } from 'pinia'

interface ProductCache {
  data: Product[]
  fetchedAt: number
}

export const useProductStore = defineStore('products', () => {
  const cache = new Map<string, ProductCache>()
  const TTL = 5 * 60 * 1000

  function isStale(key: string): boolean {
    const entry = cache.get(key)
    if (!entry) return true
    return Date.now() - entry.fetchedAt > TTL
  }

  async function fetchByCategory(category: string): Promise<Product[]> {
    if (!isStale(category)) {
      return cache.get(category)!.data
    }

    const data = await $fetch<Product[]>(`/api/products`, {
      params: { category }
    })

    cache.set(category, { data, fetchedAt: Date.now() })
    return data
  }

  function invalidate(category?: string) {
    if (category) cache.delete(category)
    else cache.clear()
  }

  return { fetchByCategory, invalidate }
})

Enter fullscreen mode Exit fullscreen mode

Persisting Pinia state across page reloads

Use pinia-plugin-persistedstate for selective persistence:

// stores/userPreferences.ts
export const usePreferencesStore = defineStore('preferences', () => {
  const theme = ref<'light' | 'dark'>('light')
  const language = ref('en')
  return { theme, language }
}, {
  persist: {
    storage: localStorage,
    pick: ['theme', 'language'] // Only persist these fields
  }
})

Enter fullscreen mode Exit fullscreen mode


Quick Reference Cheat Sheet {#cheatsheet}

Problem Solution Where
Large API array re-renders unnecessarily shallowRef instead of ref Vue component / composable
Static template subtree re-renders v-once Template
Expensive list items re-render v-memo="[id]" Template v-for
Derived state re-computed too often Chain computed() Component / composable
Same request fired by multiple components Same useAsyncData key Nuxt pages
Data fetched on server AND client useAsyncData / useFetch (never raw $fetch in setup) Nuxt pages
Client navigates back, unnecessary refetch getCachedData or staleTime useAsyncData options
API route hammered by many users cachedEventHandler with Nitro server/api/
Shared server function called redundantly defineCachedFunction server/utils/
Marketing page slow to load prerender: true in routeRules nuxt.config.ts
Product page needs periodic freshness isr: 60 in routeRules nuxt.config.ts
State shared across components Pinia store with TTL map stores/
Preferences survive page reload pinia-plugin-persistedstate Pinia plugin

Key Takeaways

Caching in Vue and Nuxt isn't a single decision — it's a strategy applied at every layer of the stack:

  • Reactivity layer: Reach for shallowRef by default for data that's replaced wholesale. Reserve deep reactivity for objects you mutate in place.
  • Computed layer: Keep them pure, chain them for granular invalidation, and use manual memos for argument-based functions.
  • Composable layer: Module-scope state is a free singleton cache — use it deliberately.
  • Data fetching layer: useAsyncData with getCachedData or staleTime gives you precise control over client-side request deduplication.
  • Server layer: cachedEventHandler and defineCachedFunction push caching to the edge, absorbing load before it ever touches your database.
  • Route layer: routeRules lets you declaratively assign the right rendering and caching strategy per page pattern.
  • HTTP layer: Set Cache-Control headers correctly to get CDN and browser caching without extra infrastructure.
  • State layer: Pinia with a TTL map is a battle-tested pattern for runtime client cache that survives navigation.

The right combination depends on your data's freshness requirements, your traffic patterns, and your deployment target. Start conservative, measure, and increase TTLs once you understand the shape of your data.


Found this useful? Follow me for more deep dives into Vue and Nuxt performance. Got a caching pattern you swear by that I missed? Drop it in the comments.