syncInternalState to store your app’s view state in the customAppInternalState URL search param. This makes the URL shareable. Anyone who opens it sees the same view. Cognite Data Fusion (CDF) passes the stored value back as initialState the next time the app mounts.
Prerequisites
Before you start, ensure you have:- A Flows custom app created and running locally. See Getting started with Flows custom apps.
@cognite/app-sdkinstalled.
How it works
When your app callssyncInternalState, CDF serializes your state object into the customAppInternalState URL search param. The state can capture anything that defines where the user was. That includes not just which page is open, but also component-level detail: which panels are collapsed, what values are prefilled in a form, which rows are expanded in a table, what filters are active. For example:
initialState. The app opens with the same tab, filters, and form values already in place.
Implement state syncing
Read initialState on mount
Destructure Always wrap
initialState from connectToHostApp. If it exists, parse it and restore your app’s state before the first render.JSON.parse in a try/catch. Saved state may be malformed if the schema changed between deploys.Open your app with a
customAppInternalState value in the URL (or reload after syncing state in step 2). The restored tab, filters, and other fields match the saved state instead of your defaults.Call syncInternalState when state changes
Call Pass the complete state object on every call.
syncInternalState after any user action that changes meaningful state. Pass a JSON-serialized string.syncInternalState replaces the stored value; it does not merge.Change a persisted value (for example, switch tabs). The browser URL updates with a new
customAppInternalState search param that reflects your change.Complete example
BecausesyncInternalState replaces the stored value on every call, the safest approach is to keep all persisted fields in a single state object and update them together. That way there is one place to serialize from and one place to restore to. No field silently overwrites another.
updateState merges the patch into the current state and immediately syncs the full object. No field is ever lost.
What to include in persisted state
Include state that defines the user’s current view and that they would want to return to:| Include | Exclude |
|---|---|
| Active tab or page | Loading flags (isLoading, isFetching) |
| Selected item IDs | Server-fetched data (re-fetch on mount) |
| Applied filters and sort order | Truly ephemeral UI state (open dropdown, tooltip) |
| Search queries | Error messages |
| Collapsed / expanded panel state | Sensitive values (tokens, passwords) |
| Prefilled form values | Large blobs or binary data |
| Zoom level or scroll position | N/A |
Further reading
- Auth API —
connectToHostApp,HostAppAPI, and helpers. - Integrating AI agents with Flows — Share app state with the built-in agent.
- Run your app locally — Dev server, HTTPS, and CDF iframe.