Skip to content

Instantly share code, notes, and snippets.

@luke-denton-aligent
Last active July 6, 2019 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luke-denton-aligent/fba261a767986fd06b7af6a99f232e64 to your computer and use it in GitHub Desktop.
Save luke-denton-aligent/fba261a767986fd06b7af6a99f232e64 to your computer and use it in GitHub Desktop.
This snippet shows how to cache and serve a skeleton page
// Offline Web Applications course on Udacity https://classroom.udacity.com/courses/ud899
//When a browser runs a service worker for the first time, an event is fired within it, 'install'. This can be used
//to trigger functions to download assets and save them to the cache, using the Cache API
//Started with snippet from https://gist.github.com/luke-denton/ef791e5150470814a7a155cd85b1bf80
var staticCacheName = "my-cache-v2"; //Update the cache version
self.addEventListener('install', function(event) {
var urlsToCache = [
'/skeleton', //Change this from / to /skeleton
'js.main.js',
'css/main.css',
'imgs/icon.png',
'https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff',
'https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff'
];
//call event.waitUntil with the same code as referenced snippet
});
self.addEventListener('fetch', function(event) {
//Respond to requests for the root page with the page skeleton from the cache
var requestUrl = new URL(event.request.url);
//Only intercept requests from the same origin (i.e. Don't intercept Google Fonts requests, or any third party request like that)
if (requestUrl.origin === location.origin) {
//Check if the request is for the root page
if (requestUrl.pathname === '/') {
//Respond with the cached skeleton, which will be there as it is now cached as part of the install step
event.respondWith(caches.match('/skeleton'));
return;
}
}
//Call respond with to handle requests that aren't from the same origin or for the root page
});
@sergio99nv
Copy link

skeleton is a folder or subfolder?

@jl-welch
Copy link

It's from a Udacity course, seems that there's a route get for '/skeleton' that just sends over what's needed.

@pvt-16
Copy link

pvt-16 commented Mar 26, 2018

where is this skeleton defined ?

@KDCinfo
Copy link

KDCinfo commented Jul 28, 2018

From the Udacity Course, "Offline Web Applications"

After a week of trying to figure out why /skeleton wasn't working on my own app... I finally figured it out!! /skeleton is a proprietary feature built specifically into this course's wittr project. Although it was introduced as though it would work anywhere, on any project, it is not, as I so wrongly derived, a magical part of the Browser's Web Cache API.

Here is the distinction between root / and /skeleton from the project's repo [server\Server.js]

    this._app.get('/', (req, res) => {
      res.send(indexTemplate({
        scripts: '<script src="/js/main.js" defer></script>',
        content: postsTemplate({
          content: this._messages.map(item => postTemplate(item)).join('')
        })
      }));
    });

    this._app.get('/skeleton', (req, res) => {
      res.send(indexTemplate({
        scripts: '<script src="/js/main.js" defer></script>',
        content: postsTemplate()
      }));
    });

Unsure how to mimick their functionality outside of their app's setup (which appears to involve a handlebar template file)... so unsure how one would apply this outside of the course's own server-run app.

@cintogia
Copy link

cintogia commented Jul 6, 2019

Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment