Skip to content

Commit cc7fd2c

Browse files
authored
chore: refactor with-web-worker example (vercel#40844)
## Changes - Updated dependencies - Migrated to typescript - Removed `div` in favour of Fragment - Replaces `var` with `let` since we don't need global hoisting here ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm lint` - [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 6a11e22 commit cc7fd2c

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

examples/with-web-worker/package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
},
88
"dependencies": {
99
"next": "latest",
10-
"react": "^17.0.2",
11-
"react-dom": "^17.0.2"
10+
"react": "^18.2.0",
11+
"react-dom": "^18.2.0"
12+
},
13+
"devDependencies": {
14+
"@types/node": "18.7.18",
15+
"@types/react": "16.9.17",
16+
"typescript": "4.8.2"
1217
}
1318
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useEffect, useRef, useCallback } from 'react'
22

33
export default function Index() {
4-
const workerRef = useRef()
4+
const workerRef = useRef<Worker>()
5+
56
useEffect(() => {
6-
workerRef.current = new Worker(new URL('../worker.js', import.meta.url))
7-
workerRef.current.onmessage = (evt) =>
8-
alert(`WebWorker Response => ${evt.data}`)
7+
workerRef.current = new Worker(new URL('../worker.ts', import.meta.url))
8+
workerRef.current.onmessage = (event: MessageEvent<number>) =>
9+
alert(`WebWorker Response => ${event.data}`)
910
return () => {
1011
workerRef.current.terminate()
1112
}
@@ -16,9 +17,9 @@ export default function Index() {
1617
}, [])
1718

1819
return (
19-
<div>
20+
<>
2021
<p>Do work in a WebWorker!</p>
2122
<button onClick={handleWork}>Calculate PI</button>
22-
</div>
23+
</>
2324
)
2425
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"incremental": true,
11+
"esModuleInterop": true,
12+
"module": "esnext",
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"jsx": "preserve"
17+
},
18+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
19+
"exclude": ["node_modules"]
20+
}

examples/with-web-worker/utils/pi.js examples/with-web-worker/utils/pi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://stackoverflow.com/a/39575124
2-
export default function pi(n) {
3-
var v = 0
2+
export default function pi(n: number) {
3+
let v = 0
44
for (let i = 1; i <= n; i += 4) {
55
// increment by 4
66
v += 1 / i - 1 / (i + 2) // add the value of the series
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This is a module worker, so we can use imports (in the browser too!)
22
import pi from './utils/pi'
33

4-
addEventListener('message', (event) => {
4+
addEventListener('message', (event: MessageEvent<number>) => {
55
postMessage(pi(event.data))
66
})

0 commit comments

Comments
 (0)