So I’m attempting to implement a very simple edge function for my Blazor WASM site. However the edge function does not seem to be working under my deployment preview at:
https://app.netlify.com/projects/system-crash/deploys/68694ad288248600086c1ccc/golive
the site loads just fine, but I get a 404 on ./golive.
I added a wwwroot/netlify/edge-functions/golive.ts
to my WASM project (wwwroot
being my publish directory, which should be my base directory)
The file code is as such: (and yes I have a GOLIVE environment variable setup as a simple string)
import type { Config, Context } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
return new Response(Netlify.env.get("GOLIVE"));
};
export const config: Config = {
path: "/golive",
};
However I don’t see any edge functions in the project UI (maybe I won’t until I deploy to prod)?
Is there anything else I need to do during build to enable edge functions? I don’t think this is a Blazor routing conflict, cause I get a Netlify 404 back, and not a Blazor redirect.
I setup the Netlify CLI and tested this locally, which half worked. The edge function ran, but oddly enough Netlify.env.get("GOLIVE")
returned undefined
(despite being logged in, and the documentation stating it would pull all the environment variables from my project).
I subsequently pushed out an update to return the response as JSON:
import type { Config, Context } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
const golive = Netlify.env.get("GOLIVE");
return Response.json({ GoLive: `${golive}` });
};
export const config: Config = {
path: "/golive",
};
but I still get a 404 from the preview site.
This might actually be a Blazor routing issue after all… I published my site locally, then ran netlify dev
on the published wwwroot
and the site ran, but the edge function URL returned a 404 this time (before I was running netlify dev
on the wwwroot
source folder which didn’t run the site, but did run the edge function).
I’m going to have to dig into this a little deeper. There is still the issue of Netlify.env.get("GOLIVE")
returning undefined… so if anyone knows why that may be happening, please let me know.
Okay, I made one big mistake… turns out I was using the wrong folder, I should not have been using wwwroot
, because Netlify expects to scan the actual base directory for edge functions. So I moved the netlify
folder up to the project root and now it’s working as expected when deployed (the environment variable is even returned correctly).
Still need to figure out a good way to get this all working locally from a development / debugging perspective, but that can wait for now.