#Server-Side Global State
6 messages · Page 1 of 1 (latest)
If you want a simple, mutable global object that only exists on the server side (for example, to manage ephemeral game lobbies) and persists across Vite HMR, you can define a module-scoped variable in a server-only file (such as inside server/api/ or server/middleware/). This variable will persist as long as the server process is running, including across Vite HMR reloads, because server files are not reloaded in the same way as client code during HMR.
Here's a minimal example for Nuxt 3:
// server/api/lobbies.ts
// This object will persist across requests and HMR (as long as the server process is alive)
const lobbies: Record<string, { players: string[] }> = {}
export default defineEventHandler((event) => {
// Example: Add a lobby
if (event.method === 'POST') {
const id = Math.random().toString(36).slice(2)
lobbies[id] = { players: [] }
return { id }
}
// Example: List lobbies
return lobbies
})
This approach is simple and does not require Pinia or Vue integration. However, keep in mind:
- This state is not shared between multiple server instances (e.g., in production with load balancing).
- The state will be lost if the server process restarts.
This pattern is commonly used for ephemeral, server-only state in Nuxt 3 API routes or middleware and is supported by Nuxt's server engine Nitro. There is no explicit documentation in the knowledge sources about this exact pattern, but it is a standard Node.js/JavaScript approach and aligns with how Nuxt server files work.
If you need more advanced features (like persistence or cross-instance sharing), you would need to use a database or external store. For your described use case, this simple approach should suffice.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@brisk forge Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@pulsar ridge that still gets cleared when I modify any files in the server/ directory
Let me check the knowledge base for you again. One moment! :books:
You're correct: when you modify files in the server/ directory, Nuxt's development server (powered by Vite or Webpack) will reload the server process, which clears any in-memory state—including module-scoped variables. This is the expected behavior for hot module replacement (HMR) in the server context.
The knowledge sources do not provide a built-in way to persist purely in-memory server state across server restarts or HMR reloads. For truly persistent or cross-restart state, you would need to use an external storage solution (like a database, Redis, or file-based storage), as suggested in the Nuxt server storage documentation. Nitro, Nuxt's server engine, supports mounting storage backends such as Redis for this purpose:
export default defineNuxtConfig({
nitro: {
storage: {
redis: {
driver: 'redis',
port: 6379,
host: "127.0.0.1",
// ...other options
}
}
}
})
You can then use useStorage('redis') in your server API handlers to read/write state that survives server restarts and HMR reloads Advanced Server Usage.
In summary:
- In-memory state in server files will be lost on HMR or server restarts.
- To persist state across reloads, use an external storage backend (e.g., Redis, file, or database) via Nitro's storage layer.