Web Workers
Univer Docs can run document layout in a Web Worker1 by registering UniverDocsLayoutWorkerPlugin. Register the plugin after the Docs core plugin. The workerFactory it accepts creates a new Worker for the layout service.
The snippets below extend the setup described in installation and basic usage. Plugins must be registered before content is created or loaded. To change an existing registration, modify that registration instead of registering the same plugin twice.
Main thread
UniverDocsLayoutWorkerPlugin
workerFactory() => WorkerCreates the Worker that runs document layout. Required.fontFacesobject[]Loads the specified fonts in the editor and in the layout Worker before layout starts.familystringFont family name used for document layout.sourcestring | ArrayBufferFont bytes, or a CSSFontFacesource such asurl(...).descriptorsFontFaceDescriptorsFont weight, style, or other browserFontFacedescriptors.
requestTimeoutMsnumberDefault:15_000Starts recovery when a single Worker request runs longer than this value, in milliseconds.
import { UniverDocsLayoutWorkerPlugin } from '@univerjs/docs'univer.registerPlugin(UniverDocsLayoutWorkerPlugin, { workerFactory: () => new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }),})Worker entry
import { Univer } from '@univerjs/core'import { UniverDocsWorkerPlugin } from '@univerjs/docs/worker'const univer = new Univer()univer.registerPlugin(UniverDocsWorkerPlugin)Use a module Worker with a separate entry file. Keep the package versions in the Worker and on the main thread identical. Rendering and user interaction remain on the main thread.
Footnotes
-
A Web Worker runs JavaScript outside the browser’s main thread and exchanges messages with it. It cannot access the page DOM directly; moving calculations into a Worker does not move the editor UI there. ↩
How is this guide?