Docsfrontbacked.js reference
On this page

Storage reference

Save browser values and declare persistent storage.

Method signatures

js

Reference excerpt: use this with the page, variables and functions described in this guide. Try a complete example →

Before you begin: Uploads and posts reference.

Storage

Use Frontbacked.storage for carts, wishlists, drafts, saved filters, and other visitor data. Browser-only values need no setup:

js

Reference excerpt: use this with the page, variables and functions described in this guide. Try a complete example →

To save a value both on this device and in the visitor's account, define its destination once in your shared theme script:

js

Reference excerpt: use this with the page, variables and functions described in this guide. Try a complete example →

Guests keep their changes on this device. After sign-in or signup, Frontbacked loads their saved account values, reconciles the guest selection, and saves automatically. Further edits also save automatically. Your normal FRL profile schema and access rules validate these saves; no extra theme endpoint is needed. For the example above, declare browserData.cart in your profile schema.

remote points to the complete field path, which can differ from the local key. Without remote, the key stays browser-only. default is a fallback for missing values, not a write: defining an empty cart does not overwrite an existing account cart. An explicit fallback passed to get takes precedence over the default. Repeating define with the same key returns its existing handle; define each key consistently on every page that uses it.

Account saves targeting the same user-data destination are batched, even when keys use different profile paths. Only changed fields are submitted. remove and clear also remove account values for remote keys, subject to FRL validation. Do not define two keys targeting the same field or overlapping parent and child paths.

reconcile combines guest and account values once. conflict resolves an ordinary edit when the account value changed since this device last read it. Each callback receives { local, remote, base }. Without a resolver, guest values replace the corresponding account field; conflicting account edits pause for review. merge.byKey combines matching entries and sums the named fields. Supplying base applies quantity differences and removals while preserving remote additions. Frontbacked.merge.unique(values) removes identical JSON values.

Pending saves survive reloads. Retries do not import the same guest cart twice, and signing out switches to a separate guest scope. Another account cannot claim the previous account's pending edits. Product references declared in FRL are saved as IDs; calculate checkout prices from saved product data in an FRL endpoint.

Read defined values and save status directly in FQL:

index.html

Each defined key exposes value, status, and error under $storage. Statuses are local, pending, saving, synced, and paused. Invalid values and denied saves pause without losing local changes. Correct the value with set to retry. The built-in destination automatically waits for authentication and retries temporary connection failures.

For an action that must wait for persistence, use await Frontbacked.storage.flush("cart"). This is optional; normal writes already save automatically. It returns { ok: true }, a pending result, or an error without opening an alert. flush() without a key waits for all defined destinations and returns their results as an array.

Optional conditions and error handling:

js

Reference excerpt: use this with the page, variables and functions described in this guide. Try a complete example →

A custom destination can supply { id, path, read, write } instead of userData.at(...). path identifies the complete field within its data. read({auth}) returns {ok:true,data,version}. write({changes,remove,operationId,version,auth}) receives the combined object patch and dotted deletion paths. Return {ok:true}, a structured error with an HTTP status, or {ok:false,sync:{conflict:true}} for a stale version. Your destination must enforce permissions, atomic version checks, and operation-ID deduplication for safe retries.

MethodPurpose
define(key, options)Configure a default and optional remote persistence once.
get(key, fallback?)Read the current value or its fallback.
set(key, value)Save immediately; configured remote keys sync automatically.
remove(key)Remove the value locally and from its configured account destination.
all(prefix?)Read browser values and defined keys, optionally filtered by prefix.
clear(prefix?)Remove matching values through the same storage behavior.
flush(key?)Optionally wait for pending account saves.

define returns a key-specific handle with get(fallback?), set(value), remove(), flush(), status, and error. Storage keys must be non-empty strings up to 160 characters; defined keys cannot contain reserved path segments (__proto__, prototype, or constructor).

Updating existing themes

The separate Frontbacked.sync API and $sync bindings have been removed. Replace connection writes with Frontbacked.storage.set(key, value) and bindings with $storage.key.value. The older syncOnAuth, syncToAuth, and syncRegisteredToAuth helpers are also removed; use a definition with remote.

When replacing an existing connection, include migrateFrom: "previous-connection-name" in each field definition and keep its existing destination and field name. For example, the former shopping connection's cart field uses remote: Frontbacked.userData.at("browserData.cart") and migrateFrom: "shopping". Keep this migration setting in subsequent releases to preserve saved data and pending requests. Existing browser-only values under the defined key are imported once when no connection state exists. Existing connection state takes precedence.

Continue with Authentication reference.