invalidateCache( )


Developer Preview

This API is subject to change. Bug fixes and new features will be released based on developer feedback throughout the preview period.

Note: This API is only supported when developing a site.

Invalidates or clears previously cached return values based on specified tags.

The invalidationMethods parameter accepts an array of objects, each containing a tag field. These tags are defined when caching return values of web methods (Velo | SDK), and for routers (currently Velo only).

For example, an array such as [{ tag: "contacts" }, { tag: "labels" }] will clear any cached return values tagged with "contacts" or "labels."

Any cache containing at least one tag specified in the invalidationMethods parameter will be cleared.

Important:

  • If you don't specify any tags in the invalidationsMethod parameter, no caches are invalidated.

  • If you don't call Invalidate Cache, caches are only invalidated when the Time to Live (TTL) expires or when the site is republished with a code change.

  • (Velo only) Call Invalidate Cache to invalidate your web method and router caches. To invalidate Server Side Rendering (SSR) caches for your site, use the invalidateCache() function from the wix-site-backend module.

Method Declaration
Copy
function invalidateCache(
  invalidationMethods: Array<InvalidationMethods>,
  options: InvalidateCacheOptions,
): Promise<void>;
Method Parameters
invalidationMethodsArray<InvalidationMethods>Required

An array of objects containing a tag field used to identify the cache to invalidate. All cached return values with any listed tags are invalidated. If no tags are specified, nothing is invalidated.


optionsInvalidateCacheOptions
JavaScript
import { cache } from "wix-cache-backend"; import { webMethod, Permissions } from "wix-web-module"; import { elevate } from "wix-auth"; // Cache the return value export const cachedMultiply = webMethod(Permissions.Anyone, (a, b) => a * b, { cache: { tags: ["multiply", "math"], ttl: 604000, }, }); // Invalidate the cache const invalidationMethods = [{ tag: "multiply" }, { tag: "math" }]; const elevatedInvalidateCache = elevate(cache.invalidateCache); export const invalidateCache = webMethod(Permissions.Anyone, async () => { try { await elevatedInvalidateCache(invalidationMethods); console.log("Successfully invalidated cache."); } catch (error) { console.error(error); // Handle the error } }); /* Promise resolves to void */
Errors

This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.

Did this help?