Search.../

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.

Authorization

Request

This endpoint does not take any parameters

Response Object

Fulfilled - When the site cache invalidation request is accepted. Rejected - An error message.

Returns an empty object.

Status/Error Codes

Was this helpful?

Invalidate a site's cache

Copy Code
1import wixSiteBackend from 'wix-site-backend';
2
3export function invalidateCache() {
4 return wixSiteBackend.invalidateCache();
5}
Get data from a 3rd party service and invalidate the site's cache

Copy Code
1/***************************************
2 * Backend code - myModule.jsw *
3 ***************************************/
4
5 import wixSiteBackend from 'wix-site-backend';
6 import { getJSON } from 'wix-fetch';
7
8 export function invalidateCache() {
9 return wixSiteBackend.invalidateCache();
10 }
11
12 export function fetchData() {
13 return getJSON("https://someapi.com/api/someendpoint");
14 }
15
16 let json = fetchData();
17
18 // This function is triggered by a 3rd-party service
19 // when data changes (for example, via webhook)
20 export function updateData() {
21 json = fetchData();
22 invalidateCache();
23 }
24
25 export function getData() {
26 return json;
27 }
28
29 /********************
30 * Client-side code *
31 ********************/
32
33 import { getData } from 'backend/myModule';
34
35 $w.onReady(function () {
36 return getData()
37 .then((json) => {
38 $w('#myTextElement').text = json.someData[0].description;
39 })
40 .catch((error) => {
41 console.log(error);
42 });
43 }