Skip to content

Instantly share code, notes, and snippets.

@robindiddams
Last active February 7, 2022 20:39
Show Gist options
  • Save robindiddams/d5d45f9ee54ffd151354811910425b8f to your computer and use it in GitHub Desktop.
Save robindiddams/d5d45f9ee54ffd151354811910425b8f to your computer and use it in GitHub Desktop.

Use Google cloud api to invalidate cdn cache

You want to do this in code: Screen Shot 2022-02-07 at 2 35 19 PM

Do it in TypeScript:

First npm i googleapis (I'm using v68.0.0)

import { google } from 'googleapis';

const compute = google.compute('v1');

const urlMapName = 'my-cdn-name';

const invalidateCache = async (path: string) => {
	const authClient = await google.auth.getClient({
		scopes: ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/compute'],
	});
	const projectId = await google.auth.getProjectId();
	await compute.urlMaps.invalidateCache({
		auth: authClient,
		project: projectId,
		urlMap: urlMapName,
		requestBody: {
			path,
		},
	});
};

call like this: await invalidateCache('/my/route')

obvi you need client credentials configured

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment