prerender-redis-cache icon indicating copy to clipboard operation
prerender-redis-cache copied to clipboard

Add delete from cache functionality of Specific URL

Open underscore05 opened this issue 6 years ago • 6 comments

underscore05 avatar Aug 29 '18 05:08 underscore05

It looks like this will just continue on with the normal routes, render the URL again, and then save the resulting HTML back into the redis cache since pageLoaded doesn't check for the req.method again. So in effect this is more like a recache instead of a delete. Is that intended?

This would be handy but I don't know if you'd actually want to expose this since anyone can send that DELETE request. Ideally you'd have a separate way of deleting pages from your cache that doesn't involve hitting the URL through your server.

thoop avatar Aug 29 '18 14:08 thoop

I think it will not continue because of the following line.

        //
        if (req.method !== 'GET' || !redisOnline) {
            return next();
        }

And maybe a header with secret-token will suffice securing that delete functionality? Though, I don't know how to do that either.

underscore05 avatar Aug 29 '18 14:08 underscore05

The next() there just means it would skip over the section of code that would return a page from the redis cache. Calling next() will let the Prerender server render the page, which then calls the pageLoaded event and, in this case, saves the content back into the redis cache. So right now you can use a POST request to recache the page by skipping redis, rendering the URL, and then saving the result into redis. The code you added just deletes the page from redis before it gets recached and saved back into redis.

For example, the code already in that file will try to find the result from redis, and if found, return the html using res.send(). If the response is not cached in redis, it will call next() so that the Prerender server can render the page and save it into the redis cache:

client.get(req.prerender.url, function (error, result) {
  if (!error && result) {
    var response = JSON.parse(result);
    var headers = response.headers;
    var key;

    for (key in headers) {
      if (headers.hasOwnProperty(key)) {
        res.setHeader(key, headers[key]);
      }
    }
    res.send(response.statusCode, response.content);
  } else {
    next();
  }
});

thoop avatar Aug 30 '18 02:08 thoop

Hmm... does it mean that POST request will still called by prerenderer and be stored to cache if it returns a 200 status?

underscore05 avatar Aug 30 '18 02:08 underscore05

So right now you can use a POST request to recache the page by skipping redis, rendering the URL, and then saving the result into redis. 

ohhhh......

underscore05 avatar Aug 30 '18 02:08 underscore05

I have actually gone ahead and implemented this as it's heavily requested, and would leave it up to the responsibility of the proxy that (hopefully) exposes prerender to block this from external calls if this is undesirable.

@thoop I'd appreciate the feedback, so if you think this isn't the right approach, do let me know.

At the moment it sits on branch v0.3.0, and will await a bit of feedback before it's merged and released.

JonathanBennett avatar Apr 19 '20 22:04 JonathanBennett