diff --git a/.changeset/brown-eagles-sparkle.md b/.changeset/brown-eagles-sparkle.md new file mode 100644 index 000000000..fc0789b78 --- /dev/null +++ b/.changeset/brown-eagles-sparkle.md @@ -0,0 +1,19 @@ +--- +'@cloudflare/next-on-pages': patch +--- + +avoid the default is not a function unclear error + +if there is some issue evaluating a lazy loaded edge function +its default export ends up not being a function and that results +in an unhelpful error like the following: +``` + TypeError: u.default is not a function +``` + +slightly improve the user experience by catching such error and +letting the user know that something went wrong with the edge +function's evaluation: +``` + Error: An error occurred while evaluating the target edge function () +``` \ No newline at end of file diff --git a/packages/next-on-pages/templates/_worker.js/utils/routing.ts b/packages/next-on-pages/templates/_worker.js/utils/routing.ts index b16e77df3..f5ad0297e 100644 --- a/packages/next-on-pages/templates/_worker.js/utils/routing.ts +++ b/packages/next-on-pages/templates/_worker.js/utils/routing.ts @@ -91,7 +91,20 @@ export async function runOrFetchBuildOutputItem( case 'function': case 'middleware': { const edgeFunction: EdgeFunction = await import(item.entrypoint); - resp = await edgeFunction.default(req, ctx); + try { + resp = await edgeFunction.default(req, ctx); + } catch (e) { + const err = e as Error; + if ( + err.name === 'TypeError' && + err.message.endsWith('default is not a function') + ) { + throw new Error( + `An error occurred while evaluating the target edge function (${item.entrypoint})`, + ); + } + throw e; + } break; } case 'override': {