Overview
serviceWorker is a utility module that provides register and unregister functions for managing a service worker, enabling offline caching and Progressive Web App (PWA) capabilities for the Flowise UI in production builds.
Description
This module follows the Create React App (CRA) service worker pattern. The register function only activates in production mode and when the browser supports service workers. It verifies that the PUBLIC_URL is on the same origin as the page, then registers the service worker script. On localhost, it performs additional validation by fetching the service worker URL and checking the response content type. The registerValidSW helper attaches an onupdatefound listener that notifies users via config.onUpdate when new content is available (requiring tab closure) or config.onSuccess when content is first cached for offline use. The checkValidServiceWorker helper handles the case where the service worker file is missing (404) or is not JavaScript, unregistering the stale worker and reloading the page. The unregister function deactivates any active service worker registration.
Usage
Call register() in the application entry point (index.js) to enable PWA caching in production. Call unregister() if you want to disable service worker caching. This module is not called by default in the Flowise app.
Code Reference
Source Location
Signature
export function register(config) { ... }
export function unregister() { ... }
Import
import * as serviceWorker from './serviceWorker'
// or
import { register, unregister } from './serviceWorker'
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| config |
object |
No |
Optional configuration object for register(). Supports onUpdate(registration) callback invoked when new content is available, and onSuccess(registration) callback invoked when content is cached for the first time.
|
Outputs
| Name |
Type |
Description
|
| (none) |
void |
Both register and unregister return nothing. They produce side effects by registering/unregistering the service worker and invoking config callbacks.
|
Internal Functions
| Function |
Description
|
| isLocalhost |
Boolean constant that evaluates to true when running on localhost, [::1] (IPv6 localhost), or 127.x.x.x (IPv4 localhost).
|
| registerValidSW(swUrl, config) |
Registers the service worker at swUrl and attaches an onupdatefound listener that triggers config.onUpdate or config.onSuccess callbacks.
|
| checkValidServiceWorker(swUrl, config) |
Fetches the service worker URL to verify it exists and is JavaScript. If not found (404 or wrong content type), unregisters the worker and reloads. Otherwise delegates to registerValidSW.
|
Usage Examples
Basic Usage
import * as serviceWorker from './serviceWorker'
// Enable service worker for offline capabilities
serviceWorker.register({
onUpdate: (registration) => {
console.log('New content available, please close all tabs.')
},
onSuccess: (registration) => {
console.log('Content cached for offline use.')
}
})
// To disable:
// serviceWorker.unregister()
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.