sst / open-next

Open source Next.js serverless adapter
https://open-next.js.org
MIT License
3.7k stars 111 forks source link

UnhandledPromiseRejection when using notFound or redirect functions #379

Closed leanderiversen closed 4 months ago

leanderiversen commented 4 months ago

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.

conico974 commented 4 months ago

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 next@14.1.2 and sst@2.40.6 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>
  );
};
leanderiversen commented 4 months ago

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!

conico974 commented 4 months ago

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

leanderiversen commented 4 months ago

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.