Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Related items going from disabled to live status does not clear cache of its relations. #555

Closed
eddiedale opened this issue Sep 11, 2023 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@eddiedale
Copy link

Steps to reproduce the issue.
(assuming blitz installed and activated)

We have Page 1 (home page) and Page 2 (Blog post)

  1. Add a related field to the home page (Related blog posts)
  2. Add a new blog post, but keep it inactive
  3. Add this post via the related field on home page
  4. Save the home page (all is fine this far)
  5. Thereafter activate the blog post
  6. The home page will not be updated.

This is unexpected behaviour. The same does not happen the other way around.
ie. If a related blog post is listed on the home page, but gets deactivated it will clear the cache of the home page.

So the expected behaviour would be that activating a previously deactivated blog post should clear the cache of the home page.

The plugin and Craft version numbers.
Blitz 4.5.2
Craft Pro 4.4.17
PHP 8.2.8

@eddiedale eddiedale added the bug Something isn't working label Sep 11, 2023
@bencroker
Copy link
Collaborator

That is indeed unexpected behaviour. Can you please show me the code you are using to output the related blog posts on the homepage?

@eddiedale
Copy link
Author

index.twig


{% set selected_events = entry.selectedEvents
  .status('live')
  .with([
      ['image', { 
        withTransforms: ['small', 'medium']
      }]
  ])
  .all() 


………


{% for item in selected_events %}
    {% if loop.first %}
        {% include '_includes/_event_card' with {item: item} %}
    {% endif %}
{% endfor %}
%}

_event_card.twig

<a href="{{ item.url }}">
{% if item.image|length %}
etc…

@eddiedale
Copy link
Author

eddiedale commented Sep 11, 2023

Probably a very hard-handed solution, but right now I refresh cache on all entry saves as a solution.

Basically most of the content edits will somehow affect the home page anyway in this particular site.

Using a "clear the cache, regenerate manually or organically". Flushes the cache more often than I'd like, but atleast it solves the issue at hand. The client was sitting and waiting for the changes to show up on the home page 😅

 Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function (ModelEvent $event) {

        $entry = $event->sender;

        Blitz::$plugin->refreshCache->refreshAll();
    }

);

@bencroker
Copy link
Collaborator

bencroker commented Sep 11, 2023

I think I’ve gotten to the bottom of this and will work on a fix in the coming days.

In the meantime, refreshing only the homepage might be a better approach.

use craft\base\Element;
use craft\elements\Entry;
use craft\events\ModelEvent;
use putyourlightson\blitz\Blitz;
use putyourlightson\blitz\models\SiteUriModel;
use yii\base\Event;

Event::on(Entry::class, Element::EVENT_AFTER_SAVE, function(ModelEvent $event) {
    /** @var Entry $entry */
    $entry = $event->sender;
    if ($entry->slug === 'homepage') {
        Blitz::$plugin->refreshCache->refreshSiteUris([
            new SiteUriModel([
                'uri' => $entry->uri,
                'siteId' => $entry->siteId,
            ]),
        ]);
    }
});

@bencroker
Copy link
Collaborator

bencroker commented Sep 18, 2023

It turns out this is a limitation rather than a bug. Because relation field queries can be eager-loaded but don’t necessarily have to be, tracking them is unreliable and is intentionally excluded.

// Don’t proceed if this is a relation field query
if (ElementQueryHelper::isRelationFieldQuery($elementQuery)) {
return;
}

I’m exploring how to get around this by tracking elements with any status (enabled and disabled) so that the desired behaviour can be achieved, and I expect to have an update later this week.

@eddiedale
Copy link
Author

Fair enough. I guess it's kind of an edge case, and could be solved by changing their workflow. But it feels better to be able to adjust to their workflow than otherwise.

Since they basically first add the inactive entry to the homepage, and then later activate it, it doesn't work with your suggestion to only update the homepage when that is saved (because it isn't saved again), but I used your suggestion to only also update the homepage on entry saves. That at least avoids refreshing the whole shabam every time :-)

use craft\base\Element;
use craft\elements\Entry;
use craft\events\ModelEvent;
use putyourlightson\blitz\Blitz;
use putyourlightson\blitz\models\SiteUriModel;
use yii\base\Event;

Event::on(Entry::class, Element::EVENT_AFTER_SAVE, function(ModelEvent $event) {
    /** @var Entry $entry */
    $entry = $event->sender;

    Blitz::$plugin->refreshCache->refreshSiteUris([
        new SiteUriModel([
            'uri' => '/',
            'siteId' => 1,
        ]),
    ]);

});

@bencroker
Copy link
Collaborator

Yup, that’ll do it!

@bencroker bencroker self-assigned this Oct 10, 2023
@bencroker
Copy link
Collaborator

bencroker commented Oct 15, 2023

I managed to fix this in 3296e91 for the next release. If you can help by testing then that would be much appreciated!

You can test this by running composer require "putyourlightson/craft-blitz:dev-develop as 4.6.0".

@eddiedale
Copy link
Author

@bencroker I can confirm that this works in our use case. 🙌

@bencroker
Copy link
Collaborator

Glad to hear it!

@bencroker
Copy link
Collaborator

bencroker commented Oct 17, 2023

Released in 4.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants