|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// Workbox RuntimeCaching config: https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.RuntimeCachingEntry |
| 4 | +module.exports = [ |
| 5 | + { |
| 6 | + urlPattern: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i, |
| 7 | + handler: 'CacheFirst', |
| 8 | + options: { |
| 9 | + cacheName: 'google-fonts-webfonts', |
| 10 | + expiration: { |
| 11 | + maxEntries: 4, |
| 12 | + maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days |
| 13 | + }, |
| 14 | + }, |
| 15 | + }, |
| 16 | + { |
| 17 | + urlPattern: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i, |
| 18 | + handler: 'StaleWhileRevalidate', |
| 19 | + options: { |
| 20 | + cacheName: 'google-fonts-stylesheets', |
| 21 | + expiration: { |
| 22 | + maxEntries: 4, |
| 23 | + maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + urlPattern: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i, |
| 29 | + handler: 'StaleWhileRevalidate', |
| 30 | + options: { |
| 31 | + cacheName: 'static-font-assets', |
| 32 | + expiration: { |
| 33 | + maxEntries: 4, |
| 34 | + maxAgeSeconds: 7 * 24 * 60 * 60 // 7 days |
| 35 | + } |
| 36 | + } |
| 37 | + }, |
| 38 | + { |
| 39 | + urlPattern: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i, |
| 40 | + handler: 'NetworkOnly' |
| 41 | + }, |
| 42 | + { |
| 43 | + urlPattern: /\/_next\/image\?url=.+$/i, |
| 44 | + handler: "StaleWhileRevalidate", |
| 45 | + options: { |
| 46 | + cacheName: "next-image", |
| 47 | + expiration: { |
| 48 | + maxEntries: 64, |
| 49 | + maxAgeSeconds: 24 * 60 * 60, // 24 hours |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + urlPattern: /\.(?:mp3|wav|ogg)$/i, |
| 55 | + handler: 'CacheFirst', |
| 56 | + options: { |
| 57 | + rangeRequests: true, |
| 58 | + cacheName: 'static-audio-assets', |
| 59 | + expiration: { |
| 60 | + maxEntries: 32, |
| 61 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 62 | + } |
| 63 | + } |
| 64 | + }, |
| 65 | + { |
| 66 | + urlPattern: /\.(?:mp4)$/i, |
| 67 | + handler: 'CacheFirst', |
| 68 | + options: { |
| 69 | + rangeRequests: true, |
| 70 | + cacheName: 'static-video-assets', |
| 71 | + expiration: { |
| 72 | + maxEntries: 32, |
| 73 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | + { |
| 78 | + urlPattern: /\.(?:js)$/i, |
| 79 | + handler: 'StaleWhileRevalidate', |
| 80 | + options: { |
| 81 | + cacheName: 'static-js-assets', |
| 82 | + expiration: { |
| 83 | + maxEntries: 32, |
| 84 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + urlPattern: /\.(?:css|less)$/i, |
| 90 | + handler: 'StaleWhileRevalidate', |
| 91 | + options: { |
| 92 | + cacheName: 'static-style-assets', |
| 93 | + expiration: { |
| 94 | + maxEntries: 32, |
| 95 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + { |
| 100 | + urlPattern: /\/_next\/data\/.+\/.+\.json$/i, |
| 101 | + handler: "StaleWhileRevalidate", |
| 102 | + options: { |
| 103 | + cacheName: "next-data", |
| 104 | + expiration: { |
| 105 | + maxEntries: 32, |
| 106 | + maxAgeSeconds: 24 * 60 * 60, // 24 hours |
| 107 | + } |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + urlPattern: /\.(?:json|xml|csv)$/i, |
| 112 | + handler: 'NetworkFirst', |
| 113 | + options: { |
| 114 | + cacheName: 'static-data-assets', |
| 115 | + expiration: { |
| 116 | + maxEntries: 32, |
| 117 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 118 | + } |
| 119 | + } |
| 120 | + }, |
| 121 | + { |
| 122 | + urlPattern: ({url}) => { |
| 123 | + const isSameOrigin = self.origin === url.origin |
| 124 | + if (!isSameOrigin) return false |
| 125 | + const pathname = url.pathname |
| 126 | + // Exclude /api/auth/callback/* to fix OAuth workflow in Safari without impact other environment |
| 127 | + // Above route is default for next-auth, you may need to change it if your OAuth workflow has a different callback route |
| 128 | + // Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809 |
| 129 | + if (pathname.startsWith('/api/auth/')) return false |
| 130 | + if (pathname.startsWith('/api/')) return true |
| 131 | + return false |
| 132 | + }, |
| 133 | + handler: 'NetworkFirst', |
| 134 | + method: 'GET', |
| 135 | + options: { |
| 136 | + cacheName: 'apis', |
| 137 | + expiration: { |
| 138 | + maxEntries: 16, |
| 139 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 140 | + }, |
| 141 | + networkTimeoutSeconds: 10 // fall back to cache if api does not response within 10 seconds |
| 142 | + } |
| 143 | + }, |
| 144 | + { |
| 145 | + urlPattern: ({url}) => { |
| 146 | + const isSameOrigin = self.origin === url.origin |
| 147 | + if (!isSameOrigin) return false |
| 148 | + const pathname = url.pathname |
| 149 | + if (pathname.startsWith('/api/')) return false |
| 150 | + return true |
| 151 | + }, |
| 152 | + handler: 'NetworkFirst', |
| 153 | + options: { |
| 154 | + cacheName: 'others', |
| 155 | + expiration: { |
| 156 | + maxEntries: 32, |
| 157 | + maxAgeSeconds: 24 * 60 * 60 // 24 hours |
| 158 | + }, |
| 159 | + networkTimeoutSeconds: 10 |
| 160 | + } |
| 161 | + }, |
| 162 | + { |
| 163 | + urlPattern: ({url}) => { |
| 164 | + const isSameOrigin = self.origin === url.origin |
| 165 | + return !isSameOrigin |
| 166 | + }, |
| 167 | + handler: 'NetworkFirst', |
| 168 | + options: { |
| 169 | + cacheName: 'cross-origin', |
| 170 | + expiration: { |
| 171 | + maxEntries: 32, |
| 172 | + maxAgeSeconds: 60 * 60 // 1 hour |
| 173 | + }, |
| 174 | + networkTimeoutSeconds: 10 |
| 175 | + } |
| 176 | + } |
| 177 | +] |
0 commit comments