Middleware

Rafał Lorenz edited this page Jan 30, 2018 · 8 revisions

peer-cdn allows you to hook middleware for fetch event. Middleware can be object, class or anything that expose get property which is a function. It can also expose put property also as a function.

How does it work ?

Middleware will be called with a given Strategy up until one of them returns response different then null, allowing service worker to return a response to a browser.

Example middleware:

function (request)
{
   get: () => {
      // get response from cache etc

      // If this method returns response or anything
      // different then null next middleware will not be called
      // and response will be returned . by service worker
      return null;
   },
   put: response => {
      // cache response or something else with it
   }
}

Register middleware

To add middleware simply pass them when registering asset's route for peer-cdn to take care of.

const middleware = []; // array of your middleware
PeerCDN.GET("/", STRATEGIES.ordered, ...middleware);

You can use spread operator or pass each middleware as arguments following strategy.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.