Skip to content

jimmywarting/fetch-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fetch-event

FetchEvent outside Service Worker

npm Package

Simple http mocking

var nodeFetch = require('node-fetch')
var {fetch, Response} = require('fetch-event')(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('fetch-event')(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
      ))
    )
  )
))

About

FetchEvent outside Service Worker

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published