Fetcher
Fetch data from the server.
Usage
Server-side only
The fetcher()
function is only server-side. You cannot use it on the
client-side.
Initial client
fetcher()
is a function to create client. Pass generic type with the response type and pass the plugin
and module
as arguments.
As an example, we will pass UsersTypes
as a generic type.
Options
You can pass the options
object to modify the fetch
function. For example we can pass cache
option to enable the cache.
Server Functions
Methods like POST
, PUT
and DELETE
require to use Server Functions. You can create mutation-api.ts
file and use it to call the fetcher()
function with FetcherInput
type.
Now you can call the mutationApi()
function on the server-side.
Handling errors
You can handle errors by checking the status
property of the response.
Handling set-cookies
React Server Components cannot handle set-cookies
headers. You need to handle them by using the handleCookiesFetcher()
function.
Client-side
If you want to use the fetcher()
on the client-side, you need to use the fetcherClient()
function.
Custom fetcher
If you want you can create your own fetch
function, but you need to remember to pass headers like:
x-forwarded-for
header - client IP address,Cookie
header - client cookies,user-agent
header - client user agent.