fallback.js 1.1 KB

123456789101112131415161718192021222324252627
  1. 'use strict'
  2. self.fallback = async request => {
  3. // https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination
  4. switch (request.destination) {
  5. case 'document':
  6. if (process.env.__PWA_FALLBACK_DOCUMENT__)
  7. return caches.match(process.env.__PWA_FALLBACK_DOCUMENT__, { ignoreSearch: true })
  8. case 'image':
  9. if (process.env.__PWA_FALLBACK_IMAGE__)
  10. return caches.match(process.env.__PWA_FALLBACK_IMAGE__, { ignoreSearch: true })
  11. case 'audio':
  12. if (process.env.__PWA_FALLBACK_AUDIO__)
  13. return caches.match(process.env.__PWA_FALLBACK_AUDIO__, { ignoreSearch: true })
  14. case 'video':
  15. if (process.env.__PWA_FALLBACK_VIDEO__)
  16. return caches.match(process.env.__PWA_FALLBACK_VIDEO__, { ignoreSearch: true })
  17. case 'font':
  18. if (process.env.__PWA_FALLBACK_FONT__)
  19. return caches.match(process.env.__PWA_FALLBACK_FONT__, { ignoreSearch: true })
  20. case '':
  21. if (process.env.__PWA_FALLBACK_DATA__ && request.url.match(/\/_next\/data\/.+\/.+\.json$/i))
  22. return caches.match(process.env.__PWA_FALLBACK_DATA__, { ignoreSearch: true })
  23. default:
  24. return Response.error()
  25. }
  26. }