question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read query string parameters in statically cached pages

See original GitHub issue

Describe 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:

  1. Create a mypage.twig file and Initialize a component like this {{ sprig('mycomponent.twig', {}, { 's-trigger': 'load'}) }}
  2. Inside mycomponent.twig, print the query parameter page like this
{% if sprig.isRequest %}
    {% dd page %}
{% endif %}
  1. Try to load mypage?page=5
  2. 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:closed
  • Created a year ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
bencrokercommented, Dec 14, 2022

@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.

0reactions
Harry-Harrisoncommented, Dec 19, 2022

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”…

{# Top level page #}
{{ sprig('mysprig/component', {}, { 's-trigger': 'load' }) }}
{# Component – ?id= is the param we're after #}
{{ hiddenInput('idVar', '') }}

{% js %}
    var urlParams = new URLSearchParams(location.search);
    document.getElementsByName("idVar")[0].setAttribute("value", urlParams('id'));
{% endjs %}

{% if sprig.isRequest %}
    {{ idVar }}
{% endif %}

If anyone has a better way, I’d love to know, but this worked great for me 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve Query String Cache issue in ASP.NET?
In my ASP.NET web site i am loading a page with a query string parameter attached to process something based on this query...
Read more >
Static caching and query parameters #3111 - statamic/cms
@jasonvarga I think OP wants to use only specific query strings to impact the static cache. I.e. it should generate a new page...
Read more >
Cache Pages with Query String Parameters for 10 minutes
I want to cache pages with query string parameters for the duration of 10 minutes. I can get it to work with the...
Read more >
How to Remove Query Strings from Static Resources ... - Kinsta
Not every server or CDN can cache query strings. Follow this tutorial on how to remove query strings from static resources in WordPress...
Read more >
7 reasons to not 'Remove query strings' from static resources
1. Squid supports query strings by default · 2. Sirv CDN is a proxy · 3. Google changed its advice in 2014 ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found