Improve caching
This commit is contained in:
		
							parent
							
								
									90d1435fb8
								
							
						
					
					
						commit
						f3d474a9c7
					
				
					 1 changed files with 28 additions and 28 deletions
				
			
		
							
								
								
									
										56
									
								
								sw.js
									
										
									
									
									
								
							
							
						
						
									
										56
									
								
								sw.js
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -12,52 +12,48 @@ const CACHE_FIRST = [
 | 
			
		|||
	'/common/back.svg',
 | 
			
		||||
 | 
			
		||||
	// Utility JS
 | 
			
		||||
	'/common/worker.js',
 | 
			
		||||
	'/common/items.js',
 | 
			
		||||
	'/common/back.js',
 | 
			
		||||
	'/common/components.js',
 | 
			
		||||
	'/common/items.js',
 | 
			
		||||
	'/common/tabs.js',
 | 
			
		||||
	'/common/trainId.js',
 | 
			
		||||
	'/common/worker.js',
 | 
			
		||||
 | 
			
		||||
	// Base
 | 
			
		||||
	'/base.css',
 | 
			
		||||
	'/base.dark.css',
 | 
			
		||||
 | 
			
		||||
	// Pages
 | 
			
		||||
	'/index.html',
 | 
			
		||||
	'/index.js',
 | 
			
		||||
 | 
			
		||||
	'/about.html',
 | 
			
		||||
	'/about.js',
 | 
			
		||||
 | 
			
		||||
	'/train.html',
 | 
			
		||||
	'/train.js',
 | 
			
		||||
 | 
			
		||||
	'/view-train.html',
 | 
			
		||||
	'/view-train.js',
 | 
			
		||||
	'/view-train.css',
 | 
			
		||||
	'/view-train.dark.css',
 | 
			
		||||
 | 
			
		||||
	'/station.html',
 | 
			
		||||
	'/station.js',
 | 
			
		||||
 | 
			
		||||
	'/view-station.html',
 | 
			
		||||
	'/view-station.js',
 | 
			
		||||
	'/view-station.css',
 | 
			
		||||
	'/view-station.dark.css',
 | 
			
		||||
 | 
			
		||||
	'/config-route.html',
 | 
			
		||||
	'/config-route.js',
 | 
			
		||||
	'/config-route.css',
 | 
			
		||||
 | 
			
		||||
	'/index.html',
 | 
			
		||||
	'/index.js',
 | 
			
		||||
 | 
			
		||||
	'/route.html',
 | 
			
		||||
	'/route.js',
 | 
			
		||||
	'/route.css',
 | 
			
		||||
	'/route.dark.css',
 | 
			
		||||
 | 
			
		||||
	// API
 | 
			
		||||
	API_TRAINS,
 | 
			
		||||
	API_STATIONS,
 | 
			
		||||
	// API_ITINERARIES,
 | 
			
		||||
	'/station.html',
 | 
			
		||||
	'/station.js',
 | 
			
		||||
 | 
			
		||||
	'/train.html',
 | 
			
		||||
	'/train.js',
 | 
			
		||||
 | 
			
		||||
	'/view-station.html',
 | 
			
		||||
	'/view-station.js',
 | 
			
		||||
	'/view-station.css',
 | 
			
		||||
	'/view-station.dark.css',
 | 
			
		||||
 | 
			
		||||
	'/view-train.html',
 | 
			
		||||
	'/view-train.js',
 | 
			
		||||
	'/view-train.css',
 | 
			
		||||
	'/view-train.dark.css',
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -140,7 +136,9 @@ const putInCache = async (request, response) => {
 | 
			
		|||
 | 
			
		||||
const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => {
 | 
			
		||||
	// First try to get the resource from the cache
 | 
			
		||||
	const responseFromCache = await caches.match(request)
 | 
			
		||||
	const responseFromCache = await caches.match(request, {
 | 
			
		||||
		ignoreSearch: true,
 | 
			
		||||
	})
 | 
			
		||||
	if (responseFromCache) {
 | 
			
		||||
		if (refreshAnyway || (responseFromCache.headers.has('SW-Cached-At') && Date.now() - new Date(responseFromCache.headers.get('SW-Cached-At')).valueOf() > 86400000)) {
 | 
			
		||||
			console.log('[cf] using cache response; refreshing anyway but returning cache', responseFromCache);
 | 
			
		||||
| 
						 | 
				
			
			@ -232,7 +230,9 @@ const networkFirst = async ({ request, preloadResponsePromise }) => {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	// Response from network wasn't ok, try to find in cache
 | 
			
		||||
	const responseFromCache = await caches.match(request)
 | 
			
		||||
	const responseFromCache = await caches.match(request, {
 | 
			
		||||
		ignoreSearch: true,
 | 
			
		||||
	})
 | 
			
		||||
	if (responseFromCache) {
 | 
			
		||||
		console.log('[nf] using cache response', responseFromCache)
 | 
			
		||||
		return responseFromCache
 | 
			
		||||
| 
						 | 
				
			
			@ -255,7 +255,7 @@ self.addEventListener('fetch', (event) => {
 | 
			
		|||
			cacheFirst({
 | 
			
		||||
				request: event.request,
 | 
			
		||||
				preloadResponsePromise: event.preloadResponse,
 | 
			
		||||
				refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url.split('?')[0]),
 | 
			
		||||
				refreshAnyway: [].includes(event.request.url.split('?')[0]),
 | 
			
		||||
			})
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue