Deploying Durandal SPA to Production

本文介绍如何使用Durandal框架部署单页应用至生产环境。通过优化工具将所有JavaScript和HTML文件打包为单一文件main-built.js,加快加载速度。文章还介绍了如何配置Require.js以避免缓存问题。

Link from: http://www.binoot.com/2013/04/18/deploying-durandal-spa-to-production/

 

I have been building a single page application (SPA) using Durandal JS. In this post, I want to talk about some of the steps that you want to take when you are deploying your single page application to your production/live web server. In my case I am using ASP.Net MVC 4 and I use Windows Azure to host my website. If you want to learn more about how to build a SPA using Durandal, I strongly suggest you watchJohn Papa‘s SPA Jump Start course in Pluralsight.

Typically your SPA application will have a structure like this:

In this all the files except the main-built.js is your application. This includes Durandal, Require JS, your files, etc. “main-built.js” is the optimized single file that contains your javascript and html files that you need to deploy to your production web server. So you do you package this for production? This is simple than I thought. I was thinking of using ASP.Net Bundle capability to bundle all the JS files, but decided against it because I felt to manage my SPA files using ASP.Net bundling could become very tricky very fast because of paths, and html files. So I went with the optimizer that is included with Durandal.

Once you install durandal using Nuget ( or HotTowel Nuget), you will see under App\durandal\amd\ you have a file called optimizer.exe. This is the tool we will use to package our SPA application.

  1. Step1: To use this you will need to install Node.js, So get node.js and install. You can download Node.js from here.
  2. Click on Start > All Programs > Node.js > Node js command prompt
  3. Navigate to your projects \app\durandal\amd folder
  4. Run optimizer.exe
  5. This will scan everything under your \App folder and creates the combined and optimized file main-built.js

Now we can use this file in our application, following code snippet shows how to include this in your index.chtml file. We will use the full files when we are in debug mode and when we are running a release configuration, use the optimized file.

1
2
3
4
5
6
7
8
@Scripts.Render( "~/scripts/vendor" )
@ if (HttpContext.Current.IsDebuggingEnabled) {
      <script src= "~/App/durandal/amd/require.js" data-main= "App/main" ></script>
}
else
{
     <script type= "text/javascript" src= "~/App/main-built.js" ></script>
}

As you can see, I am using ASP.Net Bundles to combine and compress all the standard JS files like bootstrap, jquery plugins, or any other components that I use and are not part of my SPA code and use the optimizer created file for my SPA code. With this approach, our application code (SPA code) is send to browser as a single file enabling faster download. If you want to bust cache when each time you are deploying new code to your server, you can configure Require.js to include a query string parameter to each resource it fetches.

1
2
3
4
require.config({
     paths: { "text" : "durandal/amd/text" },
     urlArgs: "bust=" + ( new Date()).getTime()
});

Reference: http://requirejs.org/docs/api.html#config.

Please note that the code snippet above is ideal only for development, for your release code, change this to something like :

urlArgs: "bust=v1.1"

This will make sure that your users will not be downloading any resources that browser already has unless it is changed (which typically happens when you have a new release)

That’s all for now folks! Happy Programming!
Cheers!
Binu

 

先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
基于蒙特卡洛,copula函数,fuzzy-kmeans获取6个典型场景进行随机优化多类型电动汽车采用分时电价调度,考虑上级电网出力、峰谷差惩罚费用、风光调度、电动汽车负荷调度费用和网损费用内容概要:本文围绕多类型电动汽车在分时电价机制下的优化调度展开研究,采用蒙特卡洛模拟、Copula函数和模糊K-means聚类方法获取6个典型场景,并在此基础上进行随机优化。模型综合考虑了上级电网出力、峰谷差惩罚费用、风光可再生能源调度、电动汽车负荷调度成本以及电网网损费用等多个关键因素,旨在实现电力系统运行的经济性与稳定性。通过Matlab代码实现相关算法,验证所提方法的有效性与实用性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源、智能电网、电动汽车调度相关工作的工程技术人员。; 使用场景及目标:①用于研究大规模电动汽车接入电网后的负荷调控策略;②支持含风光等可再生能源的综合能源系统优化调度;③为制定合理的分时电价政策及降低电网峰谷差提供技术支撑;④适用于学术研究、论文复现与实际项目仿真验证。; 阅读建议:建议读者结合文中涉及的概率建模、聚类分析与优化算法部分,动手运行并调试Matlab代码,深入理解场景生成与随机优化的实现流程,同时可扩展至更多元化的应用场景如V2G、储能协同调度等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值