Skip to main content
@craft-ts
LearnLearn with EffectAI agentsGuideReference
@craft-ts/core@craft-ts/component@craft-ts/dev-tools@craft-ts/effect@craft-ts/style@craft-ts/style-testing@craft-ts/i18n@craft-ts/i18n-effect@craft-ts/mcp
ExamplesMigrationEffect compatibility & maturityAdopting CraftTS progressivelyPress kitRoadmapBacklog
Beta
  • Learn
  • Learn with Effect
  • AI agents
  • Guide
  • Reference
  • Packages
  • Resources
  • Guide overview
  • Create a project
i.Core concepts
  • The mental model
  • Which primitive should I use?
  • Anatomy of a primitive
  • Generators and yield*
  • Insertions
  • Typed insertion pipes
  • Exceptions as values
ii.Managing state
  • Local state
  • State machines
  • query
  • Mutations
  • queryParams
  • asyncProcess
  • Selecting
  • Reacting to mutations
  • Collections
  • Collection utilities
  • Persistence
  • GlobalPersisterHandler
  • Pagination placeholders
  • Schema validation
iii.Recommended approaches
  • Inject at the point of use
iv.Structuring the app
  • craftService
  • Service scopes
  • Shaping the public API
  • Abstract services
  • App start
  • Lazy services
  • Server functions
  • craftRegisterFor
  • Target wrapper
v.Routing & type-safe DI
  • Setup
  • CLI automation
  • ESLint rules
  • Route providers
  • Route guards
  • Route exception handling
  • Non-blocking navigation
  • Global error component
  • Route load errors
  • Scaling routes
vi.Components & templates
  • Components
  • Activating the style system
  • Typed styles
  • Defining a design system
  • Tokens and variables
  • Global foundation and fonts
  • Pseudo-elements and animations
  • Axes and the matrix
  • Context obligations
  • Testing visual states
  • Text contrast
  • Visual attestation
  • Template obligations
  • Typed CSS variables
  • Fine-grained reactivity
  • Progressive rendering
  • Directives and .pipe(...)
  • settledValue & pendingNode
  • Accessibility
  • Customization
  • Content projection
  • Styling a component
  • Template migrator
vii.Forms
  • Overview
  • Validators
  • Submitting
  • Nested forms
  • Exception handling
  • Complete examples
viii.Testing
  • Testing services
  • Testing components
  • Type-level tests
  • Browser boundaries
  • Page stability
  • Architecture rules
    • Overview
    • Declarative baseline
    • Unique identities
    • HTTP endpoint ownership
    • Computed purity
    • Dependency cycles
    • Mutation reactions
    • Route DI proofs
    • Route component files
    • Path boundaries
    • Exclusive links
    • Persisted identities
    • insertSelect keys
    • craftEffect and network
    • craftEffect and imperative sync
    • Interactive element names
    • Metric thresholds
    • Server-state loaders
    • Primitive loader requirements
  • Extensible architecture graph
  • Graph insights
  • Craft graph vs Nx
ix.Reactivity utilities
  • craftComputed
  • craftEffect
  • craftMethod
  • source$
  • on$
  • fromEventToSource$
  • sourceFromEvent
  • afterRecomputation
x.Internationalisation
  • Type-safe i18n
  • The catalogue
  • Tokens
  • The runtime
  • With Effect
xi.Deployment (experimental)
  • Overview
  • Manifest reference
  • Diagnostics
  • Providers
  • Alchemy provider
xii.Going further
  • SSR and hydration
  • Effect integration
  • Program operators
  • Pattern matching
  • Observability
  • AI agents
  • Temporal runtime
  1. Managing state
  2. GlobalPersisterHandler

GlobalPersisterHandler#

Clears everything @craft-ts has persisted through the active StoragePersister, in one call.

Use it when cached data must not outlive a session boundary: logout, switching accounts, a "reset the app" action. Not when you want to invalidate one resource — reload that query, or give it a shorter cacheTime in Persistence.

It clears everything

There is no per-key variant. Every persisted query, mutation and async process goes.

typescript
import {
  GlobalPersisterHandlerService,
  provideGlobalPersisterHandlerService,
} from '@craft-ts/core';

providers: [provideGlobalPersisterHandlerService()];

How it works#

The handler delegates to the active StoragePersister. The built-in localStorage and sessionStorage implementations remove every key that starts with the craft-ts- prefix from their respective backend. This ensures complete cleanup of all data cached by @craft-ts, including:

  • Persisted queries

  • Persisted mutations

  • Persisted async processes

  • Any other data cached by the @craft-ts persistence layer

The common case — clearing on logout#

ts
import { craftService, GlobalPersisterHandlerService, craftExpose } from '@craft-ts/core';

const { LogoutHandler } = craftService(
  { name: 'LogoutHandler', providedIn: 'toProvide' },
  function* () {
    const persister = yield* GlobalPersisterHandlerService();

    yield* craftExpose('logout', () => persister.clearAllCache());
  },
);

Force refresh all data#

typescript
const { CacheActions } = craftService(
  { name: 'CacheActions', providedIn: 'toProvide' },
  function* () {
    const persister = yield* GlobalPersisterHandlerService();
    yield* craftExpose('clearCache', () => persister.clearAllCache());
  },
);

Clear cache when switching accounts#

ts
import { GlobalPersisterHandlerService, craftService, craftExpose } from '@craft-ts/core';

const { AccountSwitcher } = craftService(
  { name: 'AccountSwitcher', providedIn: 'toProvide' },
  function* () {
    const persister = yield* GlobalPersisterHandlerService();
    yield* craftExpose('switchAccount', (accountId: string) => {
      persister.clearAllCache();
      // Load the selected account...
      return accountId;
    });
  },
);
Other situations where this comes up

1. User Logout#

Remove all user-specific cached data when a user logs out to prevent data leakage to the next user.

typescript
logout() {
  this.persisterHandler.clearAllCache();
  this.authService.logout();
}

2. Privacy Compliance#

Ensure no sensitive data remains in the selected storage backend after a user session ends.

typescript
ngOnDestroy() {
  if (this.isPrivateMode) {
    this.persisterHandler.clearAllCache();
  }
}

3. Development/Testing#

Quickly clear all cached data during development or testing.

typescript
resetCache() {
  if (environment.development) {
    this.persisterHandler.clearAllCache();
    console.log('Cache cleared');
  }
}

4. Data Corruption Recovery#

Clear potentially corrupted cached data and force fresh data loading.

typescript
handleDataError() {
  this.persisterHandler.clearAllCache();
  this.showMessage('Cache cleared. Please refresh the page.');
}

See Also#

  • Local Storage Persister

  • Query

  • Mutation

←PersistencePagination placeholders→

On this page

  • How it works
  • The common case — clearing on logout
  • Force refresh all data
  • Clear cache when switching accounts
  • 1. User Logout
  • 2. Privacy Compliance
  • 3. Development/Testing
  • 4. Data Corruption Recovery
  • See Also

@craft-ts · MIT licensed

GitHubnpm

Search