Middleware running in Edge Function is broken by design. Proposal: Opt-out Middleware to use Node.js/Serverless Function runtime instead of Edge Function (or introduce caching in Edge Function) #50577
Closed
awinogrodzki
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
After some recent experiments, it seems that you can share global variables between multiple requests in Edge runtime, which basically solves the problem of caching between requests. Closing the discussion |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
No response
Background
I am the author of next-firebase-auth-edge library.
The idea of this library is to setup simple authentication using Next.js Middleware as a single point of configuration.
I managed to make Firebase Authentication work in Edge environment, BUT...
In middleware, before I validate request using RequestCookies, I have to fetch Google x509 certificates from https://www.googleapis.com/robot/v1/metadata/x509/[email protected]. I need those certificates to act as public key, which I then verify token against.
Those certificates have expiry date, which means I cannot load them during build time.
Middleware runs on Edge Function which does not expose
CacheStorage
APIWhich means – when app with
next-firebase-auth-edge
is deployed to Vercel environment, a call to https://www.googleapis.com/robot/v1/metadata/x509/[email protected] is made with each request.There's no simple workaround for this issue.
It's possible to create proxy API Route to fetch Google certificates, and then call it from inside Middleware, but adding a dedicated API route each time a request has to be cached is counter-productive and definitely not a good idea for a library.
Which begs the question – why Middleware is called by Edge Function in the first place? If there's no possibility to cache inside Edge Function, it simply won't serve it's purpose.
The main idea behind Edge Function is to make round-trips fast. Eventually people will be forced to load things in Middleware before processing the request, which makes Edge Function more harmful than Node.js environment for Middleware, as they would have to make N+ roundtrips to different servers before actually returning a response.
My personal solution was to unfortunately move away from Vercel and use AWS Node.js environment to make
CacheStorage
usable in Middleware.I also created #50102 issue on this topic
Proposal
Middleware file could export
runtime
variable with either one of following values:edge
,serverless
,nodejs
I am interested in contributing
Beta Was this translation helpful? Give feedback.
All reactions