简体   繁体   English

预加载外部javascript,图像和CSS文件

[英]Preloading external javascripts, images and css files

I have been working on a web application which (despite my best efforts) has been steadily getting bigger and bigger with more and more scripts, images and css files being called. 我一直在开发一个Web应用程序,尽管我尽了最大努力,但随着越来越多的脚本,图像和css文件被调用,它一直在稳步增长。

Being a web application I think it is perfectly acceptable for a pre-loading screen to appear after the user has logged in and before they reach the dashboard. 作为Web应用程序,我认为在用户登录后且到达仪表板之前出现预加载屏幕是完全可以接受的。 On this page I want to call all external scripts, css files and images to be downloaded to the browser so that they are pulled from local cache rather than being downloaded on demand during the running of the application. 在此页面上,我想调用所有要下载到浏览器的外部脚本,css文件和图像,以便从本地缓存中提取它们,而不是在应用程序运行期间按需下载。

I am aware that there are billions of image preloader scripts, but I need to go one step further, with everything (mainly external scripts) being called on the page to be preloaded. 我知道有数十亿个图像预加载器脚本,但是我需要更进一步,在要预加载的页面上调用所有内容(主要是外部脚本)。

Thanks, Tim 谢谢,蒂姆

http://requirejs.org/ for script loading. http://requirejs.org/用于脚本加载。 Not sure about for css. 不确定CSS。 Also, not sure what platform you are on, but jammit for rails is pretty great for asset management. 另外,不确定您使用的平台是什么,但是对于Rails来说, 干扰对于资产管理来说是非常好的。

Although I havent seen a question in there, I am assuming we are talking about speeding up a web page loads. 尽管我在那里没有看到任何问题,但我假设我们正在谈论加快网页加载速度。

Having a preload page is a little pointless since the script ultimately has to be downloaded, so why bother sending the user to a pre load page. 有一个预加载页面是没有意义的,因为最终必须下载脚本,所以为什么要麻烦用户将其发送到预加载页面。 You simply incur additional redirect requests. 您只需招致其他重定向请求。

You got 3 main ways to reduce load times: 您有3种减少加载时间的主要方法:

  • Reduce HTTP Requests 减少HTTP请求
  • Reduce download size 减少下载大小
  • Move static content closer to the client 将静态内容移近客户端

Combres (.NET) is great and handles concatination and minification of scripts and CSS. Combres(.NET)很棒,可以处理脚本和CSS的缩写和缩小。 I am sure there is something similar for other tech stacks. 我确信其他技术堆栈也有类似的东西。 This limits the number of files downloaded (also providing a minification hook) 这限制了下载文件的数量(还提供了一个缩小挂钩)

Also make sure you are using CSS sprites. 还要确保您使用的是CSS Sprite。 This limits the number of images downloaded 这限制了下载的图像数量

As far as size goes: 就大小而言:

You havent mentioned the tech stack you are on, but try look at some script minification libraries (YUI Compressor, etc). 您还没有提到您所使用的技术堆栈,但是请尝试查看一些脚本缩小库(YUI Compressor等)。

Also make sure you are using GZip (via either IIS config or on hardware via load balancer or whatever) 另外,请确保您使用的是GZip(通过IIS配置或通过负载平衡器或任何其他方式在硬件上)

There is also several javascript techniques that you can use to perform JIT downloads of scripts and basically prioritize the user exeprience. 您还可以使用多种JavaScript技术来执行JIT脚本下载,并从根本上确定用户的优先级。 Steve Souders has some great books on these techniques 史蒂夫·索德斯(Steve Souders)有一些关于这些技术的好书

After these approaches you then start to look for hardware solutions like CDN's, etc. These ensure your content sits close to your target audience for faster downloads. 使用这些方法之后,您便开始寻找CDN等硬件解决方案。这些确保您的内容位于目标受众附近,以加快下载速度。

Yahoo have some great guidance on this: http://developer.yahoo.com/performance/rules.html 雅虎对此有一些很好的指导: http : //developer.yahoo.com/performance/rules.html

You can : 您可以 :

  1. Divide the page in two iframes or DIV elements 将页面分为两个iframe或DIV元素
  2. On page load, just show iframe that has a "loading..." animation 在页面加载时,只需显示具有“正在加载...”动画的iframe
  3. Second iframes's source URL should be blank to begin with. 第二个iframe的源URL首先应为空。 On page load, set to actual page URL. 在页面加载时,设置为实际页面URL。 This iframe should be hidden. 该iframe应该被隐藏。
  4. On Completion of load for this iframe, hide the "loading" iframe and show this one. 在完成此iframe的加载后,隐藏“正在加载”的iframe并显示该iframe。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM