getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.4k stars 1.39k forks source link

Taxonomy filtering do nothing if cache is enabled #2487

Open Heraes-git opened 5 years ago

Heraes-git commented 5 years ago

Hi,

Having a problem on my production server, but not on my dev one, I suspected that the cache was involved. After disabling it completly, the problem disappeared.

Here was the problem : when clicking on a tag label, the filtering was failing and the page was reloading with all the blog items, instead of filtering them. Yet, the URI has all the required params.

My actual blog config

I use a special user_blog_route: frontmatter option, in order to solve the use of {{ blog_url|rtrim('/') }} in taxonomy.html.twig (taken from the Quark theme). Indeed, the native system provides only one blog route in site.yaml.

So in this user_blog_route:, I did set '{{page.parent.url}}' in order to use its parent's route (/critiques, actually) in a dynamic manner to avoid breaking if the page is moved.

Apart from that, my blog system is pretty a clone of the Quark one. I control filtering with this :

    {% set tag = uri.param('tag') %}
    {% set request = tag ? taxonomy.findTaxonomy({'tag':tag}) : collection %}
    {% for child in request %}
        {% include 'partials/blog/images_layout.html.twig' with {'blog':page, 'blog_url':blog_url, 'page':child, 'truncate':true} %}
    {% endfor %}

And the layout of this blog display its sub-templates like the one of Quark does. Finally, the tags are displayed with the usual :

{% if page.taxonomy.tag %}
<div class="tags">
    {% for tag in page.taxonomy.tag %}
    <a class="label" href="{{ blog_url|rtrim('/') }}/tag{{ config.system.param_sep }}{{ tag }}#page_content">{{ tag }}</a>
    {% endfor %}
</div>
{% endif %}

The only difference is that this blog is in a modular page.

Tests made (concluding)

(between each attempts, I cleared the cache on the prod server)

  1. Disabling twig cache in the frontmatter : FAIL.
  2. In system.yaml : twig cache false + cache true = FAIL.
  3. In system.yaml : twig cache false + cache false = SUCCEED.
  4. In system.yaml : twig cache true + cache false = SUCEED.
  5. In system.yaml : twig cache true + cache true = FAIL.

And, for clearing the situation about twig use in frontmatter, I did set user_blog_route: '/critiques' and retried :

  1. In system.yaml : twig cache false + cache true = FAIL.
  2. In system.yaml : twig cache false + cache false = SUCCEED.
  3. Doesn't need to go further...
rhukster commented 5 years ago

Thanks for the details on this. However, I'm a little confused by your Twig. It seems you are manually trying to filter the collection based on a tag. However, Grav already automatically filters collections using taxonomy filtering passed in the URL:

https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Page/Page.php#L2694-L2717

So do you have this system.pages.url_taxonomy_filters option turned off? And if so why when you seem to be trying to do the same thing? If you don't have this option turned off, I think your logic is stepping on the toes of the built-in logic.

Heraes-git commented 5 years ago

Hi @rhukster and thanks for the answer. For the twig code, I simply followed the documentation on how to loop over items with a specified taxonomy : https://learn.getgrav.org/16/content/taxonomy#taxonomy-example

<h2>Kevin Smith's Posts</h2>
<ul>
{% for post in taxonomy.findTaxonomy({'author':'ksmith'}) %}
    <li>{{ post.title }}</li>
{% endfor %}
</ul>

I adapted this code to a more restricted logic, given that I only use tags as taxonomy, and never categorie or other type of thing. So, I used : {% set tag = uri.param('tag') %} to say "get the tag specified in the url". Then, I used : {% set request = tag ? taxonomy.findTaxonomy({'tag':tag}) : collection %} to say "if there is tag, find the items, or if there isn't, just use the normal collection un-filtered".

And if there's a tag found, my {% for child in request %} basically do what {% for post in taxonomy.findTaxonomy({'author':'ksmith'}) %} does.

My system.pages.url_taxonomy_filters is turned on, I never changed it.

If I may add something : I think this may be due to my personal set of twig files, but it's unprobable, given that I just imported all what I could find in the Quark theme, relative to blog and taxonomy. There might be a minor change, but not so it could generate the problem. The problem seems tightly related to the cache system. It may be due to a uncomplete twig code in my files, but I don't see where.

It's hard for me to investigate more, because I just have two or three files and basically just what I copied here, that is concerned. So I've been a dozen times around it, and I don't see anything wrong. I'll try to test with a fresh grav install + some manipulations to mirror what I did here and see if it reproduces the bug.

rhukster commented 5 years ago

The example in the docs demonstrates a programatic way to use taxonomy, but it seems what you are trying to do is something that Grav has built-in without the need of custom code. If you download the grav blog skeleton (https://getgrav.org/download/skeletons/blog-site/2.0.0), you will see that the taxonomy filters in action and how they filter the blog listing, by using the built-in capabilties of the collection. I suggest you look into that approach.

Heraes-git commented 5 years ago

Ok, I confirm that the Quark theme just uses a :

{% for child in collection %}
         {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
{% endfor %}

and that it works even in my theme.

But the other thing I confirm, is that using my own way to collect items works in the Quark theme (I tested it). I also noticed that my production server keeps failing to filter things with the Quark's way + cache enabled, and that the Quark way does work with the cache enabled in local. So I conclude that the problem comes from my templates.

The problem is that it means that using doc tutorials, or "exporting" Quarks snippets, isn't enough and the cache conflicts with the process, and alternatively that it is difficult to make the taxonomy to work without copying the right files (or even, the entire files) from the default Quark theme. I will investigate with people's help on Discord, especially those who would have successfully used taxonomy.

Thanks for your time @rhukster ;). I'll bring more information when I'll have some.

PS : I'll download the blog skeleton you provided, and explore it. Thx.

NicoHood commented 3 years ago

@Romarain Any updates on this?