Skip to content

Commit 1dc88b6

Browse files
authored
chore: update docs to include new settings (#84)
1 parent 2862116 commit 1dc88b6

File tree

2 files changed

+92
-49
lines changed

2 files changed

+92
-49
lines changed

docs/api.md

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,8 @@ Default value is `false`, so no errors are emitted.
105105
To receive the events, you register an event handler like this:
106106

107107
```js
108-
import { isPreviewMessage } from '@webcontainer/api';
109-
110-
window.addEventListener('message', (event) => {
111-
const data = event.data;
112-
113-
if (!isPreviewMessage(data)) {
114-
return;
115-
}
116-
117-
// process the data received from a preview
108+
webcontainerInstance.on('preview-message', (message) => {
109+
// process the message received from a preview
118110
});
119111
```
120112

@@ -152,7 +144,7 @@ Listens for an `event`. The `listener` is called every time the `event` gets emi
152144
<br />
153145

154146
<h4 id="on-signature">
155-
<code>on(event: 'port' | 'error' | 'server-ready', listener: <a href="#portlistener">PortListener</a> | <a href="#">ErrorListener</a> | <a href="#">ServerReadyListener</a>): () => void</code>
147+
<code>on(event: 'port' | 'error' | 'server-ready' | 'preview-message', listener: <a href="#portlistener">PortListener</a> | <a href="#errorlistener">ErrorListener</a> | <a href="#serverreadylistener">ServerReadyListener</a> | <a href="#previewmessagelistener">PreviewMessageListener</a>): () => void</code>
156148
</h4>
157149

158150
<br />
@@ -201,6 +193,54 @@ Listens for `error` events, emitted when an internal error is triggered.
201193
202194
<br />
203195
196+
<h4>
197+
▸ <code>on(event: 'preview-message', listener: <a href="#previewmessagelistener">PreviewMessageListener</a>): () => void</code>
198+
</h4>
199+
200+
Listens for `preview-message` events, emitted when an internal error is triggered.
201+
202+
<span id="previewmessagelistener">`PreviewMessageListener` (Function)</span>
203+
204+
```ts
205+
(message: PreviewMessage): void
206+
```
207+
208+
<h4 id="previewmessage-type">
209+
<a id="previewmessage-type"><code>PreviewMessage</code></a>
210+
<a href="#previewmessage-type" class="header-anchor" aria-hidden="true">#</a>
211+
</h4>
212+
213+
```ts
214+
type PreviewMessage = (UncaughtExceptionMessage | UnhandledRejectionMessage | ConsoleErrorMessage) & BasePreviewMessage;
215+
216+
interface BasePreviewMessage {
217+
previewId: string;
218+
port: number;
219+
pathname: string;
220+
search: string;
221+
hash: string;
222+
}
223+
224+
interface UncaughtExceptionMessage {
225+
type: PreviewMessageType.UncaughtException;
226+
message: string;
227+
stack: string | undefined;
228+
}
229+
230+
interface UnhandledRejectionMessage {
231+
type: PreviewMessageType.UnhandledRejection;
232+
message: string;
233+
stack: string | undefined;
234+
}
235+
236+
interface ConsoleErrorMessage {
237+
type: PreviewMessageType.ConsoleError;
238+
args: any[];
239+
}
240+
```
241+
242+
<br />
243+
204244
<h4>
205245
▸ <code>on(event: 'server-ready', listener: <a href="#serverreadylistener">ServerReadyListener</a>): () => void</code>
206246
</h4>
@@ -284,55 +324,31 @@ All entities derived from this instance (e.g. processes, the file system, etc.)
284324

285325
---
286326

287-
## `isPreviewMessage`
288-
289-
Check wether or not a message has the `PreviewMessage` shape.
327+
## `reloadPreview`
290328

291-
<h4 id="ispreviewmessage-signature">
292-
<a id="ispreviewmessage-signature">Signature</a>
293-
<a href="#ispreviewmessage-signature" class="header-anchor" aria-hidden="true">#</a>
294-
</h4>
329+
Added in version `1.2.2`.
295330

296-
<br />
331+
Reload the provided iframe by sending a message to the iframe and falling back to resetting the `src` if the iframe didn't respond in time.
297332

298-
<h4 id="ispreviewmessage-signature">
299-
<code>isPreviewMessage(data: any): data is <a href="#preview-message">PreviewMessage</a></code>
333+
<h4 id="reloadpreview-signature">
334+
<a id="reloadpreview-signature">Signature</a>
335+
<a href="#reloadpreview-signature" class="header-anchor" aria-hidden="true">#</a>
300336
</h4>
301337

302338
<br />
303339

304-
<h4 id="ispreviewmessage-returns">
305-
<a id="ispreviewmessage-returns">Returns</a>
306-
<a href="#ispreviewmessage-returns" class="header-anchor" aria-hidden="true">#</a>
340+
<h4 id="reloadpreview-signature">
341+
<code>reloadPreview(preview: HTMLIFrameElement, hardRefreshTimeout?: number = 200): Promise&lt;void&gt;</code>
307342
</h4>
308343

309-
Returns `true` if `data` is of type `PreviewMessage`.
344+
<br />
310345

311-
<h4 id="preview-message">
312-
<a id="preview-message"><code>PreviewMessage</code></a>
313-
<a href="#preview-message" class="header-anchor" aria-hidden="true">#</a>
346+
<h4 id="reloadpreview-returns">
347+
<a id="reloadpreview-returns">Returns</a>
348+
<a href="#reloadpreview-returns" class="header-anchor" aria-hidden="true">#</a>
314349
</h4>
315350

316-
```ts
317-
export type PreviewMessage = UncaughtExceptionMessage | UnhandledRejectionMessage | ConsoleErrorMessage;
318-
319-
export interface UncaughtExceptionMessage {
320-
type: PreviewMessageType.UncaughtException;
321-
message: string;
322-
stack: string | undefined;
323-
}
324-
325-
export interface UnhandledRejectionMessage {
326-
type: PreviewMessageType.UnhandledRejection;
327-
message: string;
328-
stack: string | undefined;
329-
}
330-
331-
export interface ConsoleErrorMessage {
332-
type: PreviewMessageType.ConsoleError;
333-
args: any[];
334-
}
335-
```
351+
Returns a `Promise` that resolves when the reload has completed.
336352

337353
## `auth`
338354

@@ -787,6 +803,7 @@ Watch for changes to a given file or directory.
787803
```ts
788804
interface Options {
789805
encoding?: BufferEncoding | null;
806+
recursive?: boolean;
790807
}
791808
```
792809

@@ -796,6 +813,10 @@ interface Options {
796813

797814
Specifies the character encoding to be used for the filename passed to the listener. Default: `'utf8'`.
798815

816+
#### `recursive?: boolean`
817+
818+
Indicates whether all subdirectories should be watched, or only the current directory. This applies when a directory is specified. Default: `false`.
819+
799820
<h4 id="watch-listener">
800821
<a id="watch-listener"><code>Listener</code></a>
801822
<a href="#watch-listener" class="header-anchor" aria-hidden="true">#</a>

docs/changelog.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,32 @@ head:
1010

1111
# Changelog
1212

13+
## 1.2.4
14+
15+
* Bug fix: [`reloadPreview`](api#reloadpreview) was always doing a hard refresh as the port was not transferred.
16+
17+
## 1.2.3
18+
19+
* Make `@webcontainer/api` ESM and SSR friendly again.
20+
* Emit preview messages on the webcontainer's [`on('preview-message')`](api#on-overloads) event handler.
21+
22+
## 1.2.2
23+
24+
* Add [`reloadPreview`](api#reloadpreview) utility, also exported under `@webcontainer/api/utils`.
25+
26+
## 1.2.1
27+
28+
:::warning
29+
It's recommended to use version `>= 1.2.3` to use `forwardPreviewErrors` as you cannot add an
30+
event listener in the `webcontainerInstance` in that version.
31+
:::
32+
33+
* Add `forwardPreviewErrors` to [`BootOptions`](api#boot-options) which lets you capture "errors" from previews.
34+
1335
## 1.2.0
1436

1537
* Add support for [`authentication`](api#auth).
16-
* add [`cwd`](api#▸-cwd-string) option to [`SpawnOptions`](api#spawnoptions).
38+
* Add [`cwd`](api#▸-cwd-string) option to [`SpawnOptions`](api#spawnoptions).
1739

1840
## 1.1.8
1941

0 commit comments

Comments
 (0)