Skip to content

Commit

Permalink
Don't return a Response in inngest/next if in serverless (#421)
Browse files Browse the repository at this point in the history
## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

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
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [ ] ~~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 `[email protected]`:
https://github.com/inngest/inngest-js/actions/runs/7181668471/job/19556571083?pr=421
- [x] Added changesets if applicable

## Related
<!-- A space for any related links, issues, or PRs. -->
<!-- Linear issues are autolinked. -->
<!-- e.g. - INN-123 -->
<!-- GitHub issues/PRs can be linked using shorthand. -->
<!-- e.g. "- inngest/inngest#123" -->
<!-- Feel free to remove this section if there are no applicable related
links.-->
- #400
  • Loading branch information
jpwilliams authored Dec 12, 2023
1 parent 4de1605 commit 471d11f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-dolphins-pump.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 14 additions & 6 deletions packages/inngest/src/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 471d11f

Please sign in to comment.