question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[middlewares via Edge Functions] fetch is not complete, runtime is not addressable

See original GitHub issue

What version of Next.js are you using?

12.0.4-canary.15

What version of Node.js are you using?

v14.18.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

Cloudflare Workers, which powers Edge Functions, has a caveat in the fetch implementation. The referrerPolicy, credentials, and mode options cannot be used. When used the code will throw/reject.

This is fine in Cloudflare Workers since it’s easy to detect that javascript runtime using something along the lines of typeof WebSocketPair === 'function', but it is not fine for Edge Functions since that global context (e.g. WebSocketPair) is not reachable.

Without a way to detect Edge Function runtime it is not possible to write universal code that uses these options in the browser but omits using them in Edge Functions.

Expected Behavior

It is possible to uniquely detect the runtime so that workarounds in universal libraries can be made to target said runtime.

If there’s a global that is unique to Edge Functions / Middlewares, we can use that, if not, add something fixed to process.env instead?

To Reproduce

This works in next dev but fails deployed to vercel.com. If a universal library does this, it needs a way to detect Edge Functions are used as a runtime to omit adding the referrerPolicy, credentials, and mode options.

export async function middleware(req, ev) {
  let fetchFailed = false;
  try {
    await fetch(new URL('https://www.googleapis.com/oauth2/v3/certs'), {
      redirect: 'manual',
      method: 'GET',
      referrerPolicy: 'no-referrer',
      credentials: 'omit',
      mode: 'cors',
    })
  } catch (err)  {
    fetchFailed = err.stack
  }

  return new Response(JSON.stringify({
    fetchFailed,
  }), { status: 200 });
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
panvacommented, Jul 4, 2022

EdgeRuntime runtime global can now be used to identify that Edge Functions or dev/local is used. But in order to work around the unique quirks of cloudflare workers behind Edge Fucntions production deployment, discerning between the two (prod / rest) is still left to be surfaced.

To demonstrate the issue again, run the following in edge-runtime

await fetch(new URL('https://www.googleapis.com/oauth2/v3/certs'), {
      redirect: 'manual',
      method: 'GET',
      referrerPolicy: 'no-referrer',
      credentials: 'omit',
      mode: 'cors',
    }).then((response) => response.json())

It will work.

But in production, it won’t, because CF workers fetch implementation is not the same as node’s / the polyfill used in edge-runtime. The Workers runtime is ever changing and unique, that’s why knowing if the code is in dev or prod is important for me as a library developer knowing the unique quirks of CF workers that may be encountered.

The issue with fetch is just one of probably many. Another one that surely is unique to CF Workers is how inverting control flow is handled / not allowed in prod.

0reactions
github-actions[bot]commented, Aug 21, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Edge Middleware Limitations – Vercel Docs
Learn about the limitations of using Edge Middleware with Vercel. ... Dynamic code execution is not available in Edge Middleware for security reasons....
Read more >
How to Use Next.js Middleware with Edge Functions - Netlify
js Middleware can run at the edge instead of from an origin server. This means Next.js developers using Netlify will see performance ...
Read more >
Advanced Features: Debugging - Next.js
This documentation explains how you can debug your Next.js frontend and backend code with full source maps support using either the VS Code...
Read more >
Deliver dynamic content at the speed of static with Next.js ...
The Edge Runtime exposes and extends a subset of Web Standard APIs such FetchEvent , Response , and Request , to give you...
Read more >
nextjs route middleware for authentication - Stack Overflow
Which I believe is because it's not using express at this point but next's custom routing. Is there a way I can bake...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found