Skip to content

feat(storage): typed browser storage, adopted by the UVE palette - #38886

Open
fmontes wants to merge 26 commits into
issue-38880-typed-browser-storagefrom
issue-38880-typed-browser-storage-impl
Open

fmontes wants to merge 26 commits into
issue-38880-typed-browser-storagefrom
issue-38880-typed-browser-storage-impl

Conversation

@fmontes

@fmontes fmontes commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Closes #38880 · Spec-Kit PR 2 of 2: the implementation of the spec in #38881. This PR is based on that spec branch, so the diff shows only the code. When #38881 merges, GitHub retargets this PR to main.

In one paragraph

The admin UI now has one shared way to save small values in the browser: DOT_STORAGE in @dotcms/data-access. Every saved value is declared once, with a type, a check and a kind. Values are kept per user and never throw because of what's in storage. The UVE palette is the first and only feature moved in this PR: its view, sort and favorites now go through it. Every other screen moves later, one PR each, by adding an entry. The shared code stays untouched.

What changes for editors

  • The UVE palette remembers its view (grid/list), sort and favorites per user, and keeps them after a reload. Before, everyone using the same browser shared them.
  • Everyone starts fresh once. Palette choices saved before this change are not carried over, and the old keys are left in place, so a rollback still finds them.
  • During Login As, the admin sees their own saved choices. Nothing they change is saved: changes stay on screen until Login As ends.
  • Content Drive's "New" dialog uses the default palette behavior. The DOT_PALETTE_PERSIST_PREFERENCES switch is gone.

How to try it

  1. cd core-web && pnpm nx serve dotcms-ui (with dotCMS on :8080).
  2. Open a page in UVE. In the palette, switch to list view, change the sort, and star a content type. Reload: all three are kept.
  3. Log in as another user. The palette shows the admin's saved choices. Change one, end Login As, and the admin's choices are unchanged.
  4. In DevTools → Application → Local Storage, the palette's keys look like dotcms:u=<userId>:uve.palette.layout.

What's in the code

Area What it does
libs/data-access/src/lib/dot-storage/ The core: typed get/set/remove/take; five entry builders (preference, history, cache, handoff, contract); the DOT_STORAGE registry; the startup sweep; injectStored() for components; and withStoredState() for signal stores
apps/dotcms-ui/src/app/providers.ts provideDotStorage() sets the current user from LoginService and runs the startup sweep once
UVE palette store, list component and favorites service Saved through DOT_STORAGE.uve.palette.*. The sort moves from searchParams (request state) to its own sort field (UI state)
libs/dotcms-models The palette types moved here so data-access can use them without a circular import
eslint.config.mjs A warning on direct localStorage/sessionStorage use in core-web, pointing to DOT_STORAGE
docs/frontend/BROWSER_STORAGE.md How to move one more saved value over. STATE_MANAGEMENT.md now shows withStoredState

How dot-storage works

flowchart LR
  REG["DOT_STORAGE<br/>every entry declared once"] --> CORE
  STORE["withStoredState()<br/>signal stores"] --> SIG["injectStored()<br/>components"]
  SIG --> CORE["dotStorage<br/>get · set · remove · take"]
  BOOT["provideDotStorage()<br/>user context + startup sweep"] --> CORE
  CORE --> LS[("localStorage /<br/>sessionStorage")]
  CORE --> MEM[("in-memory values<br/>(this session only)")]
Loading

There are four layers. Each one only talks to the layer below it.

1. Declare: entries and the registry (dot-storage.entries.ts, dot-storage.registry.ts)

  • A builder (preference, history, cache, handoff or contract) returns a frozen entry with these fields:
    • its kind;
    • where it lives (local or session);
    • who owns it (user or app);
    • a version;
    • a default;
    • a check, a function (raw: unknown) => T | undefined that accepts or rejects what was read.
  • A true/false, number or text default needs no check. Any other type must pass one, or the code doesn't compile.
  • DOT_STORAGE = defineDotStorage({ uve: { palette: { layout: preference(...) } } }) gives each entry its path from where it sits in the tree, for example uve.palette.layout. It also adds the entry to a path → entry index, which the sweep and the storage-full cleanup use.
  • Callers only ever pass entries, such as DOT_STORAGE.uve.palette.layout, never key strings. So the type of the value comes from the declaration.

2. Core: dotStorage (dot-storage.core.ts)

  • Keys: dotcms:app:<path> for shared entries, and dotcms:u=<encoded userId>:<path> for per-user ones. The stored value is wrapped as { v: version, at: writeTime, d: data }. Contracts are the exception: they're stored under their fixed raw key, as plain text or JSON with no wrapper, because older code reads them.

  • Read:

    1. A value kept in memory for this session wins.
    2. Otherwise the stored text is parsed and unwrapped.
    3. If the version doesn't match, or the check rejects the data, the key is deleted and the default is returned.
    4. Any exception, such as blocked storage or invalid JSON, also returns the default.
  • Write:

    • The value is kept in memory only when:
      • the user isn't known yet;
      • Login As is active and the entry is per user;
      • storage is blocked.
    • Otherwise it's saved.
    • If the browser reports storage is full, the core removes one key at a time and retries. The order is unreadable keys, then cache, history and handoff keys, each oldest first. Preference and contract keys are never removed.
    • If storage is still full, the value is kept in memory.
  • Context: a signal holding { userId, loginAs }. Any change of user or Login As state clears every in-memory value, so one person never sees another person's unsaved values.

  • Reactivity: a revision signal goes up on these events:

    • a context change;
    • a successful set or remove;
    • a storage event from another tab for a dotcms: key, or a full clear.

    Everything built on top re-reads when it changes.

3. Angular adapters (inject-stored.ts, with-stored-state.feature.ts)

  • injectStored(entry) returns a WritableSignal<T>: a linkedSignal that re-reads on every revision change. Its set and update save through dotStorage, then show the value.
  • withStoredState({ field: entry }) is a signal-store feature.
    • On init, it loads each field from storage.
    • Then two effects keep them in sync: storage to state, and state to storage.
    • Both sides are compared as JSON, so a change never causes a write loop.
    • The palette store uses it for layoutMode, sort and favorites, so the rest of the store keeps using plain patchState.

4. Bootstrap (provide-dot-storage.ts, dot-storage.sweep.ts)

  • provideDotStorage() runs as an app initializer in dotcms-ui.
    • It sets the context from LoginService.auth, read immediately, and then from each auth$ emission.
    • The user ID is always auth.user.userId, which is the admin even during Login As.
    • loginAs is auth.isLoginAs === true.
  • It then runs sweepDotStorage() once. The sweep deletes every dotcms: key whose path isn't declared, or whose scope or storage doesn't match its entry. Keys without the prefix are never touched.
  • Web components embedded in JSPs don't run this. The first feature moved into one of them has to call setDotStorageContext itself (documented in the guide).

Guardrail: the ESLint warning on direct localStorage/sessionStorage use keeps new code on DOT_STORAGE. These places are exempt:

  • dot-storage itself;
  • dotcms-js;
  • the SDK;
  • specs;
  • e2e apps.

Behavior worth a reviewer's eye

Behavior Why
Nothing throws: corrupt, old-version, wrong-shape or blocked storage reads as the default One bad value can't crash a screen
When storage is full, set makes room: unreadable keys first, then cache, history and handoff keys, each oldest first. Preference and contract keys are never removed. If it's still full, the value lives in memory for the session Long-lived choices survive; what can be fetched again goes first
The startup sweep deletes dotcms: keys that DOT_STORAGE no longer declares Renamed or removed entries don't pile up. Keys without the prefix, including the old palette keys, are never touched
Every entry must be declared in DOT_STORAGE The sweep runs at startup. An entry declared in a lazily loaded chunk would be swept on every load (documented in the guide and on defineDotStorage)
Contract entries store raw text or JSON under a fixed name For values older JSP or dotcms-js code also reads

Tests

