effectiveweb.training
Sample application repository for effectiveweb.training.
Also checkout webstandards.igniter and webcomponents.training and other online workshops at airhacks.io.
Repository for Effective Web Online Course / airhacks.io
Sample application repository for effectiveweb.training.
Also checkout webstandards.igniter and webcomponents.training and other online workshops at airhacks.io.
@AdamBien Thanks for the great course!
I have the following issue:
Is it possible to cache with service worker only the app specific assets, but allow the browser to load, manage and cache the D3 and LIT-HTML modules? This would result in the lowest cost for your AWS network bill, especially since D3 is 0.5mg download and you do not want to serve it from your AWS servers!
Specifically, in Air Stockz, I would like to use the free unpkg CDN to download the D3 and LIT-HTML from, instead of my AWS server, like so:
import * as d3 from 'https://unpkg.com/d3?module';
import {html, render} from 'https://unpkg.com/lit-html?module';
This works nicely without service worker! The problem is that the service worker fails to handle such downloads :
lit-html:35 GET https://unpkg.com/lib/dom.js?module net::ERR_ABORTED 404
lit-html:33 GET https://unpkg.com/lib/directive.js?module net::ERR_ABORTED 404
lit-html:36 GET https://unpkg.com/lib/part.js?module net::ERR_ABORTED 404
lit-html:31 GET https://unpkg.com/lib/template-result.js?module net::ERR_ABORTED 404
lit-html:38 GET https://unpkg.com/lib/render.js?module net::ERR_ABORTED 404
lit-html:30 GET https://unpkg.com/lib/default-template-processor.js?module net::ERR_ABORTED 404
lit-html:40 GET https://unpkg.com/lib/template-instance.js?module net::ERR_ABORTED 404
lit-html:39 GET https://unpkg.com/lib/template-factory.js?module net::ERR_ABORTED 404
lit-html:42 GET https://unpkg.com/lib/template.js?module net::ERR_ABORTED 404
lit-html:37 GET https://unpkg.com/lib/parts.js?module net::ERR_ABORTED 404
so is there a way to let the browser download (and cache) standard modules like D3 and LIT-HTML outside the service worker cache:
@AdamBien
Thanks for the great course!
One issue I encountered was that loading assets upon install event did not work for me - it is very weird, the cache did get populated (as seen in the Chrome cache inspector tab), but subsequent service worker fetch events found nothing in the cache and ended up downloading assets over the network. Except for a style.css
, which is the only thing found in the cache! ...Spent a whole day hacking it, got nowhere! This might be an issue with the latest Chrome (Version 86.0.4240.80), especially if you did not see anything like this in 2019, when you recorded the course.
Here is the install event code that did not work for me:
const cacheName = 'stockz-cache-v20';
const resources = [
'./index.html',
'./style.css',
'./app.js',
'./AirNav.js',
'./AirSlot.js',
'./AirCrumb.js',
'./AirUpdate.js',
'./views/TotalView.js',
'./views/AirElement.js',
'./views/AddView.js',
'./views/ListView.js',
'./views/OverView.js',
'./views/AboutView.js',
'./views/Stocks.js',
'./web_modules/d3.js',
'./web_modules/lit-html.js'
];
const prefetch = (name) => caches.open(name).then(cache => cache.addAll(resources));
//Install - the cache does get populated as seen in the Chrome inspector console, but subsequent fetch events cannot find items in //the cache!
self.addEventListener('install', e => {
//console.log("worker install - prefetch")
self.skipWaiting();
e.waitUntil(prefetch(cacheName));
});
What works though, is lazily populating the cache after fetch fails to find the asset in the cache
self.addEventListener('fetch', event => {
const {request} = event;
event.respondWith(caches
.match(request)
.then(response => {
if (response) {
//console.log(`worker fetch from cache ${request.url}`);
return response;
}
//console.log(`worker fetch missed in cache ${request.url}`);
return fetch(event.request).then(
response => {
// -Ensure the response is valid.
// -Check the status is 200 on the response.
// -Make sure the response type is basic, which indicates that it's a request from our origin. This means that requests to third party assets aren't cached as well.
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// IMPORTANT: Clone the response. A response is a stream
// and because we want the browser to consume the response
// as well as the cache consuming the response, we need
// to clone it so we have two streams.
const responseToCache = response.clone();
caches.open(cacheName)
.then(cache => {
cache.put(event.request, responseToCache);
//console.log(`added to cache ${request.url}`);
});
return response;
}
);
})
);
});
I wonder if you have time to re-test this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.