Skip to content

Instantly share code, notes, and snippets.

@shisama
Last active December 24, 2018 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shisama/33570f8f4f638892c8b7a960ce263a82 to your computer and use it in GitHub Desktop.
Save shisama/33570f8f4f638892c8b7a960ce263a82 to your computer and use it in GitHub Desktop.
Hello, WorkerDOM! - create element to show 'Hello, World!' in worker with @ampproject/worker-dom.
setTimeout(() => {
const h1 = document.createElement('h1');
h1.textContent = 'Hello World!';
document.getElementById('waiting').remove();
document.body.appendChild(h1);
}, 3000);
<head>
<script src="https://unpkg.com/@ampproject/worker-dom@0.2.8/dist/index.js" nomodule defer></script>
</head>
<body>
<div src="hello.js" id="hello">
<div id="waiting">Waiting...</div>
</div>
<script type="module">
import {upgradeElement} from 'https://unpkg.com/@ampproject/worker-dom@0.2.8/dist/index.mjs';
upgradeElement(document.getElementById('hello'), 'https://unpkg.com/@ampproject/worker-dom@0.2.8/dist/unminified.worker.mjs');
</script>
<script nomodule async=false defer>
document.addEventListener('DOMContentLoaded', function() {
MainThread.upgradeElement(document.getElementById('upgrade-me'), '/dist/worker.js');
}, false);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment