React Cache Function Explained
Mar 15, 2024
0
React Cache API which is right now in canary version, but going to introduce in React 19 mostly likely. It's help to cache the result of a data fetch or computation. But we can only use cache API in React Server Components. Since next.js already support React Server Components, so we can use it in next.js.
Unlike React Query, React cache API is not a library, it's a built-in feature of React. It's a low-level API that allows you to cache the result of a data fetch or computation. It's not a replacement for React Query as It's only work with React Server Components.
It's also very different from other React useMemo
or useCallback
hooks. It's not a hook, it's a API that you can call anywhere in your server component. It's also not a replacement for useMemo
or useCallback
hooks.
Key Features of React Cache API
cache
only can use with React Server Components.- React will invalidate the cache for all memoized functions for each server request.
- It can cache all type of data, like if here
cache(fn)
infn
return error, then it will cache the error as well.
Usage of Cache API#
In the above example, we are using cache
API to cache the result of getPost
function. So, when we call getSinglePost
function, it will return the cached result if it's already cached.
How to efficiently use Cache API#
There are few things that you should keep in mind while using cache function. Always consider to keep cache function
in a separate file, so that you can import it anywhere and use same cache all over the places.
Keeping cache
in seperate file only needed if you want to use the same cache
all over the places. It recommended to keep it in a separate file, so that you
get good performance improvement over cache hit.
In the above example, we are using cache
in a separate file, so that we can import it anywhere and use so the all places will use the same cache.
Nginx as Reverse Proxy for Kubernetes Services
Nginx is a popular open-source web server and reverse proxy server. We can use Nginx as a reverse proxy for Kubernetes services.
Read Full PostPreload data with Cache API#
The best part of cache is that you can preload the data as well. You can use cache
to preload the data. This is helpful when you want to preload the data before the component is rendered.
For example we want only fetch user posts when user data fetched, so we can use cache
to preload the user posts.
In the above example, we are using cache
to preload the user posts. So, when the User
component is rendered, the user posts will be already fetched.
This help to improve the performance of the application, as the user posts will be already fetched before the component is rendered.
Calling a memoized function outside of a component will not use the cache. The cache is only used when the memoized function is called inside a component.
Conclusion#
cache
API is useful when we want to memoize work that can be shared across React Server Components. So that we can improve the performance of the application by caching the result or we can preload the data before the component is rendered.
I hope you find this article helpful. If you have any question, feel free to ask in the comment section below. Happy coding!
Comments (5)