A simple react-cache implementation that works for both Server Components and Client Components!
npm install @matthamlin/simple-cache@matthamlin/simple-cacheA simple react-cache implementation that works for both Server Components and
Client Components!
``tsx
// For use on the client, or in client components during SSR
import { useCache } from '@matthamlin/simple-cache/client'
let cache = new Map()
interface Result {
something: boolean
}
function useFetch(endpoint) {
return useCache
fetch(endpoint).then((data) => data.json()),
)
}
`
Or with Server Components (experimental):
`tsx
import { useCache } from '@matthamlin/simple-cache/server'
let cache = new Map()
interface Result {
something: boolean
}
function useData(endpoint) {
return useCache
fetch(endpoint).then((res) => res.json()),
)
}
`
- cache - A cache, expects the same api as a Mapkey
- - A unique key (string) to index the cache bymiss
- - A function that returns a promise to suspend (function)
#### Sharing a cache between the server and the client:
> Note
> I haven't done extensive testing with this approach, but it could
> work!
The server entrypoint also exports a serializeCache function that takes incache
the and returns a stringified representation of the cache. It's up to
you to pass that data down to the client, one possible approach is a