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

Refresh entry button inside entry #593

Closed
josh-attwood opened this issue Dec 12, 2023 · 4 comments
Closed

Refresh entry button inside entry #593

josh-attwood opened this issue Dec 12, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@josh-attwood
Copy link

Hi there!

We are having some issues occasionally with blitz not refreshing certain pages when saved which I think is just fluky. The main problem is that we then get emails from clients confused as to how to refresh the cache for a particular page. It would be amazing if they could refresh the cache from within an entry through some kind of button in the sidebar, it would definitely save us a lot of hassle!

@josh-attwood josh-attwood added the enhancement New feature or request label Dec 12, 2023
@bencroker
Copy link
Collaborator

bencroker commented Dec 13, 2023

This is an interesting idea, but it feels like a detail that non-technical users shouldn’t have to think about. I’d start by exploring why those pages are not refreshed and fix that, which is a more robust and long-term solution. Also, if you really want to allow users to refresh specific cached pages then you already can, via the cache utility.

@pixelmachine
Copy link

pixelmachine commented Dec 20, 2023

@josh-attwood obviously Ben is right but I also had a site that I just didn't have time to really dive into in order to figure out why the cache wasn't clearing. Instead I made a quick button like you described.

I added an API key in the Blitz advanced settings, added a tab to the entry type, called Cache, then added a template UI element like this:

<p class="light">NOTE: Save before refreshing!</p>
<a class="btn submit" 
   id="refresh-this-page-cache" 
   href="{{ siteUrl }}actions/blitz/cache/refresh-urls?key=[API_KEY]&urls={{ element.url }}">
   Refresh {{ element.title }}
</a>

{% js %}
let cacheBtn = document.getElementById('refresh-this-page-cache');
let cacheMsg = document.getElementById('notifications');

cacheBtn.addEventListener('click', function(e) {
  e.preventDefault();

  let notification = document.createElement("div");
  notification.className = "notification notice";
  notification.textContent = "Refreshing..."
  cacheMsg.appendChild(notification);

  var request = new XMLHttpRequest();
  request.open('GET', cacheBtn.href, true);

  request.onload = function() {
    if (this.status >= 200 && this.status < 400) {
      // Success!
      var data = JSON.parse(this.response);
      notification.textContent = data.message
    } else {
      notification.textContent = "There was an error, try again or refresh the cache via Utilities"
    }

    window.setTimeout(function() {
      $(notification).fadeOut();
    }, 5000);
  };

  request.onerror = function() {
    notification.textContent = "There was an error, try again or refresh the cache via Utilities";
    window.setTimeout(function() {
      $(notification).fadeOut();
    }, 5000);
 };

  request.send();
}, false);
{% endjs %}

Hopefully I'll get back to the site to remove this at some point but the client has been using it for a couple of years with no issues. Just watch out for that lazy jQuery fadeOut() and the #notifications element – I haven't tried this on Craft 4 or more recent versions of Blitz so I don't know if they'll still work.

@bencroker
Copy link
Collaborator

Thanks for sharing, @pixelmachine.

@bencroker
Copy link
Collaborator

@josh-attwood @pixelmachine FYI I’ve opened a PR to address this at #690, feel free to take a look and offer any suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants