Our WordPress site has multiple views for every page and post depending on whether or not a specific query string is used.
For example, if visited example.com/blog-post-with-chart
(a page which includes a chart as the body content) you would see the entire page. The navbar, content, footer..everything.
However, if you went to example.com/blog-post-with-chart/
?view=embed then everything but the chart would be removed. So you would only see the chart. It's how other sites might embed our charts.
WordPress's get_query_var('view')
doesn't return anything. I have also tried to just var_dump or print_r the $_GET superglobal which also dont show anything.
I've noticed that this only doesnt work if "view" is the first query string parameter. so If my URL is example.com/blog-post-with-chart/
?fake=arg&view=embed then the chart view will work as expected.
It looks like Varnish is dropping the query arguments from being passed to the server. If I add the query parameter "nocache" then everything works expected. And that is because I have a Vanish rule:
if (req.url ~ "nocache") {
set req.backend = default;
return(pass);
}
As a workaround, I added a new rule to bypass the cache if the url contains view=
if ( req.url ~ "view=" ) {
set req.backend = default;
return(pass);
}
But how can I stop Varnish from "dropping" the query args? We definitely want that page cached, and we also want it cached with the query string so that embedded view is always cached.
EDIT - here is the VCL file I am using: https://gist.github.com/kyle-jennings/589e513808fa83b93a27d1cf3c10c642
You basically ask varnish to cleanup every query argument if there is a "view" at the beginning of the query string:
So ... it comply