Using the Offline Mode: How it works
Back to Using the Offline Mode.
How it works
Section titled “How it works”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.
Caches
Section titled “Caches”In the Brezel offline mode, there are multiple caches for different types of data:
| Cache name | Description | Strategy | Data | Level 1 | Level 2 |
|---|---|---|---|---|---|
| spa-cache | Non-API requests | PreCaching and NetworkFirst | index.html, main.js, app bundle, external resources | ✅ | ✅ |
| api-cf-cache | API responses for static-like data | CacheFirst | Contents of file entities | ✅ | ✅ |
| api-base-cache | API responses for static configuration | StaleWhileRevalidate | Modules, layouts, translations, styles, menus, views, events, languages, countries, subscription plans | ✅ | ✅ |
| api-nf-cache | API responses for volatile data | NetworkFirst | Entities (index, tables, show) | ❌ | ✅ |
Background Sync
Section titled “Background Sync”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.
Offline Resource Tables
Section titled “Offline Resource Tables”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:
- loads all entities from both the
api-nf-cacheand theapi-queuebackground sync queue, - applies
pre_filtersandfilters, - sorts,
- paginates and
- 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.