Cache and limit direct access to the data managed on your Directus instance, powered by Cloudflare Workers, Workers KV and Cloudflare Pages.
- Always available API with live updates1.
- Origin Priority mode to serve content from your Directus instance, with fallback to the KV cache when unavailable.
- Cache Priority mode to serve content from the KV cache, with fallback to your Directus instance when unavailable.
- Origin Only mode to only serve content from your Directus instance.
- Cache Only mode to only serve content from the KV cache.
- Caching of image assets.
- Automatic updating of cache upon webhook trigger.
- Preset requests to prevent user from the tampering of query parameters.
- Full support of Directus Global Query Parameters.
- Demo Vue web application to showcase functionality.
-
Head over to the Directus instance at https://cztkclnq.directus.app.
-
Login with the following credentials.
- Email:
[email protected] - Password:
abcd1234
- Email:
-
Create > update > delete your content.
Permissions have been set to prevent changes to certain items.
-
Check out your updates at https://directus-cf-cache.pages.dev.
demo.mov
Fetching and storing content into Workers KV.
Serve requests either from the origin or the KV cache.
Store cached content retrieved from Directus.
Host the demo Vue 3 web application.
Create a Fork of this GitHub repository as the deployments are pushed via GitHub Actions.
- Sign up for a Cloudflare Workers account.
- Copy the Account ID shown in the Workers page somewhere.
- Copy the Subdomain shown in the Workers page somewhere.
- Head over to KV tab, add a new namespace and copy the Namespace ID somewhere.
- Head over to API Tokens and create a new Custom API Token with Permission of "Account/Worker Scripts/Edit".
- Copy the API Key somewhere, you will require it later.
- If you already have a Directus instance, self-hosted or cloud, skip to point 5.
- Sign up for a Directus Cloud account.
- Create a new Project.
- Check your email for the login credentials.
- Login to your Directus instance.
- Head over to Settings > Data Model.
- Set up all your required collections, in the demo there are
articles,projectsandpeople. - Create a new collection with the following settings:
- Set name as
cf_cache_options - Check the singleton checkbox
- Skip the configuration of other parameters and save.
- Set name as
- Click on Data Model.
- Create a new collection with the following settings
- Set name as
global_query_params - Skip the configuration of other parameters and save.
- Set name as
- Create the following fields in
global_query_params:descriptionwithstringtypeoperationwithstringtype (eitherlistorget)collectionwithstringtype (collection names that you want to cache)keywithstringtype (cache key to differentiate between presets)enabledwithbooleanquery_paramswithjson(the query that will be sent with the request)file_pathswithcsv(the paths containing files from the result)sortwithintegerto be used for sorting
- Create the following fields in
cf_cache_options:cache_modewithstring(origin_priority,cache_priority,origin_only,cache_only)preset_requestswithOne to Many Relationshiponglobal_query_paramswith a foreign key ofcf_cache_option.- Continue in advanced field creation mode.
- Click on
Relationshipand set the sort field assortto enable sorting. - Skip the configuration of other parameters and save.
- Head over to Settings > Data Model
- Click on the kebab menu of
global_query_params. - Select Make Collection Hidden.
- Click on the kebab menu of
- Head over to Settings > Webhooks
- Create a new webhook and give it a name.
- Set the URL as your Cloudflare Worker webhook endpoint.
- Add a new request header
Secretwith your secret and copy this value somewhere. - Toggle all actions to be turned on (create, update, delete).
- Select the collections that you wish to cache including
Cf Cache Options
- Head over to Settings > Roles & Permissions
- Create a new role and uncheck
App Access. - Allow
All AccessforReadcolumn of the tables you wish to cache includingcf_cache_options - Create a new user, enter a secure token and copy this value somewhere.
- Save all changes.
- Create a new role and uncheck
- Head over to Content > Cf Cache Options and configure your required caching settings.
Demo
cf_cache_optionsconfigurationDemo
preset_requestsconfiguration
- Sign up for a Cloudflare Pages account.
- Create a new project, selecting the fork of
directus-cf-cachein your GitHub account. - Set the build output directory as
dist. - Set the root directory path as
app. - Create the following environment variables:
VITE_DIRECTUS_CF_CACHE_URLwith your Cloudflare Worker URL such ashttps://directus-cf-cache.<your workers subdomain>.workers.dev.- After the first build is completed, pause the Automatic git deployments.
- Add a new deploy hook, copy the Deploy hook URL somewhere.
-
Open the forked GitHub project > Settings > Secrets.
-
Create the following repository secrets:
You may refer to .env.sample for sample values.
CLOUDFLARE_ACCOUNTIDwith the Account ID.CLOUDFLARE_PAGES_WEBHOOK_URLwith the Deploy hook URL.CLOUDFLARE_TOKENwith the API Key.CLOUDFLARE_ZONEIDwith the Zone ID, empty string unless you are using a custom domain.DIRECTUS_ACCESS_TOKENwith the token.DIRECTUS_CF_CACHE_KVwith the Namespace ID.DIRECTUS_SERVER_URLwith the root URL of your Directus instance.DIRECTUS_WEBHOOK_SECRETwith the secret.WORKER_ROUTESwith your custom domain or the workers.dev route.
-
Click on the Actions tab, manually run the workflows.
- Paging of
listresults to handle bigger datasets - Integration with Cloudflare R2 for assets storage
- Utilize Directus Flows instead of Webhooks for a "push" approach
- Cache management API to manage content stored in the KV cache
- Have an idea? Open a discussion!
Developed by licitdev
Footnotes
-
Changes may take up to 60 seconds to propagate globally when the request is served from the KV cache instead of the origin https://developers.cloudflare.com/workers/learning/how-kv-works ↩