Written first and confirmed failing before each story's code (TDD), one Red and one Green commit per story.

  • Unit: data-access 1349, dotcms-models 16, edit-ema-ui 359, dotcms-ui 2419, content-drive 2375. All pass.
  • E2E: uve-palette-view-persists, "keep the palette list view after a reload @critical". It passes.
  • /speckit-converge and docs-converge both report converged.
  • Not automated, by decision:
    • The lint rule is checked by running lint.
    • Login As, two users and two tabs are covered by unit tests and the manual pass (quickstart), because the e2e setup has no second user.

Known, not caused by this PR

  • nx affected -t lint fails on four projects. Changing the root ESLint config marks every project as affected, and four of them already fail lint: dotcms-js, block-editor, dotcms-webcomponents and dotcms-scss. Their error counts are the same with the new rule turned off.
  • The palette menu items are announced by their raw i18n keys to screen readers. This was already the case before this PR.

🤖 Generated with Claude Code

fmontes and others added 25 commits December 4, 2026 12:19
…nd tasks

Refs #38880

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cms-models

Adds the empty dot-storage barrel and its entry types, and moves
DotPaletteViewMode, DotPaletteSortOption and the default sort
(DEFAULT_PALETTE_SORT) to @dotcms/dotcms-models so the storage registry
can reference them without depending on the editor. Pure move: the
palette tests stay green. Tasks T001-T005 of #38880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Unit and type tests for the entry checks and the preference builder,
the registry's path assignment and the three UVE palette entries, and
the core's envelope, key format, version and check handling and
blocked storage. They fail because the builders, checks, core and
registry don't exist yet. Tasks T006-T008 of #38880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds the checks (asBoolean, asNumber, asString, oneOf, asArrayOf,
asRecordOf) and the preference builder; the core dotStorage get, set
and remove, which store values as { v, at, d } under
dotcms:<scope>:<path> and return the default for anything missing,
unreadable, of another version, rejected by the check or blocked; and
defineDotStorage with the three UVE palette entries in DOT_STORAGE.
Several defineDotStorage calls add to one shared index. Tasks
T011-T014 of #38880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Identity tests for the core (unknown user reads defaults and keeps
writes in memory only, switching users, clearing in-memory values on a
context change, user id encoding) and for provideDotStorage (context
from LoginService.auth at startup and auth$ later, the signed-in user
rather than the Login As user, loginAs only when isLoginAs is true).
They fail because the overlay and provideDotStorage don't exist yet.
Tasks T015-T016 of #38880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The storage context is now an Angular signal, set by provideDotStorage
from LoginService: the current auth at startup, then every auth$
emission until the app is destroyed. The user id always comes from
auth.user, never from the Login As user. Values that can't be saved
(user not known yet, storage blocked or refusing the write) are kept
in an in-memory overlay that get reads first; a change of user or
Login As state clears it and bumps a revision signal for reactive
readers. provideDotStorage is registered in dotcms-ui's providers.
Tasks T019-T022 of #38880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…S1 Red)

Unit tests for injectStored and withStoredState, the favorites service on
per-user dot-storage, the palette store and list reading view, sort and
favorites from storage, the content-type picker providing no persistence
token, and an e2e that the list view survives a reload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…reen)

dot-storage re-reads on saves, removals and changes from other tabs.
injectStored gives Angular code a writable signal on an entry, and
withStoredState keeps top-level SignalStore fields in storage.

The favorites service now stores per user through dotStorage. The UVE
palette store keeps layoutMode, sort and favorites at the top level of
its state, saved with withStoredState; the sort leaves searchParams and
is sent with every request. The old localStorage keys and the
DOT_PALETTE_PERSIST_PREFERENCES token are gone, so the Content Drive
picker shares the user's saved sort and favorites.

The e2e helper clicks the menu option by its text: PrimeNG names menu
items by the raw i18n key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Red)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Green)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ms: raw key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…US6 Green)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ontract text, no sort alias (Polish Red)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ntract text, no sort alias (Polish Green)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When session storage is full, set removes handoff keys oldest first,
after cache and history, and still never preference or contract keys
(spec User Story 5 kinds table: a handoff is removed when storage is full).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…een)

evictionCandidates lists handoff keys after history keys, oldest first,
matching the spec's kinds table. The dotStorage and handoff JSDoc, the
browser storage guide and data-model.md say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-kind matrix gets a full-storage case for history, cache, handoff
and contract: set doesn't throw and get returns the value just set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The persistence section no longer shows hand-rolled storage reads and
writes, which the new lint rule warns on. It declares a DOT_STORAGE entry,
maps it with withStoredState and links the browser storage guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@claude

claude Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fmontes's task in 3m 45s —— View job


PR Review — typed browser storage, adopted by the UVE palette

  • Gather context (diff vs origin/issue-38880-typed-browser-storage)
  • Review dot-storage core (get/set/take/remove, full-storage eviction, overlay)
  • Review entries, registry, sweep
  • Review Angular glue (injectStored, withStoredState, provideDotStorage)
  • Review UVE palette + favorites-service adoption
  • Verify Login As identity handling against LoginService

This is a clean, carefully built change. I traced the parts most likely to break and could not find a correctness, security, or convention bug introduced by the PR. Notes below are verification items, not defects.

New Issues

  • 🟡 Medium: core-web/libs/portlets/dot-content-drive/portlet/src/lib/components/dialogs/dot-content-drive-dialog-content-type-selector/dot-content-drive-dialog-content-type-selector.component.ts:28 — the Content Drive "New" dialog no longer opts out of palette persistence (the old DOT_PALETTE_PERSIST_PREFERENCES: false provider is gone), so it now shares DOT_STORAGE.uve.palette.*. This reverses a previously deliberate isolation: changing view/sort or starring a type in this transient picker now persists per-user and leaks into the UVE palette, and vice-versa. The PR body states this is intentional ("uses the default palette behavior"). Assumption: the spec for Typed browser storage for core-web, adopted by the UVE palette #38880 accepts shared state here. What to verify: confirm with the spec/product that the "New" dialog's sort and favorites are meant to persist and co-mutate with UVE, rather than being ephemeral as before. If not, re-introduce an opt-out. Not a code defect — just the one behavior change in the diff a reviewer should consciously sign off on.

Things I checked and found correct

  • Login As identity. provideDotStorage keys storage off auth.user.userId with loginAs = auth.isLoginAs. Confirmed in login.service.ts:190 that during Login As setAuth keeps user: this._auth.user (the admin) and only sets loginAsUser, and isLoginAs flips true (:330). So keyFor reads the admin's keys, and isMemoryOnly correctly routes per-user writes to the overlay — admin's saved values are never overwritten, and overlay.clear() on the context change (dot-storage.core.ts:94) restores them when Login As ends. The story holds.
  • No withStoredState ping-pong or default-overwrite. The state→storage effect reads stored via untracked, so a context/revision bump re-reads through storage→state only; the default is never written back over a real stored value on init (with-stored-state.feature.ts:64-79). JSON-equality guards both directions.
  • Favorites sync. DotFavoriteContentTypeService writing through dotStorage.set bumps the revision that the store's injectStored depends on, so the list stays consistent; add() now copies the array before mutating (dot-favorite-contenttype.service.ts:33), avoiding in-place mutation of the stored reference.
  • Never-throwing reads. readContract uses ?? (not ||), so a valid falsy value (0, false, '') is kept; version/shape/quota/blocked-storage paths all fall back to the default without throwing.
  • Eviction only removes same-area cache/history/handoff (plus unreadable dotcms: keys), never preferences/contracts, never the key being written (dot-storage.core.ts:327-377).
  • Sort relocation. Moving orderby/direction out of searchParams into sort is safe: buildESContentParams (contentlets) hardcodes modDate/DESC and never read those fields, and getData re-merges store.sort() into every content-type request.
  • Moved palette types (DotPaletteViewMode, DotPaletteSortOption, DEFAULT_PALETTE_SORT) have no stale importers left pointing at palette/models.

No blocking issues. 🟢
· issue-38880-typed-browser-storage-impl

…082)

parseKey sits next to keyFor and is the only place that splits a dotcms:
key into scope and path; evictionCandidates and the sweep's isDeclared use
it. The sweep imports storageFor from the core instead of keeping a copy.
One sweep test covers an empty scope, a scope with no path and an empty path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant