|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Optimizing Single Page App Splash Screen |
| 4 | +author: fernando |
| 5 | +--- |
| 6 | + |
| 7 | +With the growing trend of single page app/websites, initial page load tend to take a bit longer. |
| 8 | +The advantage being that after only small bits of JSON will be passed from the client/server. |
| 9 | + |
| 10 | +When I started working on Jackpine website, I paid little attention on the images |
| 11 | +which were being handed over from the designer. Pretty soon the total |
| 12 | +size for the page download was nearing 2.0 MB, 1.6 MB for images. |
| 13 | + |
| 14 | +The intial splash page background image was around 307 KB and from using the Google inspector |
| 15 | +end of the stack and would slowly reveal itself. This looked horrible. After some |
| 16 | +searching on the web on prioritizing GET requests for the background image I found |
| 17 | +that by default background images are last to build becasue they are set in the CSS. |
| 18 | +You could inline the css background but that would make the HTML structure have styling not |
| 19 | +cool for the semantics nerds. And even if you did add the inline it would still load after |
| 20 | +GETting all those huge JS libraries needed. |
| 21 | + |
| 22 | +The solution is simple just instantiate a Javascript image object with |
| 23 | +the source of the image. |
| 24 | + |
| 25 | +CODE HERE |
| 26 | + |
| 27 | +img = new Image |
| 28 | +img.src = 'image/source.png' |
| 29 | + |
| 30 | +And placing this inline at the top of the html page before any calls to the JS or CSS, GETs |
| 31 | +image before anything else. For more bonus points in quick loading don't use any jQuery, just |
| 32 | +straight JS and it will shave a few milliseconds. And for even more make a low res picture and |
| 33 | +overlap the images using the CSS background comma spearated. |
| 34 | + |
| 35 | +background-image: url(low-res.png), url(high-res.png); |
| 36 | + |
| 37 | +This will basically load the first image on the list and once the next image is complete it will |
| 38 | +overlap it in a sudden blink. The effect is pretty suddle if you aren't looking for it. |
| 39 | + |
| 40 | +Now your page might be getting somewhere and looking a little more snappy on the first laod. |
| 41 | +To really polish it off add and fade in effect to the text and your page is on its way to really |
| 42 | +pro work. Because what happend is that if you are using some fancy web font this again add to a |
| 43 | +bit of timing that cause the text to rended with some default font followed by the web font. |
| 44 | +And since the image is the screen first you might want the fade in in to avoid having a nasty |
| 45 | +flash of text. |
| 46 | + |
| 47 | +Well all this shouldn't add to much time to your project for adding some extra quality to your |
| 48 | +site. Feel free to check the source code on this site. And I hope you enjoyed this tech write up. |
| 49 | + |
0 commit comments