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
// If we don't have support for intersection observer, load the images immediately | |
if (!('IntersectionObserver' in window)) { | |
Array.from(images).forEach(image => preloadImage(image)); | |
} else { | |
// It is supported, load the images | |
observer = new IntersectionObserver(onIntersection, config); | |
images.forEach(image => { | |
observer.observe(image); | |
}); |
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
function onIntersection(entries) { | |
// Loop through the entries | |
entries.forEach(entry => { | |
// Are we in viewport? | |
if (entry.intersectionRatio > 0) { | |
// Stop watching and load the image | |
observer.unobserve(entry.target); | |
preloadImage(entry.target); | |
} |
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
// Get all of the images that are marked up to lazy load | |
const images = document.querySelectorAll('.js-lazy-image'); | |
const config = { | |
// If the image gets within 50px in the Y axis, start the download. | |
rootMargin: '50px 0px', | |
threshold: 0.01 | |
}; | |
// The observer for the images on the page | |
let observer = new IntersectionObserver(onIntersection, config); |
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
importScripts('path/to/offline-google-analytics-import.js'); | |
workbox.googleAnalytics.initialize(); |
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
importScripts('workbox-sw.prod.v1.1.0.js'); | |
const workboxSW = new self.WorkboxSW(); | |
workboxSW.precache(fileManifest); | |
// The route for any requests from the googleapis origin | |
workboxSW.router.registerRoute('https://fonts.googleapis.com/(.*)', | |
workboxSW.strategies.cacheFirst({ | |
cacheName: 'googleapis', | |
cacheableResponse: { |
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
if (navigator.storage && navigator.storage.persist) { | |
navigator.storage.persist().then(granted => { | |
if (granted) { | |
alert("Storage will persist and not be cleared"); | |
} else { | |
alert("Storage won’t persist and may be cleared"); | |
} | |
}); | |
}; |
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
var image = document.getElementById('image'); | |
// Does the current browser support the text detection API? | |
if (window.TextDetector) { | |
let textDetector = new TextDetector(); | |
textDetector.detect(theImage) | |
.then(detectedTextBlocks => { | |
// Loop through the results | |
for (const textBlock of detectedTextBlocks) { |
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
var image = document.getElementById('image'); | |
// Does the current browser support the API? | |
if (window.FaceDetector) { | |
var faceDetector = new FaceDetector(); | |
faceDetector.detect(image) | |
.then(faces => { | |
console.log(‘Faces found:’, faces.length); |
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
const app = express(); | |
const timings = require('server-timings'); | |
app.use(timings.start('routing')); | |
app.use('/', require('./routes/home.js')); | |
app.use(timings.end('routing')); |