What I want to build:
My goal is to have one website available through multiple domain names. I added a redirect to my website so all incoming request to the root actually go through my lambda. Based on which domain is currently targeted, I want to return slightly different content.
What I tried
During development, I was using netlify-lambda to locally serve my functions. In that context, I could read event.headers.host and correctly get localhost:9000 so I assumed this was the way to go.
But once I deployed in production, I realized the headers exposed are totally different and do not include any mention of the public hostname.
Other workaround I thought of
I thought that maybe I could redirect each domain to a different subdirectory and then add redirect rules for each of this subdirectories to pass and additional header mirroring the domain name.
Something like www.foo1.com goes to foo.netlify.com/foo1 and www.foo2.com goes to foo.netlify.com/foo2 and then I add redirects such as /foo1 goes to my lambda with X-hostname: www.foo1.com as an additional header.
But I couldn’t find a way to have domains target anything else than the root, so I’m also blocked here.
Any idea, even an convoluted one, how I could have my lambdas aware of the url they’re running on?
It might help to clarify something before we go further. Are the function calls themselves happening because of a Netlify redirect? Or is site javascript making the calls to the functions? It sounds like the former is the case but I wanted to be sure.
Ah, I’m not sure there is a workaround for that. If you are invoking the function from somewhere that is not the site it is associated with, you could probably include a custom-header that includes the request host, so you can check a single place, since the context object is not available in ‘netlify dev’ and I doubt it would ever be.
As you can see, roles shows [Array] instead of actual array, so the logs is not going in dept. Seems like you might be logging something else? Object is the correct representation of what type it is.
Is there a solution for ntl dev AND prod to get the hostname in a function from a client js fetch call? Reading this thread I cannot see one. In dev, event.headers.host works, but likely not in prod. Is there a workaround? Maybe check if typeof(event.headers.host)!='undefined' before in prod to not get a functions error? In dev, context.clientContext for me results in {}, mentioned in the thread that it does not work.
ok, I did ntl deploy --prod and can log the function:Sep 3, 10:27:38 AM: 38e15b75 INFO 1 { custom: { netlify: 'eyJzaXRlX3VybCI6Imh0dHBzOi8vNjMxMzBmZDM4MGZlMDU2OTI1OGYxZGEwLS10aWsyLm5ldGxpZnkuYXBwIn0=' } } 2 63130fd380fe0569258f1da0--tik2.netlify.app Sep 3, 10:27:38 AM: 38e15b75 Duration: 143.05 ms Memory Usage: 86 MB. So event.headers…
works on prod.