Server cache (products and app config)¶
/api/products and /api/init are cached per locale with defineCachedFunction (server/utils/wp/products.ts and server/utils/wp/config.ts). Entries are refreshed after 5 minutes. With swr, the old entry is served while the new one is fetched.
Where the cache lives¶
| Setup | Storage |
|---|---|
Pages project has a KV binding named CACHE_KV | Workers KV, shared by all Worker instances |
| No binding (local dev, or binding not added yet) | Memory, per Worker instance |
server/plugins/kv-cache.ts checks for the binding on the first request in each Worker instance and moves Nitro's cache storage to KV if it is there. No code change or env var is needed.
Why KV: Cloudflare runs many short-lived Worker instances. With the memory cache, most of them start empty and call WordPress directly. When WordPress fails or is slow, the page renders without products (price 0, no colour picker). With KV, almost every request gets a cached entry.
Setting up the binding¶
Do this once for each Pages project (elome-nuxt-master and elome-nuxt-dev):
- Cloudflare → Storage & Databases → KV → create one namespace per project, e.g.
elome-nuxt-cacheandelome-nuxt-cache-dev. - Pages project → Settings → Bindings → add a KV namespace binding with the variable name
CACHE_KVfor the Production environment, and pick that project's namespace. Leave it out of Preview (or give Preview its own namespace). Cache entries from different deployments of the same code are treated as valid, so a preview pointing at another WordPress would otherwise write its prices into the production cache. - Redeploy the frontend. Bindings only take effect on a new deployment.
Notes:
- KV writes are counted, and the Workers Paid plan is needed (the free plan allows 1,000 writes a day). Every Worker instance that sees an expired entry refreshes and writes it, so expect more than one write per locale per 5 minutes. KV also allows only one write per second per key; extra writes fail with a 429 that is logged as
[cache] Cache write errorand is harmless. - KV is eventually consistent. After an entry is deleted, other regions can serve the old entry for up to about 60 seconds.
- Entries have no expiry. A stale entry is served once and refreshed in the background, so after a quiet period the first visitor can see data that is older than 5 minutes. A deploy does not clear the cache.
Clearing the cache¶
There is no API endpoint for this. With the CACHE_KV binding, delete the entries in Cloudflare. The keys look like this:
- Dashboard: Storage & Databases → KV → the namespace → find the key → delete.
- Wrangler:
npx wrangler kv key delete --namespace-id <NAMESPACE_ID> --remote "nitro:functions:products:de-de.json"
The next request fetches fresh data from WordPress. Other regions can serve the old entry for up to about 60 seconds.
Without the binding the cache is in memory per Worker instance and cannot be cleared from outside. Wait up to 5 minutes, or redeploy.
If products still fail to load¶
app/plugins/products-init.ts fetches products in the browser, with one retry, when the server render did not include them.