Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
188
|
第
17
章
代码既想用在后端又想用在客户端该怎么办?有多种方法可以解决这个问题,但无论如
何,发往客户端的
JavaScript
通常都是静态的。
二进制下载文件
这个分类可以包含一切:
PDF
、压缩文件、
W
ord
文档、安装程序,等等。
如果只是在构建一个
API
,可能就没有静态资源。如果是这样,则可以跳过
本章。
17.1
 性能上的考量
静态资源的处理方式可以显著影响网站的实际性能,尤其是网站主要提供多媒体服务的时
候。两个最主要的性能考量,一是要
降低请求的数量
,二是要
缩减内容的大小
。
在两者之中,降低(
HTTP
)请
求的数量更为关键,尤其是对移动端来说(通过蜂窝网络
建立
HTTP
连接的开销要大很多)
。减少请求的数量可以通过两种途径来达到:合并资源
和浏览器缓存。
合并资源主要从架构和前端方面考虑:小图片应该尽可能地合并成单一文件。合并之后再
使用
CSS
来设置图片的偏移和大小
,只显示想要的部分。要创建合并的单一文件,我强烈
推荐一个免费服务——
SpritePad
。它使得合并图片简单得不可思议
,同时也会生成
CSS
。
没有比这更容易操作的了。
SpritePad
的免费功能对你来说很可能已经够用了
,但如果要创
建大量的合并文件,也许你会觉得它的付费服务是很值得的。
浏览器缓存也可以通过把常用的静态资源存储在客户端浏览器上的方式来减少
HTTP
请
求。尽管浏览器在很大程度上已经把缓存自动化,但它并不是什么“魔法”,而在让浏览
器缓存静态资源这件事上,你还有很多可以做也是应该做的。
最后,缩减静态文件的大小也可以提升性能。有些技术是 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099