Skip to content

Commit

Permalink
Add a simple Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 19, 2021
1 parent 32273ee commit d196c28
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import { hello4 } from './lib';
// From the Hugo template.
import * as params from '@params';

var worker = new Worker(params.myworker);

worker.addEventListener(
'message',
function(e) {
console.log('Worker said: ', e.data);
},
false
);

worker.postMessage('Hello Worker');

// https://github.com/gohugoio/hugo/issues/7948
// TODO(bep) make this work in Hugo integration tests import { helloNodeModules } from 'mynodemod';

Expand Down
7 changes: 7 additions & 0 deletions assets/js/myworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
self.addEventListener(
'message',
function(e) {
self.postMessage('Echo From Worker: ' + e.data);
},
false
);
4 changes: 3 additions & 1 deletion layouts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
<script src="https://unpkg.com/react@17/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js" crossorigin></script>
{{ end }}
{{/* We (Hugo/ESBuild) needs to improve the worker integration, but this could be a OK workaround. */}}
{{ $worker := resources.Get "js/myworker.js" | js.Build }}
{{ $inject := slice "js/shims/process.js" }}
{{ $js := resources.Get "js/main.js" | js.Build (dict "minify" false "params" (dict "myparam" "Hugo Rocks!") "inject" $inject ) }}
{{ $js := resources.Get "js/main.js" | js.Build (dict "minify" false "params" (dict "myparam" "Hugo Rocks!" "myworker" $worker.RelPermalink) "inject" $inject ) }}
<script src="{{ $js.RelPermalink }}"></script>
{{ partialCached "jslibs/alpinejs/script-src.html" "-" }}
</head>
Expand Down

0 comments on commit d196c28

Please sign in to comment.