Purge Cloudflare Pages Functions Cache

I have build an API with Cloudflare Pages Functions to get data from Faunadb. it’s cool that Cloudflare Pages Functions has file based routing, middlewares system & more …

I have added a top-level middleware to set CORS Headers & Cache Responses using Cloudflare Workers Cache API & it’s working fine, but purging the cache is not something that I am be able to do, because there’s no ways to purge the Cloudflare Cache in a Free Account.

Are there any methods that I can use to purge the Cache. Thanks.

What do you mean? If you are using a custom domain you should be able to clear the cache of that zone (though I have not tested this with Pages Functions ) - are you perhaps trying to clear the cache of a pages.dev zone? Because I believe that is not possible from the dashboard.

You could call caches.delete to remove a specific request from cache, but not purge it all unless on a custom domain zone.

to be clear, I’m not using a custom domain, it’s just in default pages.dev domain.
So I should have to Setup a route to clear the cache based the cache key. Great, I will try it.

are there any examples how to do these?

Thanks, I am be able to create a purge route & delete the cache by using URL.

// purge/[[path]].ts


import { json } from "../utils/json";

export const onRequestGet: PagesFunction = async ({ request }) => {
  try {
    const url = new URL(request.url);
    const href = url.href;

    const cacheURL = href.replace("purge", "");

    const cache = await caches.open("resources:cache");
    const isDeleted = await cache.delete(cacheURL);

    return json({ cacheCleared: isDeleted });
  } catch (error) {
    return json({ error: "Something Went Wrong!" });
  }
};

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.