3

I am using react-query prefetchQuery and NextJS (v.13) getServerSideProps along with its rewrites option in next.config.js file to proxy API calls.

The rewrites looks like this:

async rewrites() {
  return [
    {
      source: "/api/:slug*",
      destination: "http://test.example.com/api/:slug*",
    },
  ];
},

The problem is, this proxying does not work in getServerSideProps. The API response is 404 not Found.

Here is what I am doing in getServerSideProps:

const queryClient = new QueryClient();

await Promise.all([queryClient.prefetchQuery(["getData"], async () => await getData())]);

where the EndPoint being called in getData is /api/get_page_data. But everything works fine when I change the EndPoint to an absolute URL, like http://test.example.com/api/get_page_data.

P.S.: I am using Axios for calling APIs, in case this info is helpful.

1
  • I'm also interested on the solution. Did you find anything ? Commented Sep 22, 2023 at 10:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.