Run a background service
Run a Web Worker for your app while the Dashboard is open.
A background web worker is a Web Worker that the Dashboard starts when it loads your organization's applications. It keeps running while the Dashboard is open, even if nobody opens your app, and runs once in each Dashboard tab. Web workers run for apps with default or unlisted visibility, and not for disabled apps.
Beta API
The Dashboard extension API is in beta and can change without notice. A web worker can run code and log output. It doesn't have access to the App SDK.
Declare the web worker
import {defineApplication, defineCliConfig, defineWebWorker} from 'sanity/cli'
export default defineCliConfig({
app: defineApplication({
organizationId: 'your-organization-id',
slug: 'content-sync',
title: 'Content sync',
entry: './src/App.tsx',
webWorkers: [
defineWebWorker({name: 'sync', title: 'Content sync', src: './src/worker.ts'}),
],
}),
})src is relative to the project root, the folder that contains sanity.cli.ts. The worker's name must be unique across the app, its views, and its other web workers.
Export the worker function
import type {WebWorkerProps} from '@sanity/sdk/dashboard'
export default ({worker}: WebWorkerProps) => {
const interval = setInterval(() => {
console.warn(`${worker.name} is running`)
}, 10_000)
return () => clearInterval(interval)
}The function runs once when the worker starts and receives WebWorkerProps. worker contains the worker's name and type. Return a cleanup function for timers, listeners, and connections.
The worker can use any Web Worker API, such as fetch.
Talk to the app's views
The function also receives bus, the Dashboard message bus. It connects the worker to the views of the same app, and nothing else: only your app's views can reach the worker, and only your app's views receive what it sends.
bus.emit(topic, payload): send a message.bus.query(stateTopic): read a state topic's current value.bus.subscribe(stateTopic, handler, {signal}): listen to a state topic.
Answer your views
Subscribe to workers.post to answer a post from one of your app's views.
import type {WebWorkerProps} from '@sanity/sdk/dashboard'
export default ({bus, worker}: WebWorkerProps) => {
const controller = new AbortController()
bus.subscribe(
'workers.post',
(message) => {
const {name, payload} = message
console.warn(`${worker.name} answering ${name}`)
message.reply(getSnapshot(payload))
},
{signal: controller.signal},
)
return () => controller.abort()
}A view's post arrives as {name, payload}: name is the worker the view addressed, and payload is what it sent. Call message.reply(value) to resolve the view's promise. message.reject(reason) refuses the post, and the view's promise rejects with REFUSED. If the handler throws, the post is refused with HANDLER_THREW.
Send to your views
Emit workers.message to send a message to the app's views. Only that app's views receive it.
bus.emit('workers.message', {items})Make authenticated requests
Subscribe to the auth.token state topic for the signed-in user's Sanity token, and send it as a bearer token. The Dashboard publishes a new value when it refreshes the token, so keep the latest one rather than reading it once.
import type {WebWorkerProps} from '@sanity/sdk/dashboard'
export default ({bus}: WebWorkerProps) => {
const controller = new AbortController()
let token: string | undefined
bus.subscribe(
'auth.token',
(value) => {
token = value
},
{signal: controller.signal},
)
const interval = setInterval(async () => {
if (!token) return
const response = await fetch('https://api.sanity.io/v2025-02-19/users/me', {
headers: {Authorization: `Bearer ${token}`},
})
bus.emit('workers.message', await response.json())
}, 60_000)
return () => {
controller.abort()
clearInterval(interval)
}
}Inspect worker output
The Dashboard forwards the worker's console output to the page console and prefixes it with the worker name, for example [sanity-workbench] [service:sync] sync is running.
By default, the Dashboard only shows warnings and errors, so it hides console.log, console.info, and console.debug. Use console.warn, or show every level by running this in the browser console and reloading the page:
localStorage.setItem('SANITY_WORKBENCH_LOG_LEVEL', 'debug')Handle errors
If the worker throws, the Dashboard logs the error and stops that web worker. Other web workers and views keep running. The Dashboard doesn't restart a failed web worker; reload the page to start it again.
Next steps
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?