3

Is there a way to invalidate / clear cached content on Cloud CDN from my express server?

For example, if I'm generating server rendered content to make it readily available and I update a specific route from my website, like editing a blogPost, for example. I need to do the following:

export const editBlogPostHandler = (req,res,next) => {
  // 1. UPDATE BLOGPOST WITH SLUG some-blogpost-slug ON DB
  // 2. INVALIDATE /some-blogpost-slug ROUTE ON CLOUD CDN CACHE
  // THIS IS NECESSARY FOR NEW REQUESTS TO GET FRESH DATA RATHER THAN A STALE DATA RESPONSE
};

How can I do that from my express server?


From Cloud CDN - Invalidating Cached Content:

You can invalidate cached content from Cloud CDN through these methods:

  • Using the console:

enter image description here

  • Using gcloud SDK:

enter image description here

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336

2 Answers2

7

There is an API endpoint for that : https://cloud.google.com/compute/docs/reference/rest/v1/urlMaps/invalidateCache

POST https://compute.googleapis.com/compute/v1/projects/{project}/global/urlMaps/{resourceId}/invalidateCache
Jofre
  • 3,718
  • 1
  • 23
  • 31
Alexandre
  • 1,940
  • 1
  • 14
  • 21
0

As a complement to Alexandre accepted answer, here are more details on how to use this endpoint:

POST https://compute.googleapis.com/compute/v1/projects/{project}/global/urlMaps/{resourceId}/invalidateCache
  1. In order to get the resourceId, you can call the endpoint mentioned here in order to get a list of urlMaps resources and their associated ids.

    GET https://compute.googleapis.com/compute/v1/projects/{project}/global/urlMaps

  2. Once you've got the resourceId, you also need to specify the path of the file/folder that you wish to invalidate in the request body (wildcard paths also work):

{ "path": "/folder/file.mp4" }
  1. In the response body, you will find the id of the compute operation - If you want to check this operation progress, you can query it using the Compute Operation Global Get method.

In addition and in order to avoid running the same request several times, it is advised to give a unique requestId parameter under the form of a UUID (as specified in RFC 4122)

schankam
  • 10,778
  • 2
  • 15
  • 26