├── LICENSE ├── README.md ├── assets └── svg │ ├── approval.svg │ ├── bubbles.svg │ ├── cog.svg │ ├── heart.svg │ ├── sign-check.svg │ ├── sign-error.svg │ ├── star.svg │ ├── svg-built.svg │ └── turntable.svg ├── index.html └── service-worker.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jaume Sanchez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Optimising SVG load with Service Workers 2 | 3 | This demo goes with the article "[Optimising SVG load with Service Worker](https://clicktorelease.com/blog/optimise-svg-load-service-worker)". 4 | 5 | If it works, you should see green dots on all images but the heart one. 6 | 7 | Supported browser: latest Chrome, Firefox and Opera. 8 | 9 | The first time the site runs, it will install the Service Worker, so it won't be ready right away: none of the images will have a green dot. 10 | 11 | The next reload, the SW should be installed and running, and the images should have the green dot. 12 | 13 | 14 | #### License #### 15 | 16 | MIT licensed 17 | 18 | Copyright (C) 2015 Jaume Sanchez Elias, http://www.clicktorelease.com 19 | -------------------------------------------------------------------------------- /assets/svg/approval.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/bubbles.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/cog.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/heart.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/sign-check.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/sign-error.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/star.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /assets/svg/svg-built.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /assets/svg/turntable.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Service Workers and SVG loading 6 | 7 | 8 | 23 | 24 | 25 | 26 | 27 | 28 |

Optimising SVG load with Service Workers

29 | 30 |

This demo goes with the article "Optimising SVG load with Service Worker".

31 |

If it works, you should see green dots on all images but the heart one.
Supported browser: latest Chrome (Desktop and Android), Firefox (Desktop enabling flags) and Opera.

32 |

The first time the site runs, it will install the Service Worker, so it won't be ready right away: none of the images will have a green dot.
The next reload, the SW should be installed and running, and the images should have the green dot. 33 | 34 |

35 | 36 |
37 |

HTML

38 |

This is an <img> tag, and the src is in the master:

39 |

This is an <img> tag, and the src is not in the master:

40 |

This is an <iframe>, and the src is in the master:

41 |

JavaScript

42 |
43 | 44 |

The SVG images processed by the Service Worker show a green dot on the top left corner.

45 |

Icons from small-n-flat set.

46 | 47 | 48 | 49 | 83 | 84 | 85 | 143 | 144 | -------------------------------------------------------------------------------- /service-worker.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | this.addEventListener('install', function(event) { 4 | event.waitUntil( 5 | caches.open('v1').then(function(cache) { 6 | return cache.addAll([ 7 | 'index.html', 8 | 'assets/svg/svg-built.svg' 9 | ]); 10 | }) 11 | ); 12 | }); 13 | 14 | this.addEventListener('fetch', function(event) { 15 | 16 | // the request URL is to a file with SVG extension 17 | 18 | if (/\.svg$/.test( event.request.url ) ) { 19 | 20 | console.log( 'Trying to fetch an SVG: ', event.request.url ) 21 | 22 | // we'll respond the fetch request with the fullfilment of the SVGPromise 23 | 24 | event.respondWith( 25 | 26 | SVGPromise( event ).then( function( r ) { 27 | 28 | // successfully retrieved the SVG file from master file in cache 29 | 30 | return r; 31 | 32 | } ).catch( function( e ) { 33 | 34 | // couldn't find the file in the master file 35 | 36 | console.log( 'ERROR: ', e ); 37 | 38 | // fetch from server normally 39 | 40 | return fetch( event.request.url ); 41 | 42 | } ) 43 | 44 | ); 45 | 46 | } 47 | 48 | } ); 49 | 50 | function matchInString( regExp, string ) { 51 | 52 | var m; 53 | 54 | while ((m = regExp.exec(string)) !== null) { 55 | if (m.index === regExp.lastIndex) regExp.lastIndex++; 56 | return m; 57 | } 58 | 59 | return null; 60 | 61 | } 62 | 63 | function SVGPromise( event ) { 64 | 65 | // we create the Promise object that will get returned 66 | 67 | var promise = new Promise( function(resolve, reject) { 68 | 69 | // retrieve from cache the master svg file 70 | 71 | var r = new Request( 'assets/svg/svg-built.svg' ); 72 | 73 | caches.match( r ).then( function( response ) { 74 | 75 | return response.text(); 76 | 77 | } ).then( function( svgMaster ) { 78 | 79 | // master file was successfully retrieved from cache 80 | // svgMaster is the text content of the file 81 | 82 | // extract the name of the file from the request 83 | // i.e. http://www.domain.com/folder/assets/file.svg to file.svg 84 | 85 | var id = matchInString( /.*\/(.*)/gmi, event.request.url )[ 1 ]; 86 | 87 | // try to extract the svg tag with the id attribute with the name 88 | 89 | var regExp = new RegExp( '()', 'gi' ); 90 | var res = matchInString( regExp, svgMaster ); 91 | 92 | if( res ) { 93 | 94 | // we found it, we can return the node as am SVG file 95 | // check what happens in SVGPromise.then 96 | var code = res[ 0 ]; 97 | code = code.replace( /<\/svg>/gmi, '' ); 98 | 99 | var svgResponse = new Response( code, { headers: { 'Content-Type': 'image/svg+xml' } } ); 100 | resolve( svgResponse ); 101 | 102 | } else { 103 | 104 | // we didn't find it, the promise is rejected 105 | // check what happens in the SVGPromise.catch 106 | 107 | reject( id + ' is not on master' ); 108 | 109 | } 110 | 111 | } ); 112 | 113 | } ); 114 | 115 | return promise; 116 | 117 | } --------------------------------------------------------------------------------