LocalStorage Based persistent cache with Nextjs #2091
-
Hello! I have a little problem. localStorage based cache provider not work with server side (Next) because How can I work around this efficiently? I'm creating an application with nextjs and next-pwa that offers some offline features, and I need to retrieve cache for SWR from localStorage, therefore, the way described in the documentation is not enough I've tried several times and in different ways but without success. I'm glad if someone help me inject the cache in a client-side way 😄 package versions:{
"swr": "^1.2.2",
"next": "12.1.4",
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
it was so much simpler to solve than I imagined simply on the server side I returned an empty Map instance, and on the client side a Map based on localstorage for the provider (using typeof window !== 'undefined') kkkkkk I haven't been able to do this before because I figured delivering an empty instance without the server side cache would cause an empty delivery to the client too, obviously is wrong kkkk |
Beta Was this translation helpful? Give feedback.
-
This is how I solved this:
|
Beta Was this translation helpful? Give feedback.
it was so much simpler to solve than I imagined
simply on the server side I returned an empty Map instance, and on the client side a Map based on localstorage for the provider (using typeof window !== 'undefined') kkkkkk
I haven't been able to do this before because I figured delivering an empty instance without the server side cache would cause an empty delivery to the client too, obviously is wrong kkkk