jrf0110 / 8track

A service worker router with async middleware and neato type-inference inspired by Koa
56 stars 6 forks source link

Generic Edge Cache Middleware #51

Open jrf0110 opened 3 years ago

jrf0110 commented 3 years ago

It shouldn't matter where the Response came from, the edge cache middleware should be able to cache it and serve it early in the middleware chain.

// These would be our cache middleware options
type CacheOptions = {
  getCacheKey?: (req: URL) => string
  cache?: typeof caches.default
  edgeTTL?: number
  browserTTL?: number
  edgeTTLForHTML?: number
  browserTTLForHTML?: number
  allowedMethods?: Method[] 
}

import { Router, cache } from '8track'

const app = new Router()

// Cache all GET requests
app.get`(.*)`.use(cache())
...