Cannot read query string parameters in statically cached pages
See original GitHub issueDescribe the bug
Because I statically cache pages, I load my sprig components using the 's-trigger': 'load'
directive, so that an AJAX request to index.php?p=actions/sprig-core/components/render
is made initially to get the dynamic contents of the component.
The problem is that the AJAX request doesn’t include any query string parameters, and because of that they are lost and I cannot read them inside my component. And since the page is statically cached I cannot pass the initial query string parameter like this {{ sprig('mycomponent', {page: craft.app.request.getParam('page') ?? 1}, {'s-trigger': 'load'}) }}
To reproduce
Steps to reproduce the behaviour:
- Create a mypage.twig file and Initialize a component like this
{{ sprig('mycomponent.twig', {}, { 's-trigger': 'load'}) }}
- Inside mycomponent.twig, print the query parameter
page
like this
{% if sprig.isRequest %}
{% dd page %}
{% endif %}
- Try to load
mypage?page=5
- See an error
Variable "page" does not exist.
Expected behaviour
I’d expect the query string parameters to be included in the index.php?p=actions/sprig-core/components/render
requests
Issue Analytics
- State:
- Created a year ago
- Comments:8
Top GitHub Comments
@Harry-Harrison You could probably use JavaScript to inject query string parameters as hidden input fields into the Sprig component before it refreshes on load, if need be.
If anyone lands on this thread in future trying to do what I was doing, this is how I’ve done it.
With Blitz set to “Cache URLs with unique query strings as the same page”…
If anyone has a better way, I’d love to know, but this worked great for me 👍