This article was posted over a year ago. The information below could be outdated.
In the last post about deploying Now with Cloudflare, I have been describing in general how to deploy a project using Now and how to integrate with Cloudflare. After success deploying, if you set Cloudflare cache to make your website faster I guess you will face issues related to a cache.
First, if you push or make changes, your website does not instantly follow the changes. From my experience that is because of cache! So to handle this case we need to purge every time we make a change and Git has a handy feature to make it automatically, which call hooks.
Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature - no need to download anything. Git hooks are run locally.
To use this feature you only need to create a shell script and make it executable then put it in the hooks directory in .git on a project root. So that will be like this:
cd my_project/.git/hooks touch pre-commit chmod +x pre-commit
pre-commit, we can add shell script to call Cloudflare API like this:
As you can see, we have separated the config file, to make the script work, we also need to create a configuration file in the user directory.
cd ~ touch .cloudflare_config_muhfajar.blog
And add detail about the Cloudflare configuration
CF_API_KEY=some-api-key-from-cloudflare [email protected] CF_ID=cloudflare-zone-id
To generate an API key in Cloudflare, please refer to this link. After all is complete, now you can deploy and also tell Cloudflare to purge your cache, so your web visitor always gets the latest data.