From 471d11fce1cee246c017bc6c089f0f5fb5f85d1c Mon Sep 17 00:00:00 2001 From: Jack Williams Date: Tue, 12 Dec 2023 14:10:59 +0000 Subject: [PATCH] Don't return a `Response` in `inngest/next` if in serverless (#421) ## Summary Next.js versions `next@>=13.0.0 <13.5.0` will throw an error if a `Response` object is returned from a serverless endpoint. `next@>=13.5.0` will display the error but continue to function. Next.js edge still requires that the `ReturnType` of the function is a `Response`, so we keep that type whilst ensuring it's not actually passed back in serverless endpoints. There are so many edge cases here and this one breaks across minor versions. Separate exports (e.g. `inngest/next-13-edge`) might be appropriate in the future if the type/runtime guards continue to become stricter on the Next.js side. ## Checklist - [ ] ~~Added a [docs PR](https://github.com/inngest/website) that references this PR~~ N/A Bug fix - [ ] ~~Added unit/integration tests~~ N/A Will observe logs disappearing in current integration tests which use `next@13.5.4`: https://github.com/inngest/inngest-js/actions/runs/7181668471/job/19556571083?pr=421 - [x] Added changesets if applicable ## Related - #400 --- .changeset/silly-dolphins-pump.md | 5 +++++ packages/inngest/src/next.ts | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/silly-dolphins-pump.md diff --git a/.changeset/silly-dolphins-pump.md b/.changeset/silly-dolphins-pump.md new file mode 100644 index 000000000..e4bcee62e --- /dev/null +++ b/.changeset/silly-dolphins-pump.md @@ -0,0 +1,5 @@ +--- +"inngest": patch +--- + +Fix serverless use of `inngest/next` with `next@>=13.0.0 <13.5.0` failing to return a response, as well as `next@>=13.5.0` logging the same error diff --git a/packages/inngest/src/next.ts b/packages/inngest/src/next.ts index 28565db65..c832c2d6d 100644 --- a/packages/inngest/src/next.ts +++ b/packages/inngest/src/next.ts @@ -14,6 +14,8 @@ export const frameworkName: SupportedFrameworkName = "nextjs"; * In Next.js, serve and register any declared functions with Inngest, making * them available to be triggered by events. * + * Supports Next.js 12+, both serverless and edge. + * * @example Next.js <=12 or the pages router can export the handler directly * ```ts * export default serve({ client: inngest, functions: [fn1, fn2] }); @@ -150,15 +152,21 @@ export const serve = (options: ServeHandlerOptions) => { typeof res?.send === "function" ) { res.status(status).send(body); + + /** + * If we're here, we're in a serverless endpoint (not edge), so + * we've correctly sent the response and can return `undefined`. + * + * Next.js 13 edge requires that the return value is typed as + * `Response`, so we still enforce that as we cannot dynamically + * adjust typing based on the environment. + */ + return undefined as unknown as Response; } /** - * Next.js 13 requires that the return value is always `Response`, - * though this serve handler can't understand if we're using 12 or 13. - * - * 12 doesn't seem to care if we also return a response from the - * handler, so we'll just return `undefined` here, which will be safe - * at runtime and enforce types for use with Next.js 13. + * If we're here, we're in an edge environment and need to return a + * `Response` object. * * We also don't know if the current environment has a native * `Response` object, so we'll grab that first.