Skip to content

Commit 8afa101

Browse files
authored
Merge pull request #9 from forge42dev/4-invalidate-normal-cache-on-action
4 invalidate normal cache on action
2 parents 88c8c26 + fbcf38f commit 8afa101

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@ dist
104104
.tern-port
105105

106106
/build
107+
.history

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,36 @@ clientLoader.hydrate = true;
237237

238238
```
239239

240+
### decacheClientLoader
241+
242+
Used to remove the data that is piped from the loader to your component using the `clientLoader` export.
243+
244+
```tsx
245+
import { json, type LoaderFunctionArgs } from "@remix-run/node";
246+
import { ClientLoaderFunctionArgs } from "@remix-run/react";
247+
import { decacheClientLoader, useCachedLoaderData } from "remix-client-cache";
248+
249+
export const loader = async ({ params }: LoaderFunctionArgs) => {
250+
const response = await fetch(
251+
`https://jsonplaceholder.typicode.com/users/${params.user}`
252+
);
253+
const user = await response.json();
254+
await new Promise((resolve) => setTimeout(resolve, 1000));
255+
return json({ user: { ...user, description: Math.random() } });
256+
};
257+
// The data is cached here
258+
export const clientLoader = (args: ClientLoaderFunctionArgs) => cacheClientLoader;
259+
clientLoader.hydrate = true;
260+
// It is de-cached after a successful action submission via the clientAction export
261+
export const clientAction = decacheClientLoader;
262+
263+
```
264+
265+
Accepts an optional object with the following properties:
266+
- `key` - key that is used to store the data in the cache.
267+
- `adapter` - the cache adapter that is used to store the data.
268+
269+
240270

241271
### useCachedLoaderData
242272

src/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SerializeFrom } from "@remix-run/server-runtime";
22
import {
33
Await,
4+
ClientActionFunctionArgs,
45
ClientLoaderFunctionArgs,
56
useLoaderData,
67
} from "@remix-run/react";
@@ -65,6 +66,18 @@ export const configureGlobalCache = (
6566
}
6667
};
6768

69+
export const decacheClientLoader = async <T extends unknown>(
70+
{ request, serverAction }: ClientActionFunctionArgs,
71+
{
72+
key = constructKey(request),
73+
adapter = cache,
74+
}: { key?: string; adapter?: CacheAdapter },
75+
) => {
76+
const data = await serverAction<T>();
77+
await adapter.removeItem(key);
78+
return data;
79+
};
80+
6881
export const cacheClientLoader = async <T extends unknown>(
6982
{ request, serverLoader }: ClientLoaderFunctionArgs,
7083
{

src/testing-app/app/routes/_index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
cacheClientLoader,
55
useCachedLoaderData,
66
} from "~/hook/useCachedLoaderData";
7+
import { decacheClientLoader } from "remix-client-cache";
78

89
export const meta: MetaFunction = () => {
910
return [
@@ -22,6 +23,8 @@ export const clientLoader = async (args: ClientLoaderFunctionArgs) =>
2223
cacheClientLoader(args, "swr");
2324
clientLoader.hydrate = true;
2425

26+
export const clientAction = decacheClientLoader;
27+
2528
export default function Index() {
2629
const { user } = useCachedLoaderData<typeof loader>();
2730
const navigate = useNavigate();

0 commit comments

Comments
 (0)