Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active June 18, 2018 21:13
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 nolanlawson/2708fc71b9e2e81a62479752b7760db4 to your computer and use it in GitHub Desktop.
Save nolanlawson/2708fc71b9e2e81a62479752b7760db4 to your computer and use it in GitHub Desktop.
Test link rel=preload with importScripts()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test link rel=preload with importScripts</title>
</head>
<body>
<h1>Test link rel=preload with importScripts</h1>
<script src="main.js"></script>
</body>
</html>
console.log('library loaded')
/* global Worker */
(() => {
var worker = new Worker('worker.js')
worker.addEventListener('message', msg => {
if (msg.data === 'ready') {
var link = document.createElement('link')
link.setAttribute('rel', 'preload')
link.setAttribute('as', 'script')
link.setAttribute('href', 'library.js')
document.head.appendChild(link)
setTimeout(() => {
worker.postMessage('loadLibrary')
}, 5000)
} else if (msg.data === 'libraryLoaded') {
console.log('library loaded in worker')
}
})
})()
/* global self importScripts */
self.addEventListener('message', msg => {
if (msg.data === 'loadLibrary') {
importScripts('library.js')
self.postMessage('libraryLoaded')
}
})
self.postMessage('ready')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment