Edge function crashing

Good Day.
I searched and didn’t come up with any relevant answers or clues.

AstroJS 5.0.4

I was trying to debug why a remote image wasn’t loading. I suddenly started receiving the This edge functon has crashed.

An unhandled error in the function code triggered the following message:

uncaught exception during edge function invocation
Connection details
Netlify internal ID: 01JF0B9PHAK8HFXGQ63FTP38BN

So I figured it was something I did. So I re-fetched the remote repo to get back to where everything was prior to my image debugging. I’m still getting the error.

The edge function log:

Dec 13, 10:47:37 AM: 01JF0B8E info   [Astro Middleware] LocalsReassigned: `locals` can not be assigned directly.
    at Object.set locals (file:///root/.netlify/v1/edge-functions/middleware/middleware.mjs:2937:13)
    at entry_default (file:///root/.netlify/v1/edge-functions/middleware/middleware.mjs:2985:14)
    at file:///root/src/bootstrap/function_chain.ts:426:15
    at AsyncLocalStorage.run (node:async_hooks:69:14)
    at FunctionChain.runFunction (file:///root/src/bootstrap/function_chain.ts:420:28)
    at FunctionChain.run (file:///root/src/bootstrap/function_chain.ts:324:31)
    at handleRequest (file:///root/src/bootstrap/handler.ts:148:34)
    at file:///root/src/bootstrap/server.ts:39:7
    at ext:deno_http/00_serve.ts:369:24
    at ext:deno_http/00_serve.ts:569:29 {
  loc: undefined,
  title: "`locals` must not be reassigned.",
  hint: "Set a `locals` property instead.",
  frame: undefined,
  type: "AstroError",
  name: "LocalsReassigned"
}

The same build works fine with netlify dev and I build locally and netlify deploy which crashes the edge function.

Here’s the relevant section of astro.config.mjs:

// i tried the /functions from another support thread. neither are solving the issue.
//import netlify from "@astrojs/netlify";
import netlify from "@astrojs/netlify/functions"
...
export default defineConfig({
...
  adapter: netlify({
    edgeMiddleware: true,
    imageCDN: true,
  }),
...

and my netlify.toml:

[functions]
  directory = "netlify/functions" # Points to your custom functions directory
[[redirects]]
  from = "/posts/*"
  to = "/.netlify/functions/builder"
  status = 200
  force = true

I do not have any files in /netlify/functions

Any ideas?
I tried deploying to a new site by drag and drop the /dist folder after building, but still getting the same edge function crashed error.
Thanks.

The error is coming from this line: adapters/packages/netlify/src/index.ts at main · withastro/adapters. That line is correct, but in your deployed site, the line looks a little different:

ctx.locals = { netlify: { context } };

So just like the error says, the line in your code is trying to re-assign the locals while the line in Astro’s codebase is just adding a property to locals. As for why your site has the incorrect line, that I don’t know. Since the Edge Function is generated by Astro, that’s something you can ask them.

I do see there was a commit yesterday:

y

which probably fixed this, so you might just have to update Astro and or the adapter.