Handle Sandboxing in Editor Mode

Follow these steps to handle sandboxed site widgets and site plugins while in the editor and in preview mode. These steps ensure that your code accesses browser APIs only when available, preventing runtime and logical errors. When APIs aren't available, mock the storage or modify your API usage accordingly.

Note: The code examples in this task use the Web Storage API localStorage property. However, you can apply these steps to all sandboxed browser APIs and properties. If you are working with the cookies API, make sure to comply with GDPR and data protection regulations.

To adapt your code for the editor, you can:

Option 1 | Check the current mode before a single API call

  1. Import window from the Site Window API.

  2. Check the current mode with viewMode() before accessing the browser API:

    Copy

Option 2 | Create a reusable flag for multiple API calls

  1. Import window from the Site Window API.

  2. Set a flag using viewMode() to indicate if the current mode is site mode:

    Copy
  3. Use the flag before accessing browser APIs:

    Copy

See also

Did this help?