我读过SPA和它的优点。我发现他们中的大多数都没有说服力。有3个优点引起了我的怀疑。
问:你能作为SPA的倡导者,证明我的前三个陈述是错误的吗?
=== ADVANTAGES ===
1. SPA非常适合响应性非常强的网站:
服务器端呈现很难为所有的中间体实现
状态——小视图状态不能很好地映射到url。
单页应用程序的特点是它们能够重绘任何部分
的UI,而不需要服务器往返检索HTML。这
是通过将数据与数据的表示分开来实现的
拥有处理数据的模型层和读取数据的视图层
从模型中。
为非spa保留模型层有什么问题?SPA是唯一在客户端与MVC兼容的架构吗?
2. 使用SPA,我们不需要对服务器使用额外的查询来下载页面。
哈,用户在访问你的网站时可以下载多少页面?2、3 ?相反,出现了另一个安全问题,你需要把你的登录页面,管理页面等分开到单独的页面。反过来,它又与SPA体系结构冲突。
3.还有其他的优势吗?别再听别人说了。
=== DISADVANTAGES ===
客户端必须启用javascript。
只有一个入口。
安全。
附注:我做过SPA项目和非SPA项目。我问这些问题是因为我需要加深我的理解。没有伤害七党联盟支持者的意思。不要让我读更多关于SPA的东西。我只是想听听你对此的看法。
缺点:
从技术上讲,SPA的设计和初始开发是复杂的,可以避免。不使用这种SPA的其他原因可能是:
a) Security: Single Page Application is less secure as compared to traditional pages due to cross site scripting(XSS).
b) Memory Leak: Memory leak in JavaScript can even cause powerful Computer to slow down. As traditional websites encourage to navigate among pages, thus any memory leak caused by previous page is almost cleansed leaving less residue behind.
c) Client must enable JavaScript to run SPA, but in multi-page application JavaScript can be completely avoided.
d) SPA grows to optimal size, cause long waiting time. Eg: Working on Gmail with slower connection.
除此之外,其他架构的限制是导航数据丢失,浏览器中没有导航历史记录,以及使用selenium难以进行自动化功能测试。
这个链接解释了单页应用程序的优点和缺点。
缺点:
从技术上讲,SPA的设计和初始开发是复杂的,可以避免。不使用这种SPA的其他原因可能是:
a) Security: Single Page Application is less secure as compared to traditional pages due to cross site scripting(XSS).
b) Memory Leak: Memory leak in JavaScript can even cause powerful Computer to slow down. As traditional websites encourage to navigate among pages, thus any memory leak caused by previous page is almost cleansed leaving less residue behind.
c) Client must enable JavaScript to run SPA, but in multi-page application JavaScript can be completely avoided.
d) SPA grows to optimal size, cause long waiting time. Eg: Working on Gmail with slower connection.
除此之外,其他架构的限制是导航数据丢失,浏览器中没有导航历史记录,以及使用selenium难以进行自动化功能测试。
这个链接解释了单页应用程序的优点和缺点。
在我的发展过程中,我发现使用SPA有两个明显的优势。这并不是说在传统的web应用程序中不能实现以下功能,只是我看到了增量收益而不会引入额外的缺点。
Potential for less server request as rendering new content isn’t always or even ever an http server request for a new html page. But I say potential because new content could easily require an Ajax call to pull in data but that data could be incrementally lighter than the itself plus markup providing a net benefit.
The ability to maintain “State”. In its simplest terms, set a variable on entry to the app and it will be available to other components throughout the user’s experience without passing it around or setting it to a local storage pattern. Intelligently managing this ability however is key to keep the top level scope uncluttered.
在我看来,除了需要JS之外(这对web应用程序来说并不是一件疯狂的事情),其他的缺点要么不是SPA特有的,要么可以通过良好的习惯和开发模式来缓解。
对于谷歌、亚马逊等公司来说,他们的服务器在24/7模式下以最大容量运行,减少流量意味着真正的金钱——更少的硬件、更少的能源、更少的维护。将cpu使用从服务器转移到客户端是有回报的,spa大放异彩。到目前为止,利大于弊。
所以,SPA与否很大程度上取决于用例。
只是为了提到另一个可能不那么明显(对于web开发人员)的spa用例:
我目前正在寻找一种在嵌入式系统中实现gui的方法,基于浏览器的架构似乎对我很有吸引力。传统上,嵌入式系统中的ui没有太多的可能性——Java、Qt、wx等或适当的商业框架。几年前,Adobe试图通过flash进入市场,但似乎并不成功。
如今,由于“嵌入式系统”与几年前的大型机一样强大,基于浏览器的UI通过REST连接到控制单元是一种可能的解决方案。这样做的好处是,为用户界面提供了大量的工具,而且是免费的。(例如Qt需要20-30美元/销售单位的特许权使用费加上3000-4000美元/开发者)
对于这样的架构,SPA提供了许多优势——例如,桌面应用程序开发人员更熟悉的开发方法,减少服务器访问(通常在汽车行业,UI和系统混乱是分开的硬件,其中系统部分有一个RT操作系统)。
由于唯一的客户端是内置的浏览器,上面提到的js可用性、服务器端日志记录、安全性等缺点都不再重要。
缺点
1. 客户端必须启用javascript。是的,这是SPA的一个明显的缺点。在我的例子中,我知道我可以期望我的用户启用JavaScript。如果你不能,那么你就不能做SPA,就这样。这就像试图将。net应用程序部署到一台没有安装。net框架的机器上。
2. 只有一个入口。我用SammyJS解决了这个问题。2-3天的工作,让你的路由正确设置,人们将能够创建深度链接书签到你的应用程序,并正常工作。你的服务器只需要公开一个端点——“给我这个应用程序的HTML + CSS + JS”端点(把它想象成预编译应用程序的下载/更新位置)——而你编写的客户端JavaScript将处理进入应用程序的实际入口。
3. Security. This issue is not unique to SPAs, you have to deal with security in exactly the same way when you have an "old-school" client-server app (the HATEOAS model of using Hypertext to link between pages). It's just that the user is making the requests rather than your JavaScript, and that the results are in HTML rather than JSON or some data format. In a non-SPA app you have to secure the individual pages on the server, whereas in a SPA app you have to secure the data endpoints. (And, if you don't want your client to have access to all the code, then you have to split apart the downloadable JavaScript into separate areas as well. I simply tie that into my SammyJS-based routing system so the browser only requests things that the client knows it should have access to, based on an initial load of the user's roles, and then that becomes a non-issue.)
优势
A major architectural advantage of a SPA (that rarely gets mentioned) in many cases is the huge reduction in the "chattiness" of your app. If you design it properly to handle most processing on the client (the whole point, after all), then the number of requests to the server (read "possibilities for 503 errors that wreck your user experience") is dramatically reduced. In fact, a SPA makes it possible to do entirely offline processing, which is huge in some situations.
Performance is certainly better with client-side rendering if you do it right, but this is not the most compelling reason to build a SPA. (Network speeds are improving, after all.) Don't make the case for SPA on this basis alone.
Flexibility in your UI design is perhaps the other major advantage that I have found. Once I defined my API (with an SDK in JavaScript), I was able to completely rewrite my front-end with zero impact on the server aside from some static resource files. Try doing that with a traditional MVC app! :) (This becomes valuable when you have live deployments and version consistency of your API to worry about.)
因此,底线是:如果您需要脱机处理(或者至少希望您的客户端能够在偶尔的服务器中断中存活)—极大地降低您自己的硬件成本—并且您可以假设JavaScript和现代浏览器,那么您需要一个SPA。在其他情况下,这更像是一种权衡。