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版)
192
|
第
17
章
17.5
 变更静态内容
缓存显著提升了网站的性能,但也有不利的一面,尤其是当你修改了某些静态资源时,在
浏览器缓存的版本过期之前,客户端可能看不到修改。谷歌推荐至少缓存一个月,缓存一
年则更好。假设有一个用户每天都在同一个浏览器上使用你的网站,那么他得整整一年才
能看到更新。
这种情形显然不是我们所希望的,而且你也不能逐一通知你的用户去清理缓存。解决方案
是使用
缓存破坏
。缓存破坏这项技术可以控制用户的浏览器何时重新下载某个资源。通常
缓存破坏是在资源名上带上一个版本号(
main.2.css
或
main.css?version=2
)
,或者带上某种
散列(
main.e16b7e149dccfcc399
e025e0c454bf77.css
)。不管是哪一种做法,变更这个资源时
资源名也会随之改变,而浏览器便知道了需要去下载它。
对多媒体资源也可以这样处理。不妨拿
logo
(
/static/img/mea
dowlark_logo.png
)做例子。
为了最大化性能,我们把它托管在
CDN
上
,指定一年后过期,之后如果修改这个
logo
,
那么用户在满一年之前可能都不会看到变更的
logo
。然而
,如果把它重命名为
static/img/
meadowlark_logo-1.png
(并且在
HTML
中体现出这个文件名修改)
,浏览器就只能下载它,
因为它看起来是一个新的资源。
如果使用一个单页应用的框架,类似
create-react-app
这样的工具就会提供一个构建步
骤,用以创建适合生产的资源包文件,并且会在这些资源包的文件名上追加一个散列。 ...
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