I am getting the error `The script will

I am getting the error The script will never generate a response when trying to use fetch() in my generateMetaData function in NextJS 14. I've seen a lot of similar bugs reported on this but haven't been able to find a solution. Is anyone familiar with this? I can reproduce this locally when running npx @Cloudflare/next-on-pages@1 and then using wrangler to do a preview, otherwise this works on local. The error I was getting before reproducing on local (in my dev cloudflare url) was A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
4 Replies
ard
ardOP7mo ago
This is the code that's causing it:
export async function generateMetadata(
{ params }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
const { collectionId } = params;

const collections_endpoint = process.env.NEXT_PUBLIC_COLLECTIONS_SERVER;

const response = await fetch(
`${collections_endpoint}/v1/collections/${collectionId}`,
{
method: "GET",
headers: {
Accept: "*/*",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
},
}
);
export async function generateMetadata(
{ params }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
const { collectionId } = params;

const collections_endpoint = process.env.NEXT_PUBLIC_COLLECTIONS_SERVER;

const response = await fetch(
`${collections_endpoint}/v1/collections/${collectionId}`,
{
method: "GET",
headers: {
Accept: "*/*",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
},
}
);
ItsWendell
ItsWendell6mo ago
Are you sure it happens because of this metadata?
ard
ardOP6mo ago
I am 99% sure it is because of the fetch call inside the metadata, but specifically the fetch(). I was able to avoid the crash by using a timeout on the promise, but it stills gives me the error just prevents the crash
dzoni
dzoni5mo ago
I had a similar error, fixed with the edge runtime included in my server side component
Want results from more Discord servers?
Add your server