Skip to content

Commit

Permalink
Merge pull request #192 from penumbra-zone/landing-caching-fix
Browse files Browse the repository at this point in the history
FIX: landing page caching stale fetch data
  • Loading branch information
ejmg authored Sep 12, 2024
2 parents 06e7e6c + 5687443 commit 1268001
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions apps/web/src/app/(index)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export default function Home() {

Promise.all([
queryClient.prefetchQuery({
queryFn: () => getTransactions({ endpoint: transactionEndpoint, pageIndex: 0 }),
queryFn: () => getTransactions({ endpoint: transactionEndpoint, pageIndex: 0, cached: false }),
queryKey: [transactionsQuery, 0],
staleTime: 0,
meta: {
errorMessage,
},
}),
queryClient.prefetchQuery({
queryFn: () => getBlocks({ endpoint: blocksEndpoint, pageIndex: 0 }),
queryFn: () => getBlocks({ endpoint: blocksEndpoint, pageIndex: 0, cached: false }),
queryKey: [blocksQuery, 0],
staleTime: 0,
meta: {
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/BlocksTable/getBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getBaseURL } from "@/lib/utils";
import { BlocksTableQuery } from "@/lib/validators/table";

export async function getBlocks ({ endpoint, pageIndex } : ({ endpoint: string, pageIndex: number })) {
export async function getBlocks ({ endpoint, pageIndex, cached = true } : ({ endpoint: string, pageIndex: number, cached?: boolean })) {
const baseUrl = getBaseURL();
console.log(`Fetching: GET ${baseUrl}/${endpoint}/?page=${pageIndex}`);
const res = await fetch(`${baseUrl}/${endpoint}?page=${pageIndex}`, { method: "GET" });

const res = await fetch(`${baseUrl}/${endpoint}?page=${pageIndex}`, { method: "GET", cache: ( cached ? "default" : "no-store") });
const json = await res.json();

console.log("Fetched result:", json);
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/PreviewTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ export function PreviewTable ({
}
} = useSuspenseQuery({
queryKey: [queryName, pageIndex],
queryFn: () => getFn({ endpoint, pageIndex }),
queryFn: () => getFn({ endpoint, pageIndex, cached: false }),
// staleTime: 0,
// refresh preview data every 15 seconds
refetchInterval: 15 * 1000,
refetchInterval: 10 * 1000,
meta: {
errorMessage,
},
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/TransactionsTable/getTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getBaseURL } from "@/lib/utils";
import { TransactionsTableData } from "@/lib/validators/table";

export async function getTransactions({ endpoint, pageIndex } : { endpoint: string, pageIndex: number}) {
export async function getTransactions({ endpoint, pageIndex, cached = true } : { endpoint: string, pageIndex: number, cached?: boolean}) {
const baseUrl = getBaseURL();
console.log(`Fetching: POST ${baseUrl}/${endpoint}?page=${pageIndex}`);
const res = await fetch(`${baseUrl}/${endpoint}?page=${pageIndex}`, { method: "GET" });
const res = await fetch(`${baseUrl}/${endpoint}?page=${pageIndex}`, { method: "GET", cache: ( cached ? "default" : "no-store") });
const json = await res.json();
console.log("Fetched Result:", json);
const result = TransactionsTableData.safeParse(json);
Expand Down

0 comments on commit 1268001

Please sign in to comment.