Modernjs's BFF is isomorphic in CSR and SSR.In the browser side rely on the Fetch API,On the server relies on the node-fetch.However, in many business scenarios we need to do some additional processing of the request or response, for example:
For the above scenario, Modern.js provides the configure
function,the customization capabilities range from low to high and can be used to configure ssr pass-through request headers, interceptors, and request SDKs..
The configure
function call needs to be called before all BFF requests are sent, to ensure that the default request configuration is overridden.
In scenarios where both Modern.js SSR and BFF are used, it is often necessary to pass some request headers on SSR page requests to the BFF service.
For example, the project has a page address https://website.com
, which is an SSR page, and the API interface https://website.com/api/info
will be called in the component, which requires the user's cookie information for authentication. When the page requests this API interface, it needs to pass the cookie
requested by the SSR page to BFF.
Currently the following request headers are automatically passed through in Modern.js:
The request header can be configured via configure
. For example, in the following example, Modern.js will automatically pass the cookie information of the SSR page request to the BFF service:
In certain business scenarios, there is a requirement for unified processing of requests and responses, and interceptors can be configured to fulfill these requirements in such scenarios.
If the requirements cannot be met by configuring interceptors alone and need to further customize the request SDK, you can configure the custom request SDK by using the configure
function:
Send a request to the BFF service when the server side renders, Modern.js will find the BFF service intranet IP via service discovery and send requests via IP to improve performance. This optimization is lost if a custom request SDK is used.
The configuration custom request SDK has the following conventions:
configure
function allows you to configure a request
function whose input is the same as the Fetch or node-fetch in the browser, and all BFF functions will send requests through this functionrequest
function must be the actual data returned by the interface, not a Promise, otherwise the BFF function will not return data properly.request
must support both browser-side and server-side sending of requests.Example of custom request SDK using axios: