Skip to content

Does this library support Next 13 app dir ? #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wangwailok opened this issue Jun 16, 2023 · 27 comments
Open

Does this library support Next 13 app dir ? #83

wangwailok opened this issue Jun 16, 2023 · 27 comments

Comments

@wangwailok
Copy link

The type requests in the new route system changed from NextApiequest to NextRequest
and the middleware seems cannot work accordingly. any workaround?

@stegano
Copy link
Owner

stegano commented Jun 18, 2023

Hi @wangwailok
I'm checked a little while ago, and this llibrary is still working well,
Could you please share with me the versions of the library you are using and the source code?

스크린샷 2023-06-18 오후 12 43 09

Please let me know if I'm misunderstanding.

@stegano
Copy link
Owner

stegano commented Jun 18, 2023

It seems difficult to see this library as a kind of 'middleware' that is currently called by Next.js.

This library has been used since Next.js v9, and at that time, libraries that handle APIs were called middleware. Currently, Next.js calls them API Routes.

@wangwailok
Copy link
Author

Hi @wangwailok I'm checked a little while ago, and this llibrary is still working well, Could you please share with me the versions of the library you are using and the source code?

스크린샷 2023-06-18 오후 12 43 09

Please let me know if I'm misunderstanding.

I am using next js 13 with App dir, the type of request object has been changed to "NextRequest" and cannot pass inot the proxy middleware anymore

image

@stegano
Copy link
Owner

stegano commented Jul 1, 2023

@wangwailok
In your case, it seems like you should use NextApiRequest and NextApiResponse. 😅

Because, this library is a middleware(handler) for handling API requests, and NextRequest and NextResponse are middlewares(handlers) for handling requests for Page Requests.

Please see links below

Please let me know if i'm missing something.

@pluce
Copy link

pluce commented Oct 5, 2023

Hi I'm facing the same kind of problems were httpProxyMiddleware doesn't work any more with new Next 13 API routing.

Here I'm defining the route in app/api/rem/route.ts:

Capture d’écran 2023-10-05 à 18 00 47

And I've got multiple errors like:

Error: No response is returned from route handler '...../src/app/api/rem/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.

or

TypeError: Cannot set property url of #<NextRequest> which has only a getter

@stegano
Copy link
Owner

stegano commented Oct 8, 2023

Hi, @pluce
Please return the result of executing the httpProxyMiddleware function.

// e.g.,
...
export async function POS(...) {
   return httpProxyMiddleware(... {}); // <- This function must return
}

@tigading
Copy link

tigading commented Oct 10, 2023

Plzz help me!! Error: Cannot set property body of # which has only a getter

@stoompa
Copy link

stoompa commented Nov 3, 2023

Having same issue as @tigading and @pluce, even when returning the function

@stegano
Copy link
Owner

stegano commented Nov 4, 2023

Hi, @stoompa

Could you please share the your project repository?

@stoompa
Copy link

stoompa commented Nov 6, 2023

Hi, @stoompa

Could you please share the your project repository?

I cannot show the whole code since it is enterprise, but this is how we are using this library:

export async function GET(req: NextApiRequest, res: NextApiResponse) {
  const onProxyInit = await getOnProxyInit(req);

  const proxyOptions: NextHttpProxyMiddlewareOptions = {
    onProxyInit,
    changeOrigin: true,
    target: REPORTS_URL,
    pathRewrite: [{ patternStr: '/api/reports', replaceStr: '' }]
  };

  await httpProxyMiddleware(req, res, proxyOptions);
}

@stegano
Copy link
Owner

stegano commented Nov 6, 2023

Hi, @stoompa
Could you please share the your project repository?

I cannot show the whole code since it is enterprise, but this is how we are using this library:

export async function GET(req: NextApiRequest, res: NextApiResponse) {
  const onProxyInit = await getOnProxyInit(req);

  const proxyOptions: NextHttpProxyMiddlewareOptions = {
    onProxyInit,
    changeOrigin: true,
    target: REPORTS_URL,
    pathRewrite: [{ patternStr: '/api/reports', replaceStr: '' }]
  };

  await httpProxyMiddleware(req, res, proxyOptions);
}

You must return the result of the httpProxyMiddleware function execution. Please try again with the code below.

export async function GET(req: NextApiRequest, res: NextApiResponse) {
  const onProxyInit = await getOnProxyInit(req);

  const proxyOptions: NextHttpProxyMiddlewareOptions = {
    onProxyInit,
    changeOrigin: true,
    target: REPORTS_URL,
    pathRewrite: [{ patternStr: '/api/reports', replaceStr: '' }]
  };

  return await httpProxyMiddleware(req, res, proxyOptions);
}
...

@stoompa
Copy link

stoompa commented Nov 6, 2023

Hi, @stoompa
Could you please share the your project repository?

I cannot show the whole code since it is enterprise, but this is how we are using this library:

export async function GET(req: NextApiRequest, res: NextApiResponse) {
  const onProxyInit = await getOnProxyInit(req);

  const proxyOptions: NextHttpProxyMiddlewareOptions = {
    onProxyInit,
    changeOrigin: true,
    target: REPORTS_URL,
    pathRewrite: [{ patternStr: '/api/reports', replaceStr: '' }]
  };

  await httpProxyMiddleware(req, res, proxyOptions);
}

You must return the result of the httpProxyMiddleware function execution. Please try again with the code below.

export async function GET(req: NextApiRequest, res: NextApiResponse) {
  const onProxyInit = await getOnProxyInit(req);

  const proxyOptions: NextHttpProxyMiddlewareOptions = {
    onProxyInit,
    changeOrigin: true,
    target: REPORTS_URL,
    pathRewrite: [{ patternStr: '/api/reports', replaceStr: '' }]
  };

  return await httpProxyMiddleware(req, res, proxyOptions);
}
...

Still same problem :(
Screenshot 2023-11-06 at 13 15 58

@stegano
Copy link
Owner

stegano commented Nov 7, 2023

@stoompa
Would you like to give it a try again with the code below?

...
export const config = {
  api: {
    // Enable `externalResolver` option in Next.js
    externalResolver: true,
  },
}
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
    const onProxyInit = await getOnProxyInit(req);
    const proxyOptions: NextHttpProxyMiddlewareOptions = {
      onProxyInit,
      changeOrigin: true,
      target: REPORTS_URL,
      pathRewrite: [{ patternStr: '/api/reports', replaceStr: '' }]
    };
  
    return await httpProxyMiddleware(req, res, proxyOptions);
};

export default handler;
...

If that doesn't work, please let me known the next.js version so I can check further.

@stoompa
Copy link

stoompa commented Nov 7, 2023

@stegano thanks for the help, unfortunately it does not work. exporting the handler as default only works with the pages directory, and I think we all have the same issue when using the /app directory.
Currently I am using Next.js 14.

@stegano
Copy link
Owner

stegano commented Nov 7, 2023

@stegano thanks for the help, unfortunately it does not work. exporting the handler as default only works with the pages directory, and I think we all have the same issue when using the /app directory. Currently I am using Next.js 14.

You're right. This library might not work properly with Next.js App Directory. It is indeed an older library, and Next.js has introduced the 'rewrites' feature which could potentially replace the functionality you're seeking from this library. Have you tried using the 'rewrites' feature yet? If not, it's worth looking into. If you have chosen not to use 'rewrites', could you provide some insight into your decision?

@arjun-mavonic
Copy link

@stegano thanks for the help, unfortunately it does not work. exporting the handler as default only works with the pages directory, and I think we all have the same issue when using the /app directory. Currently I am using Next.js 14.

You're right. This library might not work properly with Next.js App Directory. It is indeed an older library, and Next.js has introduced the 'rewrites' feature which could potentially replace the functionality you're seeking from this library. Have you tried using the 'rewrites' feature yet? If not, it's worth looking into. If you have chosen not to use 'rewrites', could you provide some insight into your decision?

