Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 764
user3472360 Avatar asked Oct 18 '25 08:10

user3472360


1 Answers

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.

like image 55
slideshowp2 Avatar answered Oct 22 '25 01:10

slideshowp2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!