JonathanBennett / prerender-redis-cache

MIT License
32 stars 30 forks source link

Add delete from cache functionality of Specific URL #16

Open underscore05 opened 5 years ago

thoop commented 5 years ago

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.

underscore05 commented 5 years ago

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.

thoop commented 5 years ago

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();
  }
});
underscore05 commented 5 years ago

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

underscore05 commented 5 years ago
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......

JonathanBennett commented 4 years ago

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.