fetch-event

v1.0.0-alpha
FetchEvent outside Service Worker
fetch event fetchevent

fetch-event

FetchEvent outside Service Worker

Simple http mocking

var nodeFetch = require('node-fetch')
var {fetch, Response} = require('./node')(nodeFetch)

fetch.on('fetch', event => {
  console.log(event.request.url) // http://example.com
  event.respondWith(new Response('hello'))
})

fetch('http://example.com')
.then(res => res.text())
.then(console.log) // hello

Using fetch-cachestorage you can create your own cache mechanism

var nodeFetch = require('node-fetch')
var {fetch, Response} = require('./node')(nodeFetch)

/*
 * Will see if it exist in the cache, if not it will fetch it
 * store it, and respond with a copy - secound time it will load
 * from the cache
 */
fetch.on('fetch', event => event.respondWith(
  caches.match(event.request).then(res =>
    res || nodeFetch(event.request).then(res =>
      caches.open('v1').then(cache => (
        cache.put(event.request, res.clone()), res
      ))
    )
  )
))
npm i fetch-event

Metadata

  • MIT
  • Whatever
  • Jimmy Wärting
  • released 12/8/2016

Downloads

Maintainers