Skip to content

UI builder - Studio API

Storage

Page storage vs. UI storage

Two different things are called storage in a UI script, and they are easy to confuse.

Api.page.storage - documented on this page - is data kept inside the page. It is stored in the page payload and is written back to the server when the page is saved, so it travels with the page and every client that opens that page sees it.

globalStorage, localStorage and sessionStorage are script globals, available in any UI script without going through Api. They live in the operator's browser, not in the page, and they never leave the machine they were written on:

Global Scope Survives a refresh Survives a restart
globalStorage Shared by every UI on this machine Yes Yes
localStorage This UI only, on this machine Yes Yes
sessionStorage This browser tab Yes No - it dies with the tab

Each of the three is a Storage-shaped object with length, getItem, setItem, key, removeItem and clear, plus on, once and off to subscribe to changes of a key. See Classes for the full type.

Warning

These three are close to, but not the same as, the browser's own Storage. getItem returns undefined - not null - for a key that was never set, and so does key(index) when the index is out of range. setItem silently discards a value that is not a string: setItem('k', 5) does nothing and reports no error.

Api.page.storage

To execute Storage APIs, take a look at the examples below:

Api.page.storage.get('MyKey');
Api.page.storage.set('MyKey', value);
Api.page.storage.set({ key1: 'value1', key2: 'value2' });
Api.page.storage.remove('key');

Warning

Writing to page storage from a script does not mark the page as changed. The operator gets no "unsaved changes" prompt, so the write is lost if the page is left without being saved. Save the page yourself with Api.page.save() after writing, if the value has to survive.

Values must be JSON-serialisable - set and assigning to value both throw on a circular object. set does nothing when there is no page payload.

get

Retrieves the Key from the storage

Api.page.storage.get(key: string): any;
Parameter Type Description
key string Gets the value of the key provided

set

Sets the Key and store it to the page

Api.page.storage.set(key: string, value: any): PageStorage;
Parameter Type Description
key string Sets the value of the key provided
value any The value of the key to be stored

To set multiple key values, use:

Api.page.storage.set(object: Record<string, any>): PageStorage;

for example:

Api.page.storage.set({ key1: 'value1', key2: 'value2' }); 
// This will add the new key values but not overwrite the whole object

set returns the storage object itself, so calls can be chained.

remove

Removes a key from the Storage

Api.page.storage.remove(key: string): PageStorage;
Parameter Type Description
key string The key to be removed

clear

Clear all storage objects from the page

Api.page.storage.clear(): PageStorage;

value

Gets or replaces the whole page storage object

Api.page.storage.value: Record<string, any>;    // read/write

Assigning to value discards every existing key and puts your object in its place. Use set(object) when you want to merge instead - that is the only difference between the two.

keys

Get all available keys in page storage

Api.page.storage.keys(): string[];