Skip to content

Commit e232474

Browse files
committed
chore: Document offline support, incl. offline_enabled attr for web component
1 parent e48ee9c commit e232474

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ The `editor-wc` tag accepts the following attributes, which must be provided as
9191
- `output_split_view`: Start with split view in output panel (defaults to `false`, i.e. tabbed view)
9292
- `project_name_editable`: Allow the user to edit the project name in the project bar (defaults to `false`)
9393
- `react_app_api_endpoint`: API endpoint to send project-related requests to
94+
- `offline_enabled`: Show an offline indicator when the user's device loses connectivity (defaults to `false`). Requires the service worker to be registered on the host page — see [Offline support](#offline-support).
9495
- `read_only`: Display the editor in read only mode (defaults to `false`)
9596
- `sense_hat_always_enabled`: Show the Astro Pi Sense HAT emulator on page load (defaults to `false`)
9697
- `show_save_prompt`: Prompt the user to save their work (defaults to `false`)
@@ -130,6 +131,33 @@ The host page is able to communicate with the web component via custom methods p
130131

131132
This allows the host page to query the current code in the editor and to control code runs from outside the web component, for example.
132133

134+
### Offline support
135+
136+
The web component ships a service worker (`service-worker.js`) that caches the editor shell and Pyodide assets so the component remains usable after a network loss.
137+
138+
To enable offline support on your host page:
139+
140+
1. Register the service worker from your host page (or let the bundled `web-component.html` do it automatically):
141+
```js
142+
if ("serviceWorker" in navigator) {
143+
navigator.serviceWorker.register("./service-worker.js");
144+
}
145+
```
146+
2. Pass the `offline_enabled` attribute to the web component so the offline indicator is shown when connectivity is lost:
147+
```html
148+
<editor-wc offline_enabled="true"></editor-wc>
149+
```
150+
151+
Offline mode is opt-in — neither the service worker registration nor the offline badge will appear unless these steps are taken.
152+
153+
#### Developing with offline support
154+
155+
The service worker is not registered in development by default. To enable it locally, set the environment variable before starting the dev server:
156+
157+
```sh
158+
REACT_APP_ENABLE_SERVICE_WORKER=true yarn start
159+
```
160+
133161
## Development
134162

135163
### Previewing

src/web-component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
newWebComp.setAttribute("with_projectbar", "true");
110110
newWebComp.setAttribute("with_sidebar", "true");
111111
newWebComp.setAttribute("use_editor_styles", "true");
112+
// see "Offline support" section in README for more details
113+
newWebComp.setAttribute("offline_enabled", "true");
112114
newWebComp.setAttribute(
113115
"sidebar_options",
114116
JSON.stringify([

0 commit comments

Comments
 (0)