Skip to content

Commit d196c28

Browse files
committed
Add a simple Worker
1 parent 32273ee commit d196c28

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

assets/js/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import { hello4 } from './lib';
1212
// From the Hugo template.
1313
import * as params from '@params';
1414

15+
var worker = new Worker(params.myworker);
16+
17+
worker.addEventListener(
18+
'message',
19+
function(e) {
20+
console.log('Worker said: ', e.data);
21+
},
22+
false
23+
);
24+
25+
worker.postMessage('Hello Worker');
26+
1527
// https://github.com/gohugoio/hugo/issues/7948
1628
// TODO(bep) make this work in Hugo integration tests import { helloNodeModules } from 'mynodemod';
1729

assets/js/myworker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
self.addEventListener(
2+
'message',
3+
function(e) {
4+
self.postMessage('Echo From Worker: ' + e.data);
5+
},
6+
false
7+
);

layouts/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
<script src="https://unpkg.com/react@17/umd/react.production.min.js" crossorigin></script>
1111
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js" crossorigin></script>
1212
{{ end }}
13+
{{/* We (Hugo/ESBuild) needs to improve the worker integration, but this could be a OK workaround. */}}
14+
{{ $worker := resources.Get "js/myworker.js" | js.Build }}
1315
{{ $inject := slice "js/shims/process.js" }}
14-
{{ $js := resources.Get "js/main.js" | js.Build (dict "minify" false "params" (dict "myparam" "Hugo Rocks!") "inject" $inject ) }}
16+
{{ $js := resources.Get "js/main.js" | js.Build (dict "minify" false "params" (dict "myparam" "Hugo Rocks!" "myworker" $worker.RelPermalink) "inject" $inject ) }}
1517
<script src="{{ $js.RelPermalink }}"></script>
1618
{{ partialCached "jslibs/alpinejs/script-src.html" "-" }}
1719
</head>

0 commit comments

Comments
 (0)