We’re trying to use a rather complex setup for scripts included on the page, using Gatsby / Partytown:
The redirects part mentioned there as “Hosting on other providers requires support for Gatsby’s createRedirect action to rewrite requests from /__third-party-proxy?url=${YOUR_URL} to YOUR_URL with a 200 status code. You may need to check with your hosting provider to see if this is supported.” is handled by the following Gatsby plugin:
It looks like that is all working as expected… we get a _redirects file created, with all the entries as intended. Those _redirects also all get read correctly when deploying to Netlify (they all get processed). But it’s not working for cases where there’s a Query Parameter as part of the URL targeted by the rewrite rule.
Any idea on where this might go wrong? Following the docs, for rewrites (200), it should just pass these parameters on?
Sorry, just noticed that repository is currently Private anyways… what would be the part of interest there? If this helps: We don’t have a netlify.toml, and all _headers and _redirects are generated by the Gatsby plugin, based on configuration done through Gatsby config mechanism.
I don’t think there’s anything else related to Netlify at play here. See the following screenshot for a deployment of this, where all those redirects seem to be processed successfully:
I’m not sure how the plugin works for query parameters syntax. However, I do not think your goal is achievable the way you need. The correct syntax for your requirement would look like:
/___third-party-proxy url=:url :url 200!
But this would be invalid syntax and is not supported.
I believe you can try using:
/___third-party-proxy url=:url https://:url 200!
and then, you won’t have to add any redirect rules to Gatsby, just this one rule should be able to handle it all. But, you need to make sure not to start your URLs with http:// prefix, as that’s something that would be added by us. But again, I’m not confident in this suggestion.
So it’s not possible to have redirects with Query Parameters, otherwise? I could easily opt out of that part of the plugin, I’m just not sure how the correct entry (with a Query Parameter) would look like in _redirects.
The entries generated without a Query Param work just as expected, like this one:
There’s no way (even hard-coded) to add the Query Param to the destination? It wouldn’t have to be a splat, I’d actually prefer to have the URLs added to the proxy be super explicit… we don’t need an “open proxy” that can work with arbitrary URLs.
Also experimented with URL encoding (because of the second ? in /__third-party-proxy url=https://www.googletagmanager.com/gtag/js?id=G-GTP95B7SBV https://www.googletagmanager.com/gtag/js?id=G-GTP95B7SBV 200), and a bunch of other stuff. But seems the moment the destination path includes a Query Parameter, it just stops working entirely.
with the hope that the Query Parameter (like id) would just be passed through as per the docs, but that also seems not to be the case:
While our service automatically passes on all query string parameters to destination paths for redirects with 200, 301, and 302 HTTP status, you can also choose to define a redirect path based on a specific parameter or combination of parameters.
I’ll test that. If I just opt out of the _redirects behavior of the addon, and add those rules myself I could see this work.
The goal is to just proxy those specific tracking scripts (with their Query Params, which often mark the account or property being tracked, for example). And as those calls can ultimately also come from third-party scripts, we need to be exact in the rewrites used for this to work. Does that make sense?
I almost get there. I think the main problem with all of this is the second question mark that ends up in the resulting call. Doing it like above avoids this by “replacing” the ? with &. Proxy answers successfully for that. Unfortunately the real links being called are generated by convention, through Partytown.
At this point, I’d suggest using Netlify Edge Functions for a much more fine-grained configurable options, and debugging experience: Edge Functions API | Netlify Docs. While you can’t use rewrite() method, you can definitely fetch the data and return that as the response.
I see. Was kinda hoping I could avoid that, as it seemed like a straigh-forward enough proposition by those addon docs. But I think I’ll give that a try.
But you don’t see any way to use encoding / decoding to your advantage to get that, right?
But to answer your question, I’d think even if we somehow did find a way, it would get very tricky and difficult to maintain. Should something change with the redirects engine, I’m not sure if your use case would be considered or tested. This is why I suggested Edge Functions as a better option.