0.0.3 • Published 7 years ago

trapezoid v0.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

#Trapezoid Trapezoid is a simple web framework for service workers. It features:

  • define precached items
  • intercept GET fetches and pass custom responses
  • create custom responses for when offline only

#Install install locally using npm and refer locally from your service worker

npm install trapezoid
importScripts("node_modules/trapezoid/trapezoid.min.js");

or reference CDN file on rawgit in your service worker:

importScripts("https://cdn.rawgit.com/sigiljs/trapezoid/master/trapezoid.min.js");

#Hello World app.js

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('sw.js').then(function(registration) {
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }).catch(function(err) {
      console.log('ServiceWorker registration failed: ', err);
    });
  });
}

sw.js

// Get things started
importScripts("trapezoid.js")
var app = trapezoid();

// Create a custom GET handler
app.get("/",function(req,res){
  res.send("I replaced what you see at base url.")
})

// Declare something to precache
app.precache("/testA.json");

// Or declare many things to precache
app.precache(["/testB.json","testB.json"]);

// Create a fallback handler for when offline only
app.offline("/offline.json",function(req,res){
  res.type('application/json');
  res.send("{'online':false}");
})

// Give it a unique name for cacheing
app.run("helloworld-v1");