简体   繁体   English

是否有一种设计模式/系统,用于延迟加载捆绑的AMD模块而不加载我已经下载的模块?

[英]Is there a design pattern/system for lazy loading bundled AMD modules without loading modules I've already downloaded?

Suppose I have a large single-page application which is built around AMD modules. 假设我有一个围绕AMD模块构建的大型单页应用程序。 When the user first visits the front page of this application, we want the initial JS payload to be reasonably light so we only load the minimum JS to set up the app and load the front page. 当用户首次访问该应用程序的首页时,我们希望初始JS负载相当轻,因此我们仅加载最小的JS来设置应用程序并加载首页。 From there the user can do "Action A" or "Action B". 从那里,用户可以执行“动作A”或“动作B”。 Each "action" requires new modules ( "module_a" and "module_b" repectively) to be loaded. 每个“操作”都需要加载新模块(分别为"module_a""module_b" )。

module_a 's dependencies are module_a_1 , module_a_2 , module_a_3 , and module_X module_b 's dependencies are module_b_1 , module_b_2 , module_b_3 , and module_X module_a的依赖关系为module_a_1module_a_2module_a_3module_X module_b的依赖关系为module_b_1module_b_2module_b_3module_X

Notice that both modules depend on module_X . 注意,两个模块都依赖于module_X

Now, if the user performs "Action A" we tell the server we want module_a . 现在,如果用户执行“操作A”,我们告诉服务器我们需要module_a The server bundles module_a and all of its dependencies into the response. 服务器将module_a及其所有依赖项捆绑到响应中。 If the user performs "Action A" again, the client knows we already have module_a so it doesn't have to download anything new. 如果用户再次执行“操作A”,则客户端知道我们已经有了module_a因此不必下载任何新内容。 That's easy. 这很容易。

But suppose the user performs "Action A" and a little while later, performs "Action B". 但是,假设用户执行“动作A”,不久之后,执行“动作B”。 Clearly we need to download module_b , module_b_1 , etc. but the client already has module_X . 显然,我们需要下载module_bmodule_b_1等, 但是客户端已经具有module_X It would be a shame if module_X came bundled with the request for module_b because we already have module_X . 如果module_X与对module_b的请求捆绑在一起,那将是可耻的,因为我们已经有了module_X

Is there a system/framework/pattern that supports the requesting AMD modules and their dependencies but not the dependencies the client already has, perhaps by somehow communicating to the server the modules the client has? 是否有一个系统/框架/模式支持请求的AMD模块及其依赖关系,但不支持客户端已经具有的依赖关系,也许通过某种方式与服务器通信客户端所拥有的模块?

Is there a system/framework/pattern that supports the requesting AMD modules and their dependencies but not the dependencies the client already has, perhaps by somehow communicating to the server the modules the client has? 是否有一个系统/框架/模式支持请求的AMD模块及其依赖关系,但不支持客户端已经具有的依赖关系,也许通过某种方式与服务器通信客户端所拥有的模块?

It seems you're missing the point of AMD, since that's exactly what it does -- if module_X is already loaded, there will not be another request to the server for module_X . 似乎您没有注意到AMD的要点,因为这正是它的作用-如果module_X已经加载,则不会module_X服务器发出关于module_X请求。

Reading between the lines of the question, I think you're not stating it properly. 仔细阅读问题的内容,我认为您的陈述不正确。 I think the situation is this: 我认为情况是这样的:

You're not specifying all the module dependencies at the beginning of your module definition. 您没有在模块定义的开头指定所有模块依赖性。 Using nested inner requires, you're lazy loading some dependencies until the moment that they're needed. 使用嵌套的内部需求,您会懒惰地加载某些依赖项,直到需要它们为止。 As a result, what you find is that the inner require loads dependencies you've already declared as part of your outer dependency, or as part of another inner require dependency. 结果,您会发现内部需求加载已作为外部依赖的一部分或另一个内部需求依赖的一部分声明的依赖。

If this is the problem, then the short answer is that the nested require needs the loading context of the outer require. 如果这是问题所在,那么简短的答案是嵌套的require需求和外部require的加载上下文。 This is done by specifying require itself as a dependency 通过将require本身指定为依赖项来完成

require(["require", /* other deps */], function(require, ...){
   require(["module_a_1","module_X"], function(moda1,modx){ /*... */});
   require(["module_b_1","module_X"], function(modb1,modx){ /*... */});
}

This isn't very well documented in RequireJS's own documentation. RequireJS自己的文档中没有对此进行很好的记录。 See https://github.com/jrburke/requirejs/issues/173 for more details. 有关更多详细信息,请参见https://github.com/jrburke/requirejs/issues/173

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

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