invalidateCache( )
Invalidates the cache for a site.
Description
Calling the invalidateCache()
function submits a request to invalidate (clear) the site cache.
The function returns a Promise that resolves when the invalidation request is accepted (or rejected). Note that the
invalidation process normally takes a few seconds to propagate.
Most Wix sites are cached by default to provide better performance. In cases where your site changes without publishing, caching may adversely affect your site. For example, if your site fetches and displays data from a 3rd-party service, you might experience the following issues on a cached site:
- Updated data is not displayed.
- Updated data is not exposed to bots, hurting SEO.
You can prevent such issues by invalidating your site's cache following a data update.
Syntax
function invalidateCache(): Promise<void>
invalidateCache Parameters
This function does not take any parameters.
Returns
Fulfilled - When the site cache invalidation request is accepted. Rejected - An error message.
Return Type:
Was this helpful?
1import { Permissions, webMethod } from 'wix-web-module';2import wixSiteBackend from 'wix-site-backend';34export const invalidateCache = webMethod(Permissions.Anyone, () => {5 return wixSiteBackend.invalidateCache();6});
1/***************************************2 * Backend code - myModule.web.js *3 ***************************************/45import { Permissions, webMethod } from 'wix-web-module';6import wixSiteBackend from 'wix-site-backend';7import { getJSON } from 'wix-fetch';89export const invalidateCache = webMethod(Permissions.Anyone, () => {10 return wixSiteBackend.invalidateCache();11});1213export const fetchData = webMethod(Permissions.Anyone, () => {14 return getJSON("https://someapi.com/api/someendpoint");15});1617let json = fetchData();1819// This function is triggered by a 3rd-party service20// when data changes (for example, via webhook)21export const updateData = webMethod(Permissions.Anyone, () => {22 json = fetchData();23 invalidateCache();24});2526export const getData = webMethod(Permissions.Anyone, () => {27 return json;28});2930/********************31 * Client-side code *32 ********************/3334import { getData } from 'backend/myModule.web';3536$w.onReady(function () {37 return getData()38 .then((json) => {39 $w('#myTextElement').text = json.someData[0].description;40 })41 .catch((error) => {42 console.log(error);43 });44});