Skip to content

Using the Offline Mode: How it works

Back to Using the Offline Mode.

The offline mode uses a service worker to cache the application files, modules, and data. When you open the application, the service worker checks if there is an internet connection. If there is no connection, the service worker loads data from the cache and serves it to SPA. The service worker is therefore a layer between the frontend in Brezel SPA and the Brezel API. It uses different strategies to cache the data. If PUT, POST, or DELETE requests are made, the service worker will store the data in the cache and synchronize it later when the internet connection is available using Background Sync.

In the Brezel offline mode, there are multiple caches for different types of data:

Cache nameDescriptionStrategyDataLevel 1Level 2
spa-cacheNon-API requestsPreCaching and NetworkFirstindex.html, main.js, app bundle, external resources✅✅
api-cf-cacheAPI responses for static-like dataCacheFirstContents of file entities✅✅
api-base-cacheAPI responses for static configurationStaleWhileRevalidateModules, layouts, translations, styles, menus, views, events, languages, countries, subscription plans✅✅
api-nf-cacheAPI responses for volatile dataNetworkFirstEntities (index, tables, show)❌✅

When you are offline and make a PUT, POST, or DELETE request, the service worker will store the request in the api-queue queue. When the internet connection is available again, the service worker will replay the requests from the queue and synchronize the data with the API.

File uploads also work in offline mode. When you upload a file, the service worker will store the upload request and attach a X-Local-Id header to the request. A mock response is returned with a local ID prefixed with local-. On replay, the service worker will intercept the returned real ID and replace all occurrences of the local ID in upcoming requests in the queue with the real ID. This strategy is also used on all other entity relations.

During replay, request headers are attached with X-Queue-Name, X-Queued-At and X-Queued-By, to log that the entity update was performed by a service worker.

Entity updates stored in the background sync queue are also virtually applied to the entities in the cache by the service worker on each request, so that the SPA always shows the latest data.

You can view the background sync queue by clicking on the cloud icon (when offline) or the sync icon (when back online) in the status bar of Brezel.

Many requests are so specific that they cannot all be reasonably cached, such as paginated lists of entities. To accommodate this, the Brezel service worker caches all resource tables and entities requests it has seen in the api-nf-cache. Then, when you are offline, the service worker intercepts your API requests for resource tables and does the following:

  1. loads all entities from both the api-nf-cache and the api-queue background sync queue,
  2. applies pre_filters and filters,
  3. sorts,
  4. paginates and
  5. mocks the API table response with the result.

Because this happens in the service worker layer, the SPA does not know that the data is not coming from the API.