CorvidReference

wix-site-backend

The wix-site-backend module contains functionality for working with your site and its pages from backend code.

To use the backend site API, import wixSiteBackend from the wix-site-backend module:

   import wixSiteBackend from 'wix-site-backend';

Table of Contents

FUNCTIONS

?
Perform actions on an object.
invalidateCache( )Invalidates the cache for a site.

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>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>

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

Examples

Invalidate a site's cache

import wixSiteBackend from 'wix-site-backend';

export function invalidateCache() {
  return wixSiteBackend.invalidateCache();
}

Get data from a 3rd party service and invalidate the site's cache

/***************************************
 * backend code - myModule.jsw *
 ***************************************/

import wixSiteBackend from 'wix-site-backend';
import {getJSON} from 'wix-fetch';

export function invalidateCache() {
  return wixSiteBackend.invalidateCache();
}

export function fetchData() {
  return getJSON("https://someapi.com/api/someendpoint");
}

let json = fetchData();

// This function is triggered by a 3rd-party service 
// when data changes (for example, via webhook)
export function updateData() {
  json = fetchData();
  invalidateCache();
}

export function getData() {
  return json;
}

/********************
* client-side code *
********************/

import {getData} from 'backend/myModule';

$w.onReady(function () {
  return getData()
    .then( (json) => {
      $w('#myTextElement').text = json.someData[0].description;
    })
    .catch((error) => {
      console.log(error);
    });
}