简体   繁体   English

有没有办法改善javascript(Dojo)加载?

[英]Are there ways to improve javascript (Dojo) loading?

I'm starting to use the Dojo toolkit and it has rich features like Dijits and themes which are useful but take forever to load. 我开始使用Dojo工具包,它具有丰富的功能,如Dijits和主题,这些功能很有用但需要永远加载。

I've got a good internet connection but those with slower connections would experience rather slow page loads. 我有一个很好的互联网连接,但那些连接速度较慢的人会遇到相当慢的网页加载。

This is also a question about heavy vs light frameworks. 这也是关于重型和轻型框架的问题。 If you make heavy use of widgets, what are some techniques to keep page load times down? 如果大量使用小部件,有哪些技术可以缩短页面加载时间?

Dojo has a build system that will drastically improve load times. Dojo有一个构建系统,可以大大缩短加载时间。 Take a look at one of the dojo books or the online docs & look at layered builds. 看一下dojo书籍或在线文档,看看分层构建。 In order to do a build, you need to have the "source" (or "full") version of dojo, which has the build tool included -- you can tell if you have this by the presence of the 'util' directory (which is at the same level as dojo, dijit, dojox). 为了进行构建,你需要拥有包含构建工具的dojo的“源”(或“完整”)版本 - 你可以通过'util'目录的存在来判断你是否拥有它(与dojo,dijit,dojox处于同一水平。 If you don't have the full version, go back to the dojo site & delve down into the download area -- it's not completely obvious perhaps. 如果您没有完整版本,请返回dojo站点并深入研究下载区域 - 这可能并不完全明显。

Anyway, if you have the right version, you basically just need to make a "build profile" file (or files ... aka a layered build), which is essentially your list of dojo.requires that you would normally have in your html. 无论如何,如果你有正确的版本,你基本上只需要制作一个“构建配置文件”文件(或文件......又称为分层构建),这实际上是你通常在html中拥有的dojo.requires列表。 The build system will jam all the javascript code for all the dijits, dojox stuff, etc. together into a "layered build" (a file) and it will run shrinksafe on it, which sort of minifies the code (removes whitespace, shortens names, etc). 构建系统会将所有dijits,dojox等所有javascript代码一起插入“分层构建”(文件)中,并在其上运行shrinksafe,这样可以缩小代码(删除空格,缩短名称)等)。 It will also do some of this to the css files. 它也会对css文件执行一些操作。 Aside from making things much smaller, you get just a single file for all the js code (or a few files if you do more than one layer, but most of the time a single layer suffices). 除了使事情变得更小之外,您只获得所有js代码的单个文件(如果您执行多个图层,则只获得一些文件,但大多数情况下单个图层就足够了)。

This will improve your load times at least ten-fold, if not more. 如果不是更多,这将使您的加载时间至少增加十倍。 It might take you a bit of reading to get down the format of the profiles and the build command itself, but it's not too hard really. 可能需要一些阅读才能了解配置文件的格式和构建命令本身,但实际上并不太难。 Once you create a build file, name it something obvious like "mystuff" and then you can dojo.require the "mystuff" file (which will be in the new build directory that is created when you build, then underneath that & hanging out with the dojo.js file in the dojo directory). 创建构建文件后,将其命名为“mystuff”,然后你可以dojo.require“mystuff”文件(它将在你构建时创建的新构建目录中,然后在其下面并与dojo目录中的dojo.js文件)。 Requiring in your built file will satisfy all the dojo.require's you normally do (assuming you have them all listed in the profile to build) and things will load very fast. 在您构建的文件中要求将满足您通常所做的所有dojo.require(假设您已将它们全部列在要构建的配置文件中)并且事情将加载非常快。

Here's a link to the older build docs, which mostly still hold true: http://www.dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds 这里是旧版构建文档的链接,大部分内容仍然适用: http//www.dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom -builds

Here's the updated docs (though perhaps a little incomplete): docs.dojocampus.org/build/index 这是更新的文档(虽然可能有点不完整):docs.dojocampus.org/build/index

It reads harder than it really is ... use the layer.profile file in the profiles directory as a starting point. 它读起来比实际更难...使用profiles目录中的layer.profile文件作为起点。 Just put a couple of things & then do a build & see if you get the release directory created (which should be at the same level as dojo, dijit, etc.) and it will have the entire dojo system in it (all minified) as well as your built (layered) stuff. 只需添加一些内容然后进行构建,看看是否创建了发布目录(应该与dojo,dijit等处于同一级别),并且它将包含整个dojo系统(全部缩小)以及你构建的(分层)东西。 Much faster. 快多了。

Dylan Tynan 迪伦泰南

It's not that big (28k gzipped). 它不是那么大(28k gzipped)。 Nevertheless you can use Google's hosted version of Dojo. 不过,您可以使用Google的托管版 Dojo。 Many of your users will already have it cached. 您的许多用户已经将其缓存。

Once you create a build file, name it something obvious like "mystuff" and then you can dojo.require the "mystuff" file (which will be in the new build directory that is created when you build, then underneath that & hanging out with the dojo.js file in the dojo directory). 创建构建文件后,将其命名为“mystuff”,然后你可以dojo.require“mystuff”文件(它将在你构建时创建的新构建目录中,然后在其下面并与dojo目录中的dojo.js文件)。 Requiring in your built file will satisfy all the dojo.require's you normally do (assuming you have them all listed in the profile to build) and things will load very fast 在您构建的文件中要求将满足您通常所做的所有dojo.require(假设您已将它们全部列在要构建的配置文件中)并且事情将加载得非常快

Slight correction -- you don't dojo.require that file, you reference it in an ordinary script tag. 稍微纠正 - 你没有dojo。需要那个文件,你在一个普通的脚本标签中引用它。

<script type="text/javascript" src="js/dojo/dojo/dojo.js" ></script>
<script type="text/javascript" src="js/dojo/mystuff/mystuff.js"></script>

For the directory layout I put the built file "mystuff.js" into the same directory as my package. 对于目录布局,我将构建的文件“mystuff.js”放入与我的包相同的目录中。 So at the same level as dojo, dojox, and dijit, I would have a directory named "mystuff", and within that I have MyClass1.js and MyClass2.js. 所以在与dojo,dojox和dijit相同的级别,我会有一个名为“mystuff”的目录,并且在其中我有MyClass1.js和MyClass2.js。 Then a fragment from the profile.js file for the build looks like: 然后,构建的profile.js文件中的片段如下所示:

layers:[
{
name: "../mystuff/mystuff.js",
dependencies: [
  "mystuff.MyClass1",
  "mystuff.MyClass2"
  ]
},...

I know this is an old thread. 我知道这是一个老线程。 But I'm posting this answer for the benefit of other users like me who may read this. 但我发布这个答案是为了让像我这样的其他用户可以阅读这篇文章。

If you are serving from apache there are other factors too. 如果你从apache服务还有其他因素。 These settings can make a huge difference - MaxClients and MaxRequestsPerChild . 这些设置可以产生巨大的差异 - MaxClientsMaxRequestsPerChild You will need to tweak them based on the resources available to your server/machine serving the files. 您需要根据服务器/机器可用资源调整它们。

Changing this worked really well for me. 改变这个对我来说非常有效。

Using the google CDN is also a good option although it may not be practical in some situations. 使用谷歌CDN也是一个不错的选择,虽然它在某些情况下可能不实用。

Custom build also has an effect as pointed out in other answers. 自定义构建也具有其他答案中指出的效果。

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

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