How to configure NextJS to proxy its requests
There may be awkward confusion around the term 'proxy' here.
If all you're needing is having your frontend relative requests (/api/example) redirect to a service that isn't your NextJS server, then you can use the NextJS rewrites configuration documented here.
This post pertains to proxying all requests the Next application might be making (including said rewrites) to a proxy server such as MITM proxy, while having them continue to intended destination.
Situation: I want to record the requests my NextJS application is making.
Where for a straight client-side SPA I could use the network tab of the developer dev tools to generate a HAR file:
For an application that involves SSR and RSCs, it's not quite this simple - as some of those requests may be made on the server side - and be returning the rendered HTML to the browser.
The solution I've opted to investigate is using the tool mitmproxy, we proxy our requests through mitmproxy and and use that to record a HAR file.
There are four kinds of requests we need to handle
I have a sample application set up at this repository here.
The application requests data in various forms from the jsonplaceholder API.
There are four kinds of requests we need to handle.
Straight client side requests
Unauthorized. Is your Github token valid?
This is the most basic, and this doesn't require special NextJS configuration - the request to jsonplaceholder is made directly from the browser, and if our browser is configured to use a proxy those requests will indeed be proxied.
Client side requests with rewrites
Unauthorized. Is your Github token valid?
In this scenario we request the data via a relative path, and we configure NextJS to rewrite the request in the next.config.js file:
Unauthorized. Is your Github token valid?
In this case the browser proxy configuration is not able to proxy these requests - as the requests are made to application server which is doing the requests on the browsers behalf.
Server side requests - with native fetch
Unauthorized. Is your Github token valid?
In this scenario the request is made on the server as a RSC. We need a way to instruct our application to proxy these requests via our proxy.
Server side requests - with node-fetch
Unauthorized. Is your Github token valid?
This is the same scenario but we're using node-fetch instead of native fetch.
Solution - use a secondary proxy
The best solution I've found is to add your own secondary proxy, and have it proxy its traffic through your target proxy.
Create a proxy server with this script:
Unauthorized. Is your Github token valid?
Update your requests, whether they're are server side requests or rewrites to reference an environment variable as the upstream.
Unauthorized. Is your Github token valid?
Start both your secondary proxy and NextJS application with the NODE_EXTRA_CA_CERTS env var provided.
Unauthorized. Is your Github token valid?
Here you can see mitmproxy recording my requests!
Alternative solutions
If the secondary proxy approach doesn't work for you, you can configure the proxying of your http client (eg. fetch or node-fetch) directly. See my post here for details.
Questions? Comments? Criticisms? Get in the comments! 👇
Spotted an error? Edit this page with Github