Hotline:15911225507
Long Xiao
Login
Home-All Posts-Nuxt.js英文,Vue.js英文-Main Content

Pinia State Persistence & Memory Governance in Nuxt 4

龙霄龙霄
Nuxt.js英文, Vue.js英文
2 months ago
0
0
4.77K
A systematic walkthrough of state management across 25 Pinia Stores in a large Nuxt 4 project, covering shallowRef memory optimization, localStorage persistence whitelisting, route-level KeepAlive control, and a comprehensive memory profiling toolchain.
Complete guide to Pinia Store memory management, persistence strategies, and route-level KeepAlive control

1. Store Architecture

25 independent Pinia Stores across four categories:

CategoryStoresPurpose
ContentPostStore, ArchiveStore, SearchStore, DocumentStore, VideoStoreArticles/archives/search/docs/video
UserLoginStore, UserCenterStore, BookmarkStore, CommentStoreAuth/profile/bookmarks/comments
BusinessCartStore, ShopStore, ProductStore, OrderStore, PayStoreCart/shop/products/orders/payments
SystemGeneralStore, HeaderStore, BuilderStore, EventStoreConfig/header/builder/events

2. Pinia Persistence

modules: [
    ['@pinia/nuxt', { autoImports: ['defineStore', 'storeToRefs', 'acceptHMRUpdate'] }],
    'pinia-plugin-persistedstate/nuxt'
]

piniaPluginPersistedstate: {
    cookieOptions: { sameSite: 'strict', secure: process.env.NODE_ENV !== 'development' },
    storage: 'localStorage',
    debug: process.env.NODE_ENV === 'development'
}
export const useCartStore = defineStore('cart', () => {
    const items = ref([])
}, {
    persist: { key: 'salong_cart', storage: localStorage, paths: ['items'] }
})

3. shallowRef Memory Optimization

// ❌ ref deeply wraps entire object tree
const posts = ref<Post[]>([])

// ✅ shallowRef only tracks reference changes
const posts = shallowRef<Post[]>([])

With 100 articles (~500KB): ref adds ~2MB overhead, shallowRef adds ~8KB — 250x less.

4. Route-Level KeepAlive

const KEEPALIVE_MAX = 12
const keepaliveExcludeRules = [/^\\/builder/, /^\\/login/, /^\\/cart/, ...]

export default defineNuxtRouteMiddleware((to) => {
    if (shouldKeepAlive(to.path)) { to.meta.keepalive = { max: KEEPALIVE_MAX } }
})
RouteKeepAliveReason
/High-frequency
.html content pagesPreserve scroll position
/builderReal-time editing
/cartData freshness

5. Memory Governance Toolchain

pnpm memory:test    # Unit tests
pnpm memory:profile # Memory profiling (--expose-gc)
pnpm memory:load    # Load testing
pnpm memory:analyze # Log analysis

6. Summary

LayerMechanismControls
StoreshallowRefLarge arrays/objects
Persistencepaths whitelistKey fields only
RouteKeepAlive max=12High-frequency pages
CacheLRU budget500 entries / 32MB
Tags:
本文原创,作者:龙霄,其版权均为龙霄所有。如需转载,请注明出处:https://lx.yfdxs.com/1426.html
龙霄

龙霄

Lv1Rec2
以 Nuxt.js 之力,焕新 WordPress 体验
159.19W113.98W
Loading...
Share:
1
Naive UI Integration &amp; UnoCSS Atomic CSS in Nuxt 4
Naive UI Integration &amp; UnoCSS Atomic CSS in Nuxt 4Previous
Nuxt 4 + WordPress GraphQL: Data Management Strategy &amp; Performance OptimizationNext
Nuxt 4 + WordPress GraphQL: Data Management Strategy &amp; Performance Optimization
相关文章
Total: 14
Nuxt 4 i18n Architecture &amp; Multi-Language Routing Strategy

Nuxt 4 i18n Architecture & Multi-Language Routing Strategy

Acompleteguidetoathree-language(zh-CN/zh-TW/en)i18nsetupwith@n…
龙霄龙霄
Nuxt.js英文, Vue.js英文
2 months ago
0
0
2.64K
0
Longxiao Theme-Visual Page Builder: Build Professional-Grade Websites in One Afternoon

Longxiao Theme-Visual Page Builder: Build Professional-Grade Websites in One Afternoon

Saygoodbyetothetraditionalwebsitebuildingworkflowofwritingcode…
龙霄龙霄
Nuxt.js英文, WordPress英文
1 months ago
0
0
2.55K
0
Nuxt.js + WordPress Architecture: A Comprehensive Guide to Performance, Speed, Security, and Caching

Nuxt.js + WordPress Architecture: A Comprehensive Guide to Performance, Speed, Security, and Caching

WhenNuxt.js'smodernfrontendengineeringcapabilitiesmeetWordPress's…
龙霄龙霄
Nuxt.js英文, WordPress英文
1 months ago
0
0
4.21K
0
Longxiao Theme &#8211; E-Commerce Platform: A Unified Transaction Engine for Course, Physical Product, and Event Marketing Plugins

Longxiao Theme – E-Commerce Platform: A Unified Transaction Engine for Course, Physical Product, and Event Marketing Plugins

Oneunderlyingtransactionpipeline,threeproducttypes,unifiedshoppi…
龙霄龙霄
Nuxt.js英文
1 months ago
0
0
2.60K
0
Longxiao Theme-E-Commerce &amp; Membership Integrated Product Plugin: Turn Content Monetization from [Can Sell] to [Sells Well]

Longxiao Theme-E-Commerce & Membership Integrated Product Plugin: Turn Content Monetization from [Can Sell] to [Sells Well]

Acompletee-commercetransactionpipelinepairedwithamulti-tiermemb…
龙霄龙霄
Nuxt.js英文, WordPress英文
1 months ago
0
0
2.72K
0
Event Plugin &#8211; Event Management Plugin: An All-in-One Event Operations Solution from Planning to Execution

Event Plugin – Event Management Plugin: An All-in-One Event Operations Solution from Planning to Execution

Behindeverysuccessfuleventliescomplexcoordination—venuemanageme…
龙霄龙霄
Nuxt.js英文, WordPress英文
1 months ago
0
0
2.84K
0
Nuxt.js Page Builder: One Module, Infinite Websites

Nuxt.js Page Builder: One Module, Infinite Websites

AdeepdiveintothedesignphilosophybehindaNuxt4visualpagebuild…
龙霄龙霄
Nuxt.js英文, Vue.js英文
1 months ago
0
0
2.88K
0
Embracing Modern Web Development: The Nuxt.js + WordPress Architectural Revolution

Embracing Modern Web Development: The Nuxt.js + WordPress Architectural Revolution

一、PerformanceOptimization:ThePerfectBalanceofStaticGenerationand…
龙霄龙霄
Nuxt.js英文, WordPress英文
2 months ago
0
0
2.89K
0
LongXiao Theme Four Little Monsters Guarding the Login: A Deep Dive into Playful Interaction Design

LongXiao Theme Four Little Monsters Guarding the Login: A Deep Dive into Playful Interaction Design

Theypeekwhenyoutypeyourpassword.Theytilttheirheadstofollowy…
龙霄龙霄
Nuxt.js英文, Vue.js英文
1 months ago
0
0
4.48K
0
Nitro Server Architecture: Security Proxy Layer &amp; Performance Engine

Nitro Server Architecture: Security Proxy Layer & Performance Engine

AninvisibleshieldthatcompletelyisolatestheWordPressbackendfrom…
龙霄龙霄
Nuxt.js英文
1 months ago
0
0
2.60K
0
Longxiao Theme-Knowledge Payment Course Plugin: Core Features and Learning Engagement Strategies

Longxiao Theme-Knowledge Payment Course Plugin: Core Features and Learning Engagement Strategies

Intheeraoftheknowledgeeconomy,howdoyoutransformprofessionale…
龙霄龙霄
Nuxt.js英文, WordPress英文
1 months ago
0
0
2.72K
0
Naive UI Integration &amp; UnoCSS Atomic CSS in Nuxt 4

Naive UI Integration & UnoCSS Atomic CSS in Nuxt 4

ApracticalguidetointegratingNaiveUIwithUnoCSSinNuxt4,coverin…
龙霄龙霄
Nuxt.js英文, Vue.js英文
2 months ago
0
0
3.57K
0
评论表单游客 您好,欢迎参与讨论。
Loading...
评论列表
Total: 0
Long Xiao
No relevant content