Options
All
  • Public
  • Public/Protected
  • All
Menu

Handles prefetching and caching in the the service worker. Responses are cached based on the browser.serviceWorkerSeconds cache setting in your routes.

Example routes.js

 import { Router } from '@layer0/core/router'

 module.exports = new Router()
   .match('/some/path', ({ cache }) => {
     cache({
       edge: {
         maxAgeSeconds: 60 * 60 * 24 // one day
       },
       browser: {
         serviceWorkerSeconds: 60 * 60 // one hour
       }
     })
   })

Example Service Worker

 import { skipWaiting, clientsClaim } from 'workbox-core'
 import { Prefetcher } from '@layer0/prefetch/sw'
 import { precacheAndRoute } from 'workbox-precaching'

 skipWaiting()
 clientsClaim()
 precacheAndRoute(self.__WB_MANIFEST || [])

 new Prefetcher().route()

Hierarchy

  • Prefetcher

Index

Constructors

constructor

Methods

cache

  • cache(pattern: RegExp, maxAgeSeconds?: number): this
  • Parameters

    • pattern: RegExp
    • Default value maxAgeSeconds: number = 60 * 60 * 24

    Returns this

getRouteConfigsFromManifest

  • getRouteConfigsFromManifest(): { convertToGet: any; criteriaPath: any; edgeMaxAgeSeconds: any; isPartialPathRoute: true; key: any; maxAgeSeconds: any; method: any; pattern: any; prefetchUpstreamRequests: any; returnsResponse: any }[]
  • Returns { convertToGet: any; criteriaPath: any; edgeMaxAgeSeconds: any; isPartialPathRoute: true; key: any; maxAgeSeconds: any; method: any; pattern: any; prefetchUpstreamRequests: any; returnsResponse: any }[]

route

  • route(): this
  • Adds a fetch event listener that serves content from the browser cache when available. When a request is not in the cache, it will be fetched from the network and added to the cache if the route has browser.serviceWorkerSeconds defined in its cache setting or if the request is a prefetch.

    Returns this

Generated using TypeDoc