Hi !
I am new to edge functions and tried to create a simple referrer check before displaying the website.
I deployed it to production to check if it was working, and it seems the logs are not showing consistently, and the edge function is not called on each request.
Am i missing something about edge functions ?
I thought it was some kind of hook before the request is passed to the website.
Here is the code I am using:
import { Context } from "@netlify/edge-functions"
const acceptList = [
'https://some.website.com',
'https://other.website.com'
]
export default async (request: Request, context: Context) => {
// get HTTP referrer header value
const referrer = request.headers.get("referrer") ?? request.headers.get("referer")
console.log('Got a request coming from referrer: ', referrer)
// if referrer included in acceptList, return response
if (acceptList.some(url => referrer?.startsWith(url))) {
console.log('referrer ok. Sending back response.');
// get the next HTTP response in the chain
const response = await context.next()
return response
}
// Else return 404
console.log('Wrong referrer, sending back 404.');
return new Response("Page not found", {
headers: { "content-type": "text/html" },
status: 404
})
};