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.

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:

Promise<void>

Was this helpful?

Invalidate a site's cache

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import wixSiteBackend from 'wix-site-backend';
3
4export const invalidateCache = webMethod(Permissions.Anyone, () => {
5 return wixSiteBackend.invalidateCache();
6});
Get data from a 3rd party service and invalidate the site's cache

Copy Code
1/***************************************
2 * Backend code - myModule.web.js *
3 ***************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import wixSiteBackend from 'wix-site-backend';
7import { getJSON } from 'wix-fetch';
8
9export const invalidateCache = webMethod(Permissions.Anyone, () => {
10 return wixSiteBackend.invalidateCache();
11});
12
13export const fetchData = webMethod(Permissions.Anyone, () => {
14 return getJSON("https://someapi.com/api/someendpoint");
15});
16
17let json = fetchData();
18
19// This function is triggered by a 3rd-party service
20// when data changes (for example, via webhook)
21export const updateData = webMethod(Permissions.Anyone, () => {
22 json = fetchData();
23 invalidateCache();
24});
25
26export const getData = webMethod(Permissions.Anyone, () => {
27 return json;
28});
29
30/********************
31 * Client-side code *
32 ********************/
33
34import { getData } from 'backend/myModule.web';
35
36$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});