user3472360
user3472360

Reputation: 2085

Where is the cache that SWR maintains, client or server-side?

I am working on a Next JS project that uses SWR and was wondering where SWR's cache gets stored, is it on the client or in the server?

I am asking because if its on the client and so the cache is different for each user then i can make user-specific requests without worrying that the cache will show a user some other user's data

Upvotes: 4

Views: 4905

Answers (1)

Lin Du
Lin Du

Reputation: 102487

From the docs cache:

By default, SWR uses a global cache to store and share data across all components. Now, there's a new way to customize it with your own cache provider.

SWR use Map data type as the default cache provider

You can create your cache provider using JavaScript Map data type like const provider = new Map().

Theoretically, you can use any data type, state management library like redux, web storage, cookies, IndexDB as your cache provider.

The cache provider must match the following definition:

interface Cache<Data = any> {
  get(key: string): Data | null | undefined
  set(key: string, value: Data): void
  delete(key: string): void
}

In performance considerations, client-side memory cache, web storage is very fast to read and write, so it is better to use client-side cache.

Server-side caching generally uses distributed caching technologies such as Redis.

Upvotes: 5

Related Questions