Skip to content

Instantly share code, notes, and snippets.

View thepassle's full-sized avatar

Pascal Schilp thepassle

  • Tranquility Base Hotel & Casino
View GitHub Profile
@thepassle
thepassle / MyComponent.js
Last active September 22, 2019 14:26
Add shadowDOM to a Preact Hooks component and use Constructible StyleSheets if available
import { Shadow, css } from './Shadow.js';
import { useState } from './web_modules/preact/hooks.js';
import { html } from '../web_modules/htm/preact.js';
const styles = css`
h1 { color: red; }
`;
@thepassle
thepassle / asd.js
Last active October 28, 2019 13:10
elements.forEach((item) => {
item.shadowRoot.querySelector('expandable-item').removeAttribute('expanded');
item.shadowRoot.querySelector('expandable-item').removeAttribute('transitioning');
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:12
strategies3
// cache only
self.addEventListener(‘fetch’, function(event) {
// If a match isn’t found in the cache, the response
// will look like a connection error
event.respondWith(caches.match(event.request));
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:14
strategies5
// network only
self.addEventListener(‘fetch’, function(event) {
event.respondWith(fetch(event.request));
// or simply don’t call event.respondWith, which
// will result in default browser behaviour
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:15
strategies7
// cache first
self.addEventListener(‘fetch’, function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:15
strategies9
// network first
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return caches.match(event.request);
})
);
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:16
strategies11
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open('my-app').then(function(cache) {
return cache.match(event.request).then(function(response) {
const fetchPromise = fetch(event.request).then(function(networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
})
return response || fetchPromise;
})
@thepassle
thepassle / index.html
Created October 28, 2019 14:17
lifecycle2
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./sw.js');
});
}
</script>
@thepassle
thepassle / sw.js
Created October 28, 2019 14:17
lifecycle3
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHENAME).then(cache => {
return cache.addAll([
'./index.html',
'./main.4c088efe.js',
'./about.cd95a028.js',
'./contact.d0b533e7.js',
'./offline.eab917fd.js',
]);
@thepassle
thepassle / sw.js
Created October 28, 2019 14:18
lifecycle6
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => cacheName.startsWith('my-cache-v')
&& cacheName !== CACHENAME)
.map(cacheName => caches.delete(cacheName))
)
})