File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 104104.tern-port
105105
106106/build
107+ .history
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11import type { SerializeFrom } from "@remix-run/server-runtime" ;
22import {
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+
6881export const cacheClientLoader = async < T extends unknown > (
6982 { request, serverLoader } : ClientLoaderFunctionArgs ,
7083 {
Original file line number Diff line number Diff line change 44 cacheClientLoader ,
55 useCachedLoaderData ,
66} from "~/hook/useCachedLoaderData" ;
7+ import { decacheClientLoader } from "remix-client-cache" ;
78
89export const meta : MetaFunction = ( ) => {
910 return [
@@ -22,6 +23,8 @@ export const clientLoader = async (args: ClientLoaderFunctionArgs) =>
2223 cacheClientLoader ( args , "swr" ) ;
2324clientLoader . hydrate = true ;
2425
26+ export const clientAction = decacheClientLoader ;
27+
2528export default function Index ( ) {
2629 const { user } = useCachedLoaderData < typeof loader > ( ) ;
2730 const navigate = useNavigate ( ) ;
You can’t perform that action at this time.
0 commit comments