Skip to content

Commit 68db7c3

Browse files
committed
Added createClientLoaderCache utility
1 parent 2034be2 commit 68db7c3

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ clientLoader.hydrate = true;
244244

245245
```
246246

247+
### createClientLoaderCache
248+
249+
Creates everything needed to cache the data via clientLoader, behind the scenes creates the clientLoader object with the correct hydrate flag and the adapter.
250+
251+
```tsx
252+
import { createClientLoaderCache, cacheClientLoader } from "remix-client-cache";
253+
254+
export const clientLoader = createClientLoaderCache();
255+
256+
// above is equivalent to:
257+
export const clientLoader = (args: ClientLoaderFunctionArgs) => cacheClientLoader(args);
258+
clientLoader.hydrate = true;
259+
```
260+
247261
### decacheClientLoader
248262

249263
Used to remove the data that is piped from the loader to your component using the `clientLoader` export.

src/index.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,19 @@ export const decacheClientLoader = async <T,>(
7979
return data;
8080
};
8181

82+
type CacheClientLoaderArgs = {
83+
type?: "swr" | "normal";
84+
key?: string;
85+
adapter?: CacheAdapter;
86+
};
87+
8288
export const cacheClientLoader = async <T,>(
8389
{ request, serverLoader }: ClientLoaderFunctionArgs,
8490
{
8591
type = "swr",
8692
key = constructKey(request),
8793
adapter = cache,
88-
}: { type?: "swr" | "normal"; key?: string; adapter?: CacheAdapter } = {
94+
}: CacheClientLoaderArgs = {
8995
type: "swr",
9096
key: constructKey(request),
9197
adapter: cache,
@@ -119,6 +125,13 @@ export const cacheClientLoader = async <T,>(
119125
};
120126
};
121127

128+
export const createClientLoaderCache = (props?: CacheClientLoaderArgs) => {
129+
const clientLoader = (args: ClientLoaderFunctionArgs) =>
130+
cacheClientLoader(args, props);
131+
clientLoader.hydrate = true;
132+
return clientLoader;
133+
};
134+
122135
export function useCachedLoaderData<T>(
123136
{ adapter = cache }: { adapter?: CacheAdapter } = { adapter: cache },
124137
) {

test-apps/testing-app/app/routes/_index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import type { MetaFunction } from "react-router";
88
import {
99
cacheClientLoader,
10+
createClientLoaderCache,
1011
decacheClientLoader,
1112
useCachedLoaderData,
1213
} from "remix-client-cache";
@@ -25,7 +26,7 @@ export const loader = async () => {
2526
return { user: { description: Math.random() } };
2627
};
2728

28-
export const clientLoader = cacheClientLoader;
29+
export const clientLoader = createClientLoaderCache();
2930
clientLoader.hydrate = true;
3031

3132
export const clientAction = decacheClientLoader;

0 commit comments

Comments
 (0)