hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2

Home Page:https://www.npmjs.com/package/next-connect

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handler not getting triggered on redirect.

Goutam192002 opened this issue · comments

I have a next.js page which redirects to a different page. Here's the snippet for the page..

export const getServerSideProps = async (context) => {
    // do something
    return {
       props,
       redirect: { destination: '/status', permanent: false }
    }
}

In the status (redirected page) I have a server side function which does a db query and returns the result as props...
Here's the snippet for it:

export const getServerSideProps: GetServerSideProps = async ({ req, res, params }) => {
  let mandate;
  const handler = nextConnect().use(databaseMiddleware).get(
    async (_req, _res, next) => {
      mandate = await Mandate.findById<IMandate>(params?.id).populate({
        path: 'customer',
        model: Customer
      }).populate({
        path: 'merchant',
        model: Merchant
      }).lean();
      next();
    }
  );
  await handler.run(req, res);
  return { 
    props: {
      mandate: JSON.parse(JSON.stringify(mandate))
    }
  };
}

After debugging I was able to figure out, that the redirected page's handler is not getting called on redirect..it works fine on loading it normally via browser request.

Is this a known bug ? Is there a known fix for this?

commented

Thanks for the issue and code snippet. However I cannot reproduce the issue: https://stackblitz.com/edit/nextjs-z41yqg

The setup is similar to yours except it does not have the database middleware and actually call the database methods.

Maybe there is something wrong with your database/database middleware?