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:
Import window from the Site Window API.
Check the current mode with viewMode() before accessing the browser API:
Import window from the Site Window API.
Set a flag using viewMode() to indicate if the current mode is site mode:
Use the flag before accessing browser APIs: