Given that I have a page element loaded via Authcache, is it possible to trigger the update of this page element with some other js on the page.
Basically I have an ajax call that updates some value on the server, and I'd like to update the page elements handled by authcache once that has happened.

Comments

alanburke created an issue. See original summary.

znerol’s picture

What is triggering the the Ajax action? Is it controlled by a page element which is inside or outside the fragment in question? Is it controlled by some other means (e.g. a timer)?

alanburke’s picture

Another piece of js will trigger the update.
It is outside of the fragments to be updated.
It will send a json request, which will mean that the fragment contents should be invalidated, and ideally refreshed.

znerol’s picture

There is no "official" support for that use case in authcache. You might be able to work around that in the following way:

  1. Create a fake DOM element with the placeholder markup, e.g. something like $('<span class="authcache-ajax-frag" data-p13n-frag="block/commerce_cart-cart"></span>')
  2. Insert it at the appropriate place in the DOM
  3. Call Drupal.attachBehaviors() on the parent node.
alanburke’s picture

So that all works really well, except the the URL response is cached in authcache_ajax.js.

So I just need a way to clear that cache, or avoid it being set in the first place.

alanburke’s picture

Is there any way to address that cache object from another js file?
So I could clear it, before calling Drupal.attachBehaviors()?

znerol’s picture

Call authcache_p13n_session_invalidate() as part of your Ajax callback. This will result in the cache busting cookie (aucp13n) being set to a new random value.

znerol’s picture

Oh, wait. There is a static cache in authcache_ajax.js. So the cache busting cookie will not help.

znerol’s picture

Consider the following function (taken from authcache_ajax.js):

  function authcacheGet(url, callback, type) {
    if (cache.hasOwnProperty(url)) {
      callback(cache[url]);
    }
    else if (pending.hasOwnProperty(url)) {
      pending[url].push(callback);
    }
    else {
      pending[url] = [callback];

      $.ajax({
        url: url,
        data: {v: $.cookie('aucp13n')},
        dataType: type,
        // Custom header to help prevent cross-site forgery requests.
        beforeSend: function(xhr) {
          xhr.setRequestHeader('X-Authcache','1');
        },
        success: function(data, status, xhr) {
          cache[url] = data;
          $.each(pending[url], function() {
            this(data);
          });
          delete pending[url];
        }
      });
    }
  }

I guess it would be possible to make your use-case work if the v parameter would be added to the URL before checking the static cache.

alanburke’s picture

I don't think so.
The cache key doesn't use the v parameter.
What If I reworked it so that the cache was part of the Drupal object.
Then I could address it from elsewhere.
Would a patch that does that be considered?

znerol’s picture

The cache key doesn't use the v parameter.

Yes, that's what I tried to express above, and yes, patches are welcome :)

alanburke’s picture

Component: Miscellaneous » Code
Assigned: Unassigned » alanburke
Category: Support request » Feature request
znerol’s picture

Status: Active » Needs review
StatusFileSize
new793 bytes

If cache-busting does not affect the static cache, then this could be considered a bug. How about just nuking the cache if the cache-busting cookie changed in the meantime?

Status: Needs review » Needs work

The last submitted patch, 13: trigger_update_of_page-2653454-13.patch, failed testing.

alanburke’s picture

Status: Needs work » Needs review
StatusFileSize
new1013 bytes

Status: Needs review » Needs work

The last submitted patch, 15: authcache-global-url-cache-2653454-13.patch, failed testing.

alanburke’s picture

Not sure what the fail means? Any clues?

znerol’s picture

The test fail is not related to this patch. I've seen that before in other issues. Although very strange that the branch tests is not affected.

As for the patch, I'd prefer something which does not leak internals. I'd like to avoid that future changes to the static cache will break existing sites. Can you take a look at #13 and report back whether it addresses your use-case?

alanburke’s picture

I had missed that patch - that works for my use case.
I set the cookie to a new random value, and this invalidates the cache.
So that works for me

  • znerol committed 5367d5e on 7.x-2.x
    Issue #2653454 by alanburke, znerol: Trigger update of page fragment...
znerol’s picture

Committed #13. Thank you for reporting this issue and for actively working on a solution.

znerol’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gunwald’s picture

Could someone point out please, what the JS would look like, to manually reload/refresh a block. As an non pro I found the authcache_ajax.js very hard to read.