Last active
June 18, 2018 21:13
-
-
Save nolanlawson/2708fc71b9e2e81a62479752b7760db4 to your computer and use it in GitHub Desktop.
Test link rel=preload with importScripts()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('library loaded') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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') | |
} | |
}) | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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