putyourlightson / craft-blitz

Intelligent static page caching for creating lightning-fast sites with Craft CMS.
https://putyourlightson.com/plugins/blitz
Other
147 stars 35 forks source link

includeDynamic not working as expected #663

Closed cweiper closed 1 month ago

cweiper commented 1 month ago

Support Request

Hi,

Loving the plugin so far. Working on getting our search page working without just fully excluding it via page options.

We have this include in our _pages/_search.twig template

{{ craft.blitz.includeDynamic("_pages/_partials/_dynamicSearch", {'query' : craft.app.request.queryParams['q'] }, { 'placeholder' : placeholder }) }}

Here is the contents of the _pages/_partials/_dynamicSearch template:

<h2 class="font-heading font-black text-4xl md:text-7xl text-dumindigo">Search for "{{ query }}"</h2>
{# Only entries with a valid URL #}
{% set results = craft.entries
    .search(query)
    .status('live')
    .orderBy('score')
    .uri(':notempty:')
    .all() %}

{# Remove duplicates #}
{% set results = results|filter|unique %}

<p class="text-base font-medium text-gray-500 mt-4">{{ results|length }} results found for <span class="font-bold">"{{query}}"</span></p>

<div class="mt-12">
{% for result in results %}
    <div class="border-b-2 border-gray-200 pt-6 pb-12">
        <a href="{{ result.url }}" class="text-3xl font-light underline">{{ result.title }}</a>
        <p class="text-gray-500">{{ '' }}</p>
        <a href="{{ result.url }}" class="text-dumred font-bold underline mt-6 block">{{result.url}}</a>
    </div>
{% endfor %}

When I hit the page for the first time, it appeared to work properly, displaying the query i had submitted. However, the second time I hit the page it seemed the AJAX request was still returning the same markup from the first page. So I am not sure where the caching is happening in that relationship. We have all Craft template caching turned off.

Thanks for the help.

Plugin Version

4.17.0

cweiper commented 1 month ago

After doing some additional testing I can confirm that the dynamic template is executing every time, but the data being sent to it is stale.

bencroker commented 1 month ago

Have you set the Query String Caching setting to Cache URLs with query strings as unique pages? If not, then Blitz won’t differentiate between the various search terms and the same results that were first searched for will appear on every subsequent request.

cweiper commented 1 month ago

We were really hoping to avoid using that setting to help reduce the number of records we have (might be an unnecessary worry) because of UTM campaigns, etc. I was hoping that Blitz would still pass that through to the dynamic template but I can understand why it wouldn't with my settings.

I guess I am more curious how any dynamic content would get passed into that `craft.blitz.includeDynamic' if the inputs become stale after the first request. Am I not understanding the functionality correctly? Is it more about building a completely dynamic template at the time of request, and not about building one thats dependent on input from the request?

Feel free to close if this is working as expected.

bencroker commented 1 month ago

Blitz caches the entire page, meaning that your Twig code is never rendered again once the page is initially cached. This includes the rendered value of craft.app.request.queryParams['q']. So the issue is that you’ve specified that the page should be cached as the same page, regardless of the value of the query string parameters.

If caching lots of pages with query string parameters is an issue, I suggest you explicitly set the “Included Query String Parameters” setting (under Advanced Settings) to only the params you want to allow caching for, in this case q. That way, UTM campaigns and other query string params will have no effect on what is, and what is not, cached.

Alternatively, you could cache a single search page and use JavaScript to extract the query string params (using URLSearchParams) and fetch the dynamic template manually.

cweiper commented 1 month ago

Great suggestions, thanks for the quick replies today!