fetch-event

FetchEvent outside Service Worker

Usage no npm install needed!

<script type="module">
  import fetchEvent from 'https://cdn.skypack.dev/fetch-event';
</script>

README

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
      ))
    )
  )
))