简体   繁体   English

如果我的Backbone main.js太大,我该怎么办?

[英]What can I do if my Backbone main.js is too big?

I use require.js (AMD) and optimize it into one final javascript file (main.js) 我使用require.js(AMD)并将其优化为一个最终的javascript文件(main.js)

My main.js is half a megabyte, and it will continue to increase. 我的main.js是半兆字节,它会继续增加。

The problem is: when the client loads my backbone app, they must first download the entire half megabyte file before they can browse the site. 问题是:当客户端加载我的骨干应用程序时,他们必须首先下载整个半兆字节文件才能浏览网站。 (the router is included inside the file) (路由器包含在文件中)

Oftentimes this can take 3 seconds before the initial load. 通常,这可能需要在初始加载之前3秒。

What can I do? 我能做什么?

Split your code up into the following categories and use separate requirejs modules for each. 将代码拆分为以下类别,并为每个类别使用单独的requirejs模块。

  1. Core code required to get the initial app displayed 获取初始应用程序所需的核心代码
    • Most of your modules that are heavily used through your app can go here 您通过应用程序大量使用的大多数模块都可以访问此处
  2. Secondary code that is OK to arrive in a subsequent request via AMD 可以通过AMD到达后续请求的二级代码
    • things like animation helpers, stuff that only appears on click, etc 像动画助手,只出现在点击上的东西等等
  3. Any modules that are only needed in the corners of your app 任何只在应用程序角落需要的模块
    • specialty error handlers 专业错误处理程序
    • edge case logic 边缘情况逻辑
    • paths most users don't take through your app (like a refund module in a e-commerce app) 大多数用户不通过您的应用程序的路径(如电子商务应用程序中的退款模块)

That way with a single, hopefully reasonably sized request, your app is up and running. 这样,只需一个希望合理大小的请求,您的应用就会启动并运行。 A second request takes another second or so to get you 98% loaded, and the last 2% trickles in on-demand if a given user needs it. 第二个请求需要另外一秒左右才能使您获得98%的负载,如果给定用户需要,则最后2%请求按需流入。

Requirejs has facilities for doing this via the config file. Requirejs具有通过配置文件执行此操作的工具。 It just takes some forethought and testing to find the right places to draw the dividing lines for your specific application. 只需要进行一些预见和测试,找到适合您特定应用的划分线的正确位置。

General tips: consider fewer large third party dependencies. 一般提示:考虑较少的大型第三方依赖项。 Consider a separate bundle for administrators if you have different user roles. 如果您有不同的用户角色,请考虑为管理员使用单独的捆绑

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

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