open-next icon indicating copy to clipboard operation
open-next copied to clipboard

UnhandledPromiseRejection when using notFound or redirect functions

Open leanderiversen opened this issue 2 years ago • 4 comments

Hi,

We have just discovered an issue where we can see Runtime.UnhandledPromiseRejection in the logs when using the notFound or redirect functions from next/navigation. The error messages are Error: NEXT_NOT_FOUND and Error: NEXT_REDIRECT, with the page itself displaying Internal Server Error, rather than executing the function.

This is a replica of the code, which lives in this file path: /src/app/blogs/[slug]/page.tsx.

const post = await getPost(params.slug);

if (!post) {
  notFound(); // or redirect('/')
}

When running this locally, it works as expected, with either a 404 page or redirect. We are using Next 14.1.2, SST 2.40.6 and OpenNext 2.3.7.

If there is more information I can provide, please just ask.

Thank you.

leanderiversen avatar Mar 09 '24 07:03 leanderiversen

It is executing the function, these functions are supposed to throw those exceptions. But it should be handled by NextServer itself.

I've tried to reproduce with something like that with [email protected] and [email protected] and it works just fine. Could you provide a minimum reproduction ?

import React from 'react';
import Image from 'next/image';
import { notFound, redirect } from 'next/navigation';

export const dynamic = "force-dynamic"

const Product = async ({ params: { id } }: { params: { id: string } }) => {

  if(id=== "not-found") {
    notFound()
  }
  if(id === "redirect") {
    redirect("/product/1")
  }
  return (
    <div>
      THE COMPONENT
    </div>
  );
};

conico974 avatar Mar 09 '24 11:03 conico974

Hi,

Please find the minimum reproduction here: https://github.com/leanderiversen/open-next-379

It looks like there could be an issue with output: standalone , rather than OpenNext, as it looks like it fails to fetch the CSS and JS required.

Happy to report this to Next directly if that is the case.

Thank you!

leanderiversen avatar Mar 09 '24 12:03 leanderiversen

This does not reproduce the issue at all, i added sst and deployed and both notFound and redirect works. For your information, with standalone output you have to manually add the css and js

conico974 avatar Mar 09 '24 13:03 conico974

Hi,

I am very sorry about this. Following your feedback, I have managed to find the root cause, which was an unexpected error in the fetch function used in generateStaticParams, when there were no data to fetch. The fetch function returns an empty array when there is no content during build, so I suspect that something was going wrong when trying to fetch the data on demand when visiting the /blogs/[slug] pages.

Sorry for the inconvenience this has caused.

leanderiversen avatar Mar 09 '24 14:03 leanderiversen