This file contains hidden or 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
function binarySearch(searchValue) { | |
'use strict'; | |
var minIndex = 0; | |
var maxIndex = sortedArray.length - 1; | |
var currentIndex; | |
var currentElement; | |
while (minIndex <= maxIndex) { | |
currentIndex = (minIndex + maxIndex) / 2 | 0; |
This file contains hidden or 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
this.addEventListener('fetch', event => { | |
// request.mode = navigate isn't supported in all browsers | |
// so include a check for Accept: text/html header. | |
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) { | |
event.respondWith( | |
fetch(event.request.url).catch(error => { | |
// Return the offline page | |
return caches.match(offlineUrl); | |
}) | |
); |
This file contains hidden or 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
'use strict'; | |
var cacheVersion = 1; | |
var currentCache = { | |
offline: 'offline-cache' + cacheVersion | |
}; | |
const offlineUrl = 'offline-page.html'; | |
this.addEventListener('install', event => { | |
event.waitUntil( |
This file contains hidden or 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> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Service Worker Toolbox</title> | |
</head> | |
<body> | |
<!-- Images --> | |
<img src="/images/contact.svg" height="80" width="80" /> | |
<img src="/images/info.svg" height="80" width="80" /> |
This file contains hidden or 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 => { | |
'use strict'; | |
// Load the sw-toolbox library. | |
importScripts('/bower_components/sw-toolbox/sw-toolbox.js'); | |
// Ensure that our service worker takes control of the page as soon as possible. | |
global.addEventListener('install', event => event.waitUntil(global.skipWaiting())); | |
global.addEventListener('activate', event => event.waitUntil(global.clients.claim())); | |
})(self); |
This file contains hidden or 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
"use strict"; | |
// Listen to fetch events | |
self.addEventListener('fetch', function(event) { | |
// Check if the image is a jpeg | |
if (/\.jpg$|.png$/.test(event.request.url)) { | |
// Inspect the accept header for WebP support | |
var supportsWebp = false; |
This file contains hidden or 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
<script> | |
// Register the service worker | |
if ('serviceWorker' in navigator) { | |
navigator.serviceWorker.register('/service-worker.js').then(function(registration) { | |
// Registration was successful | |
console.log('ServiceWorker registration successful with scope: ', registration.scope); | |
}).catch(function(err) { | |
// registration failed :( | |
console.log('ServiceWorker registration failed: ', err); | |
}); |
This file contains hidden or 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
// Listen to fetch events | |
self.addEventListener('fetch', function(event) { | |
// Clone the request | |
var req = event.request.clone(); | |
// Check if the image is a jpeg | |
if (/\.jpg$/.test(event.request.url)) { | |
// Check the headers |
This file contains hidden or 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
<script type="application/ld+json" async> | |
{ | |
"@context": "http://schema.org", | |
"@type": "Person", | |
"name": "Dean Hume", | |
"url": "http://www.deanhume.com", | |
"image" : "http://d1cf5d4f6459eaab586f-791099b254e564f039f4f8eec59a966d.r53.cf3.rackcdn.com/logo.png", | |
"sameAs" : [ | |
"https://twitter.com/DeanoHume", | |
"https://uk.linkedin.com/in/deanhume", |
This file contains hidden or 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
fetch(url, { | |
method: 'POST', | |
headers: { | |
'auth': '1234' | |
}, | |
body: JSON.stringify({ | |
name: 'dean', | |
login: 'dean', | |
}) | |
}) |