Skip to content

Commit

Permalink
Merge pull request #22 from forge42dev/19-feature-request-bypass-havi…
Browse files Browse the repository at this point in the history
…ng-to-type-client-loader

Added createClientLoaderCache utility
  • Loading branch information
AlemTuzlak authored Jan 21, 2025
2 parents 2034be2 + 133739c commit 4b4350e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ clientLoader.hydrate = true;

```

### createClientLoaderCache

Creates everything needed to cache the data via clientLoader, behind the scenes creates the clientLoader object with the correct hydrate flag and the adapter.

```tsx
import { createClientLoaderCache, cacheClientLoader } from "remix-client-cache";

export const clientLoader = createClientLoaderCache();

// above is equivalent to:
export const clientLoader = (args: ClientLoaderFunctionArgs) => cacheClientLoader(args);
clientLoader.hydrate = true;
```

### decacheClientLoader

Used to remove the data that is piped from the loader to your component using the `clientLoader` export.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remix-client-cache",
"version": "2.0.1",
"version": "2.1.0",
"description": "Utility library to cache your client data in React Router",
"type": "module",
"main": "./dist/index.cjs",
Expand Down
15 changes: 14 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ export const decacheClientLoader = async <T,>(
return data;
};

type CacheClientLoaderArgs = {
type?: "swr" | "normal";
key?: string;
adapter?: CacheAdapter;
};

export const cacheClientLoader = async <T,>(
{ request, serverLoader }: ClientLoaderFunctionArgs,
{
type = "swr",
key = constructKey(request),
adapter = cache,
}: { type?: "swr" | "normal"; key?: string; adapter?: CacheAdapter } = {
}: CacheClientLoaderArgs = {
type: "swr",
key: constructKey(request),
adapter: cache,
Expand Down Expand Up @@ -119,6 +125,13 @@ export const cacheClientLoader = async <T,>(
};
};

export const createClientLoaderCache = (props?: CacheClientLoaderArgs) => {
const clientLoader = (args: ClientLoaderFunctionArgs) =>
cacheClientLoader(args, props);
clientLoader.hydrate = true;
return clientLoader;
};

export function useCachedLoaderData<T>(
{ adapter = cache }: { adapter?: CacheAdapter } = { adapter: cache },
) {
Expand Down
3 changes: 2 additions & 1 deletion test-apps/testing-app/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import type { MetaFunction } from "react-router";
import {
cacheClientLoader,
createClientLoaderCache,
decacheClientLoader,
useCachedLoaderData,
} from "remix-client-cache";
Expand All @@ -25,7 +26,7 @@ export const loader = async () => {
return { user: { description: Math.random() } };
};

export const clientLoader = cacheClientLoader;
export const clientLoader = createClientLoaderCache();
clientLoader.hydrate = true;

export const clientAction = decacheClientLoader;
Expand Down

0 comments on commit 4b4350e

Please sign in to comment.