We want to dynamically set a path based on the value of an inbound cookie, so were looking at Layer0 Router Embedded Values
Which says “You can inject values from the request or response into headers or cookies as template literals”
However, we want to use the value of the cookie as part of a redirect path - something like:
.match('/redirect/:path*',
({ redirect }) => {
redirect('https://www.example.com/${req:cookie:test}', 301)
})
However doing exactly that results in the following:
location: https://www.example.com/$%7Breq:test%7D
So we ended up using compute to parse the cookies from request.headers.cookie - just want to see if there is a way to utilize the cookie directly in the redirect path without needing to go into compute.
We need to do some further manipulation of the string so it may need compute
regardless, so just looking for future info since something seems to be happening with that string in the above example (curlybraces are encoded and the word cookie gets eaten)
UPDATE:
did some more testing
I found that this works:
.match('/redirect/:path*',
({ setResponseHeader }) => {
setResponseHeader('Location', 'https://www.example.com/${req:cookie:test}')
})
So redirect
and setResponseHeader
treat those embedded values differently.
So 3 questions:
-
Is it expected that the embedded values are not available in
redirect
? -
If we decided to go the route of manually setting the Location header, is there a way to set the status code without having to use
transformResponse
orcompute
to avoid utilizing serverless for something so trivial? -
if we do use
compute
ortransformResponse
is thereq:cookie
object used for those embedded values available, or do we need to manually convert it from a string viarequest.headers.cookie
?