55const GITHUB_API_BASE = 'https://api.github.com' ;
66const GITHUB_API_CACHE = 'github-api-cache-v2' ;
77const STATIC_CACHE = 'static-cache-v2' ;
8+ const EXTERNAL_CACHE = 'external-cache-v1' ;
89const TOKEN_STORE = 'github-token-store' ;
910const STATIC_ASSETS = [ '/' , '/index.html' , '/sw.js' ] ;
1011
@@ -33,7 +34,7 @@ self.addEventListener('activate', (event) => {
3334 caches . keys ( ) . then ( cacheNames => {
3435 return Promise . all (
3536 cacheNames . map ( cacheName => {
36- if ( cacheName !== GITHUB_API_CACHE && cacheName !== STATIC_CACHE ) {
37+ if ( cacheName !== GITHUB_API_CACHE && cacheName !== STATIC_CACHE && cacheName !== EXTERNAL_CACHE ) {
3738 console . log ( 'Deleting old cache:' , cacheName ) ;
3839 return caches . delete ( cacheName ) ;
3940 }
@@ -48,12 +49,50 @@ self.addEventListener('activate', (event) => {
4849self . addEventListener ( 'fetch' , ( event ) => {
4950 const url = new URL ( event . request . url ) ;
5051
52+ // Don't intercept non-GET requests to our own origin (they should go directly)
53+ if ( url . origin === location . origin && event . request . method !== 'GET' ) {
54+ return ;
55+ }
56+
5157 // Handle all GitHub API requests
5258 if ( url . origin === GITHUB_API_BASE ) {
5359 event . respondWith ( handleGitHubRequest ( event . request ) ) ;
60+ return ;
5461 }
55- // Handle static assets with network-first strategy
56- else if ( event . request . method === 'GET' ) {
62+
63+ // Allow external resources to pass through without caching scripts
64+ // Only handle if method is GET
65+ if ( event . request . method === 'GET' ) {
66+ // For gravatar images - cache them, but let umnico and other scripts pass through
67+ if ( ( url . hostname . includes ( 'gravatar.com' ) || url . hostname . includes ( 'www.gravatar.com' ) ) &&
68+ event . request . destination === 'image' ) {
69+ event . respondWith (
70+ caches . open ( EXTERNAL_CACHE ) . then ( cache => {
71+ return cache . match ( event . request ) . then ( cached => {
72+ return fetch ( event . request )
73+ . then ( response => {
74+ if ( response && response . status === 200 ) {
75+ cache . put ( event . request , response . clone ( ) ) ;
76+ }
77+ return response ;
78+ } )
79+ . catch ( ( ) => cached || new Response ( '' , { status : 204 } ) ) ;
80+ } ) ;
81+ } )
82+ ) ;
83+ return ;
84+ }
85+
86+ // For umnico and other external resources - network only, don't cache scripts
87+ if ( url . hostname . includes ( 'umnico.com' ) || url . hostname . includes ( 'www.umnico.com' ) ) {
88+ event . respondWith (
89+ fetch ( event . request )
90+ . catch ( ( ) => new Response ( '' , { status : 204 } ) )
91+ ) ;
92+ return ;
93+ }
94+
95+ // Handle static assets with network-first strategy
5796 event . respondWith ( handleStaticRequest ( event . request ) ) ;
5897 }
5998} ) ;
0 commit comments