Giter VIP home page Giter VIP logo

effectiveweb.training's Introduction

effectiveweb.training's People

Contributors

adambien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

effectiveweb.training's Issues

Best of both worlds ...

@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:

Service Worker not caching as expected?

@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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.