Documentation
Getting Started
Functions
Guides
Concepts
Client Query
How to fetch on client with cached dataTo fetch cached data in Client Components, use the clientQuery
function.
When fetching cached data in Client Components, the following sequence occurs:
Step | Environment | Description |
---|---|---|
Pre | Browser | Sends a request to the server for the website at https://example.com . |
Pre | Server | Sends the initial HTML response to the browser. |
1 | Browser | Receives the response, hydrates the page and sends a request to the server for the data. |
2 | Server | Finds the cached response for the relevant API query and sends it to the browser. |
3 | Browser | Receives the response and displays the data. |
Post | Server | After the response is sent, the server checks if the data is stale. If it is, the server fetches the fresh data from the API for the next request. |
The following example fetches data on the client side using a GET request.
To cache the response on the server, set queryCache
to true or pass an object with custom cache options.
"use client";
import { clientQuery } from "@bigbang-sdk/next-query";
function ClientComponent() {
const { data, error, isLoading } = clientQuery({
queryUrl: "https://api.example.com/data",
queryCache: true
});
console.log(data, error, isLoading);
}
Made with
by Bigbang