There is isomorphic core/fetch
module that can be used the same way in both
client-side and server-side code as follows:
import fetch from '../core/fetch';
export const path = '/products';
export const action = async () => {
const response = await fetch('/graphql?query={products{id,name}}');
const data = await response.json();
return <Layout><Products {...data} /></Layout>;
};
When this code executes on the client, the Ajax request will be sent via
GitHub's fetch library (whatwg-fetch
),
that itself uses XHMLHttpRequest behind the scene unless fetch
is supported
natively by the user's browser.
Whenever the same code executes on the server, it uses
node-fetch module behind the scene that
itself sends an HTTP request via Node.js http
module. It also converts
relative URLs to absolute (see ./core/fetch/fetch.server.js
).
Both whatwg-fetch
and node-fetch
modules have almost identical API. If
you're new to this API, the following article may give you a good introduction: