Skip to content

Commit 8d3b652

Browse files
authored
Merge pull request #10 from forge42dev/5-cacheclientloader-fails-to-follow-redirect-from-loader-with-populated-cache
Follow redirects when swr cache gets a redirect
2 parents 8afa101 + 6d29d3c commit 8d3b652

4 files changed

Lines changed: 31 additions & 12 deletions

File tree

src/index.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ClientActionFunctionArgs,
55
ClientLoaderFunctionArgs,
66
useLoaderData,
7+
useNavigate,
78
} from "@remix-run/react";
89
import { useEffect, useState } from "react";
910
import React from "react";
@@ -122,6 +123,7 @@ export function useCachedLoaderData<T extends any>(
122123
{ adapter = cache }: { adapter?: CacheAdapter } = { adapter: cache },
123124
) {
124125
const loaderData = useLoaderData<any>();
126+
const navigate = useNavigate();
125127
const [freshData, setFreshData] = useState<any>({
126128
...("serverData" in loaderData ? loaderData.serverData : loaderData),
127129
});
@@ -130,12 +132,22 @@ export function useCachedLoaderData<T extends any>(
130132
useEffect(() => {
131133
let isMounted = true;
132134
if (loaderData.deferredServerData) {
133-
loaderData.deferredServerData.then((newData: any) => {
134-
if (isMounted) {
135-
adapter.setItem(loaderData.key, newData);
136-
setFreshData(newData);
137-
}
138-
});
135+
loaderData.deferredServerData
136+
.then((newData: any) => {
137+
if (isMounted) {
138+
adapter.setItem(loaderData.key, newData);
139+
setFreshData(newData);
140+
}
141+
})
142+
.catch((e: any) => {
143+
const res = e instanceof Response ? e : undefined;
144+
if (res && res.status === 302) {
145+
const to = res.headers.get("Location");
146+
to && navigate(to);
147+
} else {
148+
throw e;
149+
}
150+
});
139151
}
140152
return () => {
141153
isMounted = false;

src/testing-app/app/root.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export default function App() {
2424
<Outlet />
2525
<ScrollRestoration />
2626
<Scripts />
27-
<LiveReload />
2827
</body>
2928
</html>
3029
);

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import type { MetaFunction } from "@remix-run/node";
2-
import { ClientLoaderFunctionArgs, json, useNavigate } from "@remix-run/react";
32
import {
3+
ClientLoaderFunctionArgs,
4+
json,
5+
redirect,
6+
useNavigate,
7+
} from "@remix-run/react";
8+
9+
import {
10+
decacheClientLoader,
411
cacheClientLoader,
512
useCachedLoaderData,
6-
} from "~/hook/useCachedLoaderData";
7-
import { decacheClientLoader } from "remix-client-cache";
13+
} from "remix-client-cache";
814

915
export const meta: MetaFunction = () => {
1016
return [
@@ -16,11 +22,11 @@ export const meta: MetaFunction = () => {
1622
export const loader = async () => {
1723
const response = await fetch("https://jsonplaceholder.typicode.com/users/2");
1824
const user = await response.json();
25+
// return redirect("/user/2");
1926
return json({ user: { ...user, description: Math.random() } });
2027
};
2128

22-
export const clientLoader = async (args: ClientLoaderFunctionArgs) =>
23-
cacheClientLoader(args, "swr");
29+
export const clientLoader = cacheClientLoader;
2430
clientLoader.hydrate = true;
2531

2632
export const clientAction = decacheClientLoader;

src/testing-app/app/routes/user.$user.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { json, type LoaderFunctionArgs } from "@remix-run/node";
22
import {
33
ClientLoaderFunctionArgs,
4+
Link,
45
useLoaderData,
56
useNavigate,
67
} from "@remix-run/react";
@@ -42,6 +43,7 @@ export default function Index() {
4243

4344
return (
4445
<div>
46+
<Link to="/">Home</Link>
4547
<SWR>
4648
{(data) => {
4749
return (

0 commit comments

Comments
 (0)