I have checked out rewrites feature. But how can do something before rewrite happens? It only shows how I can rewrite a url to another. I am loosing what I can do with the response I receive from proxy middleware.

@stegano
Copy link
Owner

stegano commented Apr 3, 2024

@stegano thanks for the help, unfortunately it does not work. exporting the handler as default only works with the pages directory, and I think we all have the same issue when using the /app directory. Currently I am using Next.js 14.

You're right. This library might not work properly with Next.js App Directory. It is indeed an older library, and Next.js has introduced the 'rewrites' feature which could potentially replace the functionality you're seeking from this library. Have you tried using the 'rewrites' feature yet? If not, it's worth looking into. If you have chosen not to use 'rewrites', could you provide some insight into your decision?

I have checked out rewrites feature. But how can do something before rewrite happens? It only shows how I can rewrite a url to another. I am loosing what I can do with the response I receive from proxy middleware.

Sorry, I'm late with my reply.

I'm not sure what you're trying to do
however, If additional tasks beyond simple proxying (changing paths) are required in the middleware, it may be better to establish a separate web application server.

@Cinea4678
Copy link

@stegano thanks for the help, unfortunately it does not work. exporting the handler as default only works with the pages directory, and I think we all have the same issue when using the /app directory. Currently I am using Next.js 14.

You're right. This library might not work properly with Next.js App Directory. It is indeed an older library, and Next.js has introduced the 'rewrites' feature which could potentially replace the functionality you're seeking from this library. Have you tried using the 'rewrites' feature yet? If not, it's worth looking into. If you have chosen not to use 'rewrites', could you provide some insight into your decision?

Hi, even if your idea is correct in the vast majority of cases (i.e., "rewrites" will solve the problem in most cases), there are some special cases, such as when I plan to use an agent (e.g., my proxy target has to be accessed through http_proxy), where I can't add an agent using rewrites, which was possible with the previous http-proxy-middleware works.

I still hope you can try to provide support for /app style routing for your project, or we can discuss together to see how your project can remain usable with /app style routing.

@rayrapetyan
Copy link

So despite of multiple examples in this thread, /app routing is not working with next-http-proxy-middleware as of today correct?

@stegano
Copy link
Owner

stegano commented Jul 31, 2024

@rayrapetyan
Unfortunately, It is still not supported by the Next.js app router. As an alternative, you can use both the app router and the pages router. Also, I'll look into ways to use it with the app router.

If anyone knows a better way, please let me know.

@rayrapetyan
Copy link

Somehow it didn't work even with page router... I've ended up using a stand-alone nginx instance just for routing purposes.

@stegano
Copy link
Owner

stegano commented Jul 31, 2024

Somehow it didn't work even with page router... I've ended up using a stand-alone nginx instance just for routing purposes.

@rayrapetyan
It must be working if you're using the Next.js page router 😅
Doesn't this work with the page router too?

@OnielN14
Copy link

@stegano
I have used next-http-proxy-middleware in pages directory on Next 14 App Router, it works fine for me.

@lvilasboas-dti
Copy link

@stegano I have used next-http-proxy-middleware in pages directory on Next 14 App Router, it works fine for me.

@OnielN14 can you provide some example on how you got it to work?

@OnielN14
Copy link

@lvilasboas-dti
you may check this repo https://github.com/OnielN14/nextjs-http-middleware-sample. Though it using Next.js 15, but it should be fine.

@lvilasboas-dti
Copy link

I just now realized you said you were using it in pages router - I skipped this part.
I also have several endpoints in pages router where I still successfully use it - the problem is with app router.

@OnielN14
Copy link

I just now realized you said you were using it in pages router - I skipped this part. I also have several endpoints in pages router where I still successfully use it - the problem is with app router.

haha.. yeah.. sorry for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants