ProxymanApp / Proxyman

Modern. Native. Delightful Web Debugging Proxy for macOS, iOS, and Android ⚡️
https://proxyman.io
5.34k stars 177 forks source link

Proxyman automatic setup does not capturing network requests from react server components in next.js #2038

Closed tonyxiao closed 4 weeks ago

tonyxiao commented 1 month ago

This simple example unfortunately doesn't work :(

app/debug/page.tsx

export default async function MyPage() {
  const res = await fetch('https://httpbin.org/anything')
  return <div>My Page{res.text()}</div>
}

EDIT: Problem is request is being cached by next.js Solution is to use no-cache with fetch. See https://github.com/ProxymanApp/Proxyman/issues/2038#issuecomment-2143324535

NghiaTranUIT commented 1 month ago

@tonyxiao It's expected behavior because fetch from NodeJS doesn't respect the system HTTP Proxy.

To capture the fetch, you should use the Automatic Setup: https://docs.proxyman.io/automatic-setup/automatic-setup

It will open the pre-configured Terminal -> Start your NodeJS server here -> Proxyman will automatically capture all NodeJS request/response 👍

tonyxiao commented 1 month ago

I used the automatic set up and it still didn't work.

tonyxiao commented 1 month ago

That was in the title of the issue actually

tonyxiao commented 1 month ago

To be sure it works for if I just run a node script, the problem is when I try to run next.js it doesn't work

NghiaTranUIT commented 1 month ago

Ah, it's a special case: from react server components in next.js

My initial is a fetch from NodeJS server. Let me see

NghiaTranUIT commented 1 month ago

I guess it's because fetch from NextJS is a custom version, and it ignores all system Proxy.

You should manually add the Proxy to the fetch and trust the self-signed certificate. Here is the answer: https://stackoverflow.com/questions/72306101/make-a-request-in-native-fetch-with-proxy-in-nodejs-18


Proxyman can't capture it automatically because it's how NextJS works.

tonyxiao commented 1 month ago

I see. What is the underlying networking lib next js uses? And wouldn't proxyman hook into that?

Separately would you be able to create an example of how to use proxyman with next.js? Feels like it should be in the docs somewhere too.

tonyxiao commented 4 weeks ago

@NghiaTranUIT following up here. Would love to have an example that works with next.js and some better understanding why it doesn't work out of the box.

NghiaTranUIT commented 4 weeks ago

Sure, I will do it this weekend 👍

NghiaTranUIT commented 4 weeks ago

@tonyxiao turn out Proxyman can capture the fetch from Server Side Component as soon as you don't use the caching.

  1. Start the Pre-configure Terminal on Tools -> Setup -> Automatic Setup
  2. Use no-cache on fetch
export default async function MyPage() {
  const res = await fetch('https://httpbin.org/anything', { cache: 'no-cache' }) // no cache, or no request is sent to Proxyman
  return <div>My Page{res.text()}</div>
}
  1. Start your NextJS server on this terminal
  2. Done ✅

Screenshot 2024-06-01 at 1 29 50 PM

tonyxiao commented 4 weeks ago

Interesting , I will give that a try. Strange why is that tho, as my request should hit a cache miss as it runs the first time, which apparently is not the same as "no-cache"?

tonyxiao commented 4 weeks ago

Hmm just tested the first request does seem to be captured - it's only later requests that are cached that seems to be not captured.