Skip to Content

SWR

The key is query.url(), the fetcher calls .get():

import from "swr"; const = .("blog").("-date").(10).(["title", "slug", "date"]); export function () { return (.(), () => .()); } const {
const data: Paginated<Pick<BlogEntry, "date" | "slug" | "title">> | undefined

The data returned by the fetcher function.

  • When suspense is enabled or fallbackData is provided: always defined
  • Otherwise: undefined during initial load or when key is falsy
data
} = ();

SWR does not pass an AbortSignal to the fetcher, so outdated requests are not cancelled.

Conditional fetching

Pass null as the key to skip the request:

import from "swr"; const = ? .("blog").() : null; const { } = (?.() ?? null, () => !.());

Pagination

With useSWRInfinite, the key is [base URL, page] and the fetcher reads the page from it:

import from "swr/infinite"; const = .("blog").("-date").(10); type = <<typeof .>>; const { , , } = ( (, : | null) => && .. >= (.. ?? 0) ? null : [.(), + 1], ([, ]: [string, number]) => .().(), );

Retries

import { } from "@headquery/statamic"; export const : = { : (, , , , { }) => { if ( instanceof && . < 500) return; if ( < 3) (() => ({ }), 2000); }, };

Server-side data

Fetch on the server and hand the result to SWR as fallback. Because the key is the URL, the server and the client agree on it without extra mapping:

const = .("blog").("-date").(10); // on the server const = { [.()]: await .() }; // then render: <SWRConfig value={{ fallback }}>…</SWRConfig>
Last updated on