I’m troubleshooting something very similar, but I think even more basic in that I’m not passing a variable in from
to to
and I still have the same issue. Everything works on local with netlify dev
but when I push to a preview branch it doesn’t work.
tldr:
Given this rule in my _redirects
file:
/api/v1/search /.netlify/functions/api-v1-search?siteId=macos 200
With netlify dev
on my local machine, event.queryStringParameters.siteId
in my function is macos
.
When pushed to a preview branch, event.queryStringParameters.siteId
in my function is undefined
.
More Context
I want to include a specific query parameter for a given domain, so the value gets hardcoded into the redirect itself at build time. I have a siteId
variable that gets set in the build. So my _redirects
template file looks like this:
/api/v1/search /.netlify/functions/api-v1-search?siteId=${siteId} 200
When this gets built, the resulting _redirects
file on disk puts out a redirect rule like this:
/api/v1/search /.netlify/functions/api-v1-search?siteId=macos 200
The weird thing is, like others in this thread, the way that rule functions seems to differ between what i’m running locally with netlify dev
and what is running in a preview branch build even though the redirect rule is the exact same.
Local redirect
When I run things locally using netlify dev
, I get a _redirects
file on disk with this rule:
/api/v1/search /.netlify/functions/api-v1-search?siteId=macos 200
Great. My function actually echos back the URL that was hit, so when i hit /api/v1/search
on local using netlify dev
, it gives me back this in the response:
"links": {
"self": "https://www.macosicongallery.com/api/v1/search"
},
Cool. Everything works as expected locally. So I push to a preview branch.
Preview Branch redirect
So now i have the same code that was working locally, but now being built remotely by netlify’s servers and on a preview branch. Through some logging in the build, I’ve confirmed that the rule in the _redirects
file is the exact same one in the local build:
/api/v1/search /.netlify/functions/api-v1-search?siteId=macos 200
But when i hit /api/v1/search
on this remote preview URL – https://5fd447f2a818530007016641--macosicongallery.netlify.app/api/v1/search – it doesn’t echo back the same thing when I did it locally. I get undefined
in place of where i was getting macos
on my local build:
"links": {
"self": "https://www.undefinedicongallery.com/api/v1/search"
},
To me, this means that the request coming into the lambda function isn’t detecting a siteId
query paramenter. But i’m unsure how/where I’m doing the wrong thing, especially since it’s working locally using netlify dev
(although, it sounds like from this thread, netlify dev
is just an approximation, which is obviously terribly misleading in this case).
Given what Netlify’s docs say:
Query string parameters are not automatically passed to our redirect engine.
I’m still not sure why this rule isn’t working:
/api/v1/search /.netlify/functions/api-v1-search?siteId=macos 200
I’m not trying to passing query params from /api...
to /.netlify/functions...
. I’m just tacking on a hard-coded query param in the to
, which I’m expecting to be passed to the invoked lambda. But somehow it’s being set to undefined
on my preview branch.