-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connect-web requests fail in next.js middleware #749
Comments
I am also experiencing the same issue. Extremely critical for us, hope this has a high priority. |
Running Connect on edge platforms such as Vercels edge runtime and Cloudflare workers is an important feature, and we do plan to support them in the future. This is more involved that you might think though. While it's true that Node.js, many browsers, and Vercels Edge runtime use V8 under the hood, V8 is "just" a runtime for ECMAScript. It does not implement any parts of the fetch, streams, or encoding API. All platforms bring their own implementation of those APIs, which may be incomplete, have bugs, or interpret ambiguous parts of the API spec differently. For example, fetch in Node.js is implemented by undici, and the Vercel Edge runtime is very likely using a different implementation. When developing locally, you are not actually running code in the Vercel Edge runtime, but in a simulated environment, that may behave slightly differently because it may be running a different implementation of fetch. See the second section in Vercels documentation. The error you are running into is raised in node_modules/next/dist/compiled/edge-runtime/index.js:1:970334, which seems to be the simulated environment, source code here: https://github.com/vercel/edge-runtime At this moment, we cannot focus on edge runtime support, but this project being open source, you can help bringing it forward. Finding the location of the error in the source code, and investigating how it is triggered seem to be the logical first steps. It may be an issue we can resolve in connect-es, or it may be an issue that needs a resolution in the edge-runtime package. |
Thanks @timostamm for your extensive comment! Yea I was a bit too unspecific and just wrong here. Just wrote the ticket as I thought there probably might be a quick fix. I was already wondering if it has to do with the edge-runtime and thought about cross-posting. I will try to investigate further, by i.e. running on Vercel itself. Thanks for clarifying that you won't focus on that right now, I'll investigate further then. |
Ok, what I found out: vercel/edge-runtime is using mysticatea/event-target-shim as a ponyfill. In its It happens to be, that the callback is the This fiddles with The questions now are:
|
This issue was fixed in vercel/edge-runtime by dropping node 14 support & removing the erroneous polyfill (See issue). The updated vercel/edge-runtime@2.5.0 will land in next.js with version 13.4.20 (See Changelog) This means, that connect-web will be perfectly usable in vercel's edge functions, as long as you're setting the request's import { Interceptor } from "@bufbuild/connect"
import { createConnectTransport } from "@bufbuild/connect-web"
const FollowRedirectsInterceptor: Interceptor = (next) => (req) => {
req.init.redirect = "follow"
return next(req)
}
const transport = createConnectTransport({
baseUrl: "https://example.net/grpc",
interceptors: [FollowRedirectsInterceptor],
}) Thanks @timostamm for pointing me into the right direction, I'll close this issue. |
Node v16 was problematic for me as well. v18 and v20 work fine though. |
Describe the bug
I try to use
connect-web
in the Next.js middleware, as Node APIs are not available, thusconnect-node
does not work.I expect it to work, as it's just using thefetch
api, and Next.js is running on V8, just asconnect-web
in the browser.I expect it to work, as the edge runtime seems to support all necessary features required by
connect-web
.However, requests fail with:
Also saw #550, which reports an issue w/ cloudflare workers & the AbortController, which also is a similar configuration, however not consuming but providing connect services.does not seem to be relatedTo Reproduce
npm install
npm run dev
Expected behavior
I expect the middleware to send the request using
connect-web
's connect transport, which uses only APIs available in browsers. Therefore I expect it to work also in vercel's edge runtime, which is running a browser-like environment.Actual behavior
The following error is thrown:
Environment
@bufbuild/connect-web@0.12.0
react@18.2.0
next@13.4.13
node@20.5.0
The text was updated successfully, but these errors were encountered: