Skip to content

Commit 4c38d48

Browse files
committed
Rename 'path' to 'url' and add 'options' prop.
1 parent e5ffcb8 commit 4c38d48

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![GitHub PRs](https://img.shields.io/github/issues-pr/ghengeveld/react-webworker.svg)](https://github.com/ghengeveld/react-webworker/pulls)
99

1010
React component for easy communication with a Web Worker. Leverages the Render Props pattern for ultimate flexibility as
11-
well as the new Context API for ease of use. Just specify the public path to your Web Worker and you'll get access to
11+
well as the new Context API for ease of use. Just specify the public url to your Web Worker and you'll get access to
1212
any messages or errors it sends, as well as the `postMessage` handler.
1313

1414
- Zero dependencies
@@ -35,7 +35,7 @@ Using render props for ultimate flexibility:
3535
import WebWorker from "react-webworker"
3636

3737
const MyComponent = () => (
38-
<WebWorker path="/worker.js">
38+
<WebWorker url="/worker.js">
3939
{({ data, error, postMessage }) => {
4040
if (error) return `Something went wrong: ${error.message}`
4141
if (data)
@@ -57,7 +57,7 @@ Using helper components (don't have to be direct children) for ease of use:
5757
import WebWorker from "react-webworker"
5858

5959
const MyComponent = () => (
60-
<WebWorker path="/worker.js">
60+
<WebWorker url="/worker.js">
6161
<WebWorker.Pending>
6262
{({ postMessage }) => <button onClick={() => postMessage("hello")}>Hello</button>}
6363
</WebWorker.Pending>
@@ -78,10 +78,13 @@ const MyComponent = () => (
7878

7979
`<WebWorker>` takes the following properties:
8080

81-
- `path` {string} (required) Public path to the Web Worker file (from the root of your domain)
81+
- `url` {string} (required) Public url to the Web Worker file (from the root of your domain)
82+
- `options` {Object} Options passed to the Worker constructor
8283
- `onMessage` {Function} Callback function invoked when a message is received, passing message data as argument
8384
- `onError` {Function} Callback function invoked when an error is received, passing error object as argument
8485

86+
> `url` and `options` are evaluated at mount time, so they must be defined immediately and won't respond to changes.
87+
8588
### Render props
8689

8790
`<WebWorker>` provides the following render props:
@@ -101,7 +104,7 @@ const MyComponent = () => (
101104
import WebWorker from "react-webworker"
102105

103106
const MyComponent = () => (
104-
<WebWorker path="/worker.js">
107+
<WebWorker url="/worker.js">
105108
{({ data, error, postMessage, updatedAt, lastPostAt }) => (
106109
<div>
107110
{data && (
@@ -119,6 +122,18 @@ const MyComponent = () => (
119122
)
120123
```
121124

125+
### Passing options to the Worker
126+
127+
```js
128+
import WebWorker from "react-webworker"
129+
130+
const MyComponent = () => (
131+
<WebWorker url="/worker.js" options={{ type: "module", credentials: "include" }}>
132+
...
133+
</WebWorker>
134+
)
135+
```
136+
122137
## Helper components
123138

124139
`<WebWorker>` provides several helper components that make your JSX even more declarative.

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class WebWorker extends React.Component {
4242
}
4343

4444
componentDidMount() {
45-
this.worker = new window.Worker(this.props.path)
45+
if (this.props.path && console) {
46+
console.warn("`path` is deprecated and will be removed in the next major release. Use `url` instead.")
47+
}
48+
this.worker = new window.Worker(this.props.url || this.props.path, this.props.options)
4649
this.worker.onmessage = this.onMessage
4750
this.worker.onerror = this.onError
4851
this.mounted = true

0 commit comments

Comments
 (0)