BobbyWibowo / lolisafe

Blazing fast file uploader and awesome bunker written in node! 🚀
MIT License
329 stars 56 forks source link

A script to purge cache of HTML pages #15

Closed BobbyWibowo closed 5 years ago

BobbyWibowo commented 5 years ago

Hmm, now that we're actively caching HTML pages with Cloudflare (as in the homepage itself, dashboard, and faq - actually pretty much only those 3 in our use case), we need a script to call Cloudflare's API to purge cache of those pages on every update (perhaps even allow it to read config.js to find out extra enabled pages and include those to the request).

I think I also still need to make sure the current Cache-Control for HTML pages will still be able to deliver the latest change ASAP. I think it already do, but just a self-reminder.

Now for those who isn't aware, the last few commits were dedicated into making safe.fiery.me-exclusive Cache-Control, mainly to cache HTML pages (the frontend). It's to ensure the frontend itself loads as fast as possible anywhere on the planet Earth. API calls are not cached, so those will still lag behind in those faraway countries, but having the pages load fast should be desirable to overall user experience.

At the moment the frontend is being cached for about 12 hours. After making this script, I presume it should be fine to raise that to 30 days right away? Not sure though, still need to see if browsers are truly respecting must-revalidate Cache-Control or not, and how often do they do it.

BobbyWibowo commented 5 years ago

Seems like I had completely wrong knowledge of this whole Cache-Control thing (if not all, then most). I've just read some articles and I think I've finally figured out the proper way to achieve what I want. I'll make a commit in a few hours after some more learning and testing, I think.

BobbyWibowo commented 5 years ago

Ok, https://github.com/BobbyWibowo/lolisafe/commit/b431d11ebad8540f9a1934cb2eea1d31372e0ff3 does it. Basically, now proxy server will cache those HTML frontends for 30 days, but clients/browsers will not. From what I can tell, clients will still cache them, but clients will use ETag to check whether the file in the proxy server has changed, and if not, skip download. And, since all that needs to be done is just to connect to proxy server, as in, not having to wait for proxy server to fetch from origin server, First Time Byte (FTB) is great worldwide. Then when it comes to having to update, the new yarn cfpurge script is all that needs to be run to purge cache of those HTML frontends in proxy server. I even made a shortcut script named yarn pm2restart to restart PM2 of the safe then run the purge script (so my workflow is pretty much only yarn pull; yarn pm2restart, huh